📄 15-1.txt
字号:
/* 范例:15-1 构造方式1、2、3、4 */
#include <iostream.h>
struct hand
{
int length;
};
class person
{
private:
char blood; char *name;
public:
hand *lefthand;
hand *righthand;
person(void) /* 构造函数,可以有多个构造函数,以参数决定使用哪一个,
对象构造后自动执行 */
{ // new为C++配置内存的指令
lefthand = new hand; righthand = new hand;
cout << "/* 构造方式1和3 */" << endl;
cout << "person class Constructor \n"; blood = 'O';
}
person(char *ch) /* 析构函数,可以重载,以参数决定调用哪一个。new为C++
配置内存的指令 */
{
lefthand = new hand; righthand = new hand;
name = ch;
cout << "\n/* 构造方式2和4 */" << endl;
cout << "person(char *ch) class Constructor \n";
cout << "------------------------\n";
}
~person(void) /* 析构函数,不能overload,且无传入值,对象消失前自动执行 */
{ // delete为C++删除内存的指令
delete lefthand; delete righthand;
cout << "person class Destructor \n";
}
void SetBlood(char a){ blood = a; }
char GetBlood(void){ return blood;}
void get_S_addr(){cout << "sca blood address=" \
<< (void *)&blood <<endl;}
void get_P_addr(){cout << "Peter blood address=" \
<< (void *)&blood <<endl;}
void ShowHandLen(void)
{
cout << "------------------------\n";
cout << "hand left=" <<lefthand->length << " hand right="
<< righthand->length << '\n';
}
void display_Pei(void)
{cout << "人生目标:" << name << endl \
<< "name指针中存放的是ch的址: " \
<< (void*)name << endl << "name address = " \
<< &name << endl; }
};
//---------------------------------------------------------------------------
void main()
{
person sca; // 直接操作方式构造对象,第一种构造方式
//person sca = person(); /* 直接构造对象,第三种构造方式,这和构造方式一是相
同的,并调用其构造函数。 */
sca.lefthand->length = 100; sca.righthand->length = 150;
sca.ShowHandLen();
cout << "sca blood=" << sca.GetBlood() << '\n';
cout << "sca address=" << &sca << endl;
cout << "sca对象大小 = " << sizeof(sca) << "bytes" << endl;
sca.get_S_addr();
cout << "sca lefthand address=" << &sca.lefthand << endl;
cout << "sca righthand address=" << &sca.righthand <<endl;
cout << "sca.lefthand->length=" << &sca.lefthand->length << endl;
cout << "sca.righthand->length=" << &sca.righthand->length << endl;
char ch[20] = "Smile Always! ^_^";
person pei_Pei(ch); /* 第二种构造方式,构造对象并对应到ch相对应的构造函
数。*/
//person pei_Pei = person(ch); //第四种构造方式, 且设定ch的初始值
/* 构造对象时传参数给构造函数,定义字符字符串对象,并自动调用相对应的构造
函数,这和第四种构造方式是一样的,所以也可以写成
person pei_pei = person(ch); */
pei_Pei.display_Pei(); /* 调用pei_Pei的成员函数display_Pei(),显示字符串内容。 */
cout << "ch size = " << sizeof(ch) <<endl;
cout << "pei_Pei address=" << &pei_Pei << endl \
<< "char ch address = " << (void *)&ch \
<< endl <<"\npei_Pei对象的大小 = " \
<< sizeof(pei_Pei) << "bytes" << endl;
cout << "ch: " << ch;
getchar();
}
程序执行结果:
/* 构造方式1和3 */
person class Constructor
------------------------
hand left=100 hand right=150
sca blood=O
sca address=0065FDF4
sca对象大小 = 16bytes
sca blood address=0065FDF4
sca lefthand address=0065FDFC
sca righthand address=0065FE00
sca.lefthand->length=00682F00
sca.righthand->length=00682F10
/* 构造方式2和4 */
person(char *ch) class Constructor
------------------------
人生目标:Smile Always! ^_^
name指针中存放的是ch的址: 0065FDAC
name address = 0065FDE8
ch size = 20
pei_Pei address=0065FDE4
char ch address = 0065FDAC
pei_Pei对象的大小 = 16bytes
ch: Smile Always! ^_^
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -