string_类_构造函数

string

include

include

using namespace std;

int main(int argc , char* argv[] ) {

// 字符串处理类:string 
string strObj;

strObj = "123456789";
strObj += "abcdefg";
strObj = string( "12" ) + "34" + "56";
string strObj2 = "123456";
if( strObj == strObj2 ) {
    cout << strObj << "等于" << strObj2 << endl;
}

int nIndex = strObj.find( '5' );
cout << "5 在" << strObj << "中第" << nIndex << "处" << endl;

return 0;

}

include

include

using namespace std;

class Person {
public:
int getAge( ){return m_age; }

Person& setAge(int age ){
    Person* _this = this;
    _this->m_age=age;
    return *this;
}

double getHeight( ){return m_height; }

private:
string m_name; // 名字
double m_height;//身高
int m_age;//年龄
};

struct MyPerson {
string m_name; // 名字
double m_height;//身高
int m_age;//年龄
};

void setAge( MyPerson* pObj , int nAge) {
pObj->m_age = nAge;
}

int main(int argc , char* argv[] ) {
MyPerson myObj1 , myObj2;
setAge( &myObj1 , 100 ) ;
setAge( &myObj2 , 50 );

Person obj1 , obj2;
obj1.setAge(100); // 有一个隐含的参数,
obj2.setAge( 50 ).setAge(0).setAge(5).setAge(0);


return 0;

}

构造函数

include

using namespace std;

class CNumber {
private:
int m_nNum;
public:
// 拷贝构造: 定义一个对象时,将另一个对象来初始化新的对象
// 就相当于已有的对象拷贝一份到新的对象中.
// 调用时机/调用场合 : 定义变量, 并以本类对象初始化
CNumber(const CNumber& obj){ }
// 转换构造 : 定义一个对象时,将其他类型的值初始化新的对象
// 调用时机/调用场合:定义变量, 并以非本类类型的值初始化
// 禁用隐式转换的关键字:explicit
/explicit/ CNumber(int nNum ):m_nNum(nNum){ }
};

class MyClass {
char* m_pStr;
const int m_constNum;
const int& m_rnNum ;
CNumber m_numObj;
public:
// 初始化列表的初始化顺序: 按照成员变量的声明顺序来初始化
MyClass( )
: m_constNum(0),
m_rnNum( m_constNum),
m_pStr(new char[10]),
m_numObj( 0 )/显示为成员调用构造/
{
cout << "MyClass构造函数被调用" << endl;
}
~MyClass( ) {
cout << "MyClass析构函数被调用" << endl;
}
};

class CPerson {
int m_nAge;
double m_heigth;
public:
CPerson() :m_nAge() , m_heigth() { }
CPerson( int age , double height )
:m_nAge( age ) , m_heigth( height )
{}
};

CNumber fun(const CNumber& obj ) {
return 1;
}

int main( int argc , char* argv[ ] ) {

// 1被隐式转换成CNumber类型
// 隐式转换成功的原因在于: CNumber类中提供了一个int型的转换构造
// 发生的事情: 为fun函数的形参CNumber obj调用了转换构造
// 函数,将整型的1构造成一个CNumber的对象了.
fun( CNumber(1) );

CPerson perObj1;
CPerson perObj( 18 , 1.8 );

CNumber numObj(0);
CNumber numObj1 = numObj;
CNumber numObj2(numObj);
CNumber numObj3 = 0 ;
{
    MyClass obj;
}

return 0;

}

============
自定义文件类

include

using namespace std;

// 文件处理类
// 1. 能够打开一个文件
// 2. 能够读写一个文件
// 3. 能够设置文件的读写位置
// 每一个类定义出来的对象就表示一个文件.
class CFile{
private:
FILE* m_pFile;
public:
CFile(const char* pFilePath=NULL)
:m_pFile(NULL) {
open(pFilePath); // 调用成员函数打开文件.
}

~CFile(){
    if(m_pFile != NULL)
        fclose(m_pFile);
}

bool open(const char* pFilePath){
    // 打开文件
    fopen_s(&m_pFile , pFilePath , "w+");
    return m_pFile != NULL;
}

// 读取文件内容
void read(char* pBuff , int nSize){
    if(m_pFile == NULL)return ;
    fread(pBuff , 1 , nSize , m_pFile);
}
// 将缓冲区的内容写入到文件中.
void write(const char* pBuff , int nSize)
{
    if(m_pFile == NULL)return ;
    fwrite(pBuff,1,nSize,m_pFile);
}

void seek(int pos){
    fseek(m_pFile,pos,SEEK_SET);
}

}

int main()
{
CFile file("1.txt");

file.write("hello" ,strlen("hello") );
int nNum = 100;
file.write((char*)&nNum , sizeof(nNum));

file.seek(0);

char buff[100];
file.read(buff,10);
cout << buff<

}

==============
自定义IO类

include

using namespace std;
class CIoHelper{
public:
CIoHelper& print(char val){cout << val; return this;}
CIoHelper& print(int val) {cout << val; return this;}
CIoHelper& print(const char
pStr) {cout<this;}
CIoHelper& print(int
arry,int nCount){
if( arry == nullptr) return;
for(int i = 0 ; i< nCount ; ++i)
cout < return this;
}
CIoHelper& scan(char& val, const char
message="")
{
cout << message;
cin >> val;
return this;
}
CIoHelper& scan(int& val, const char
message=””){
cout<>val;
return this;
}
CIoHelper& scan(char
const pStr, const char message=””){
cou<> pStr;
return this;
}
CIoHelper& scan(int
arry,int nCount, const char* message=””){
cout< for(int i = 0; i cin >> arry[i];
}
return *this;
}
};

int main()
{
CIoHelper ioObj;
ioObj.print('4').print(12).print("hello");
char ch;
int nNum;
char buff[32];
int nArray[10];
ioObj.scan(ch,"请输入一个字符").scan(nNum,"请输入一个整数");
ioObj.scan(buff,"请输入一串字符:");
}

============
自定义cstring

include

using namespace std;

class CString{
private:
char* m_pStr;
public:
CString(const char* pStr=nullptr):m_pStr(NULL)
{
if(pStr){
int nLen = strlen(pStr);//获取字符串的长度
// 申请对空间,以保存传入进来的字符串.
m_pStr = new char[nLen + 1];
strcpy_s(m_pStr,nLen+1,pStr);
}
}
CString& append(const char* pStr)
{
// pStr -->> ["hello"]
// m_pStr -->> ["123456789"]
// pNewStr-->> [ ]<<==new[]
// pNewStr-->> ["123456789" ]<<==strcpy
// pNewStr-->> ["123456789hello" ]<<==strcat
// 得到两个字符串的总长度
int nSize = strlen(m_pStr) + strlen(pStr)
// 申请新空间
char* pNewStr = new char[nSize + 1];
sprintf_s(pNewStr,nSize+1,"%s%s",m_pStr,pStr);

    // 释放旧的空间
    delete[] m_pStr;
    // 将新空间的首地址保存到m_pStr
    m_pStr = pNewStr;
}

CString& append(int val){
    char buff[32];
    // 将整型保存到字符数组中.
    sprintf_s(buff,32,"%d",val);
    // 调用自身的成员函数(追加字符串)
    append(buff);
}   

};

int main()
{
CString str("123456");

str.append("helo").append(12).append("world");

}

你可能感兴趣的:(string_类_构造函数)