类和对象
#include<iostream>#include<string.h>
using namespace std;
class MyString
{
private:
char*buffer;
public:
MyString(const char* initString) // default constructor
{
buffer = NULL;
if(initString != NULL)
{
buffer=new char[strlen(initString) +1];
strcpy(buffer, initString);
}
}
MyString(const MyString& copySource)//copy constructor
{
buffer=NULL;
if(copySource.buffer!=NULL)
{
buffer = new char [strlen(copySource.buffer) +1];
}
}
~MyString()
{
delete[]buffer;
}
int GetLength()
{return strlen(buffer);}
const char*GetString()
{return buffer;}
} ;
class Human
{
private:
int age;
bool gender;
MyString name;
pubilc:
Human(const MyString& InputName,int InputAge,bool gender)
:name(InputName),age(InputAge),gender(gender){}
int GetAge()
{return age;}
};
int main()
{
MyString mansName("Adam");
MyString womansName("Eve");
cout<<"sizeof(MyString) = "<<sizeof(MyString)<<endl;
cout<<"sizeof(mansName) = "<<sizeof(mansName)<<endl;
cout<<"sizeof(womansName) = "<<sizeof(womansName)<<endl;
请大神指点一下,到底是错在哪里 编译器报错的在下面截图,代码和书上的一样啊,怎么会报错啊,我用的是Dev-C++ 5.1
Human firstMan(mansName,25,true);
Human firstWoman(womnasNmae,18 false); 谢谢了
cout<<"sizeof(Human) = "<<sizeof(Human)<<endl;
cout<<"sizeof(firstMan) = "<<sizeof(firstMan)<<endl;
cout<<"sizeof(firstWoman) =" <<sizeof(firstwoman)<<endl;
}