?? 15-1.txt
字號:
/* 范例:15-1 構(gòu)造方式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) /* 構(gòu)造函數(shù),可以有多個構(gòu)造函數(shù),以參數(shù)決定使用哪一個,
對象構(gòu)造后自動執(zhí)行 */
{ // new為C++配置內(nèi)存的指令
lefthand = new hand; righthand = new hand;
cout << "/* 構(gòu)造方式1和3 */" << endl;
cout << "person class Constructor \n"; blood = 'O';
}
person(char *ch) /* 析構(gòu)函數(shù),可以重載,以參數(shù)決定調(diào)用哪一個。new為C++
配置內(nèi)存的指令 */
{
lefthand = new hand; righthand = new hand;
name = ch;
cout << "\n/* 構(gòu)造方式2和4 */" << endl;
cout << "person(char *ch) class Constructor \n";
cout << "------------------------\n";
}
~person(void) /* 析構(gòu)函數(shù),不能overload,且無傳入值,對象消失前自動執(zhí)行 */
{ // delete為C++刪除內(nèi)存的指令
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; // 直接操作方式構(gòu)造對象,第一種構(gòu)造方式
//person sca = person(); /* 直接構(gòu)造對象,第三種構(gòu)造方式,這和構(gòu)造方式一是相
同的,并調(diào)用其構(gòu)造函數(shù)。 */
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); /* 第二種構(gòu)造方式,構(gòu)造對象并對應(yīng)到ch相對應(yīng)的構(gòu)造函
數(shù)。*/
//person pei_Pei = person(ch); //第四種構(gòu)造方式, 且設(shè)定ch的初始值
/* 構(gòu)造對象時傳參數(shù)給構(gòu)造函數(shù),定義字符字符串對象,并自動調(diào)用相對應(yīng)的構(gòu)造
函數(shù),這和第四種構(gòu)造方式是一樣的,所以也可以寫成
person pei_pei = person(ch); */
pei_Pei.display_Pei(); /* 調(diào)用pei_Pei的成員函數(shù)display_Pei(),顯示字符串內(nèi)容。 */
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();
}
程序執(zhí)行結(jié)果:
/* 構(gòu)造方式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
/* 構(gòu)造方式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! ^_^
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -