關(guān)于C++復(fù)制構(gòu)造函數(shù)的實(shí)現(xiàn)講解
復(fù)制構(gòu)造函數(shù)是一種特殊的構(gòu)造函數(shù),有一般構(gòu)造函數(shù)的特性。它的功能是用一個(gè)已知的對(duì)象來初始化一個(gè)被創(chuàng)建的同類對(duì)象。復(fù)制構(gòu)造函數(shù)的參數(shù)傳遞方式必須按引用來進(jìn)行傳遞,請(qǐng)看實(shí)例:
#include <iostream> #include <cstring> using namespace std ; class Student { private : char name[8]; int age ; char sex ; int score ; public : void disp(); //打印信息的函數(shù)聲明 Student(char name[],int age , char sex ,int score); //構(gòu)造函數(shù)聲明 Student(Student &dx); //復(fù)制構(gòu)造函數(shù)的聲明 ~Student(); //析構(gòu)函數(shù)的聲明 }; //打印信息函數(shù)的實(shí)現(xiàn) void Student::disp() { cout << this->name << endl ; cout << this->age << endl ; cout << this->sex << endl ; cout << this->score << endl ; } //構(gòu)造函數(shù)的實(shí)現(xiàn) Student::Student(char name[],int age , char sex ,int score) { strcpy(this->name,name); this->age = age ; this->sex = sex ; this->score = score ; } //復(fù)制構(gòu)造函數(shù)的實(shí)現(xiàn) Student::Student(Student &dx) { strcpy(this->name , dx.name); this->age = dx.age ; this->sex = dx.sex ; this->score = dx.score ; } //析構(gòu)函數(shù)的實(shí)現(xiàn) Student::~Student() { cout << "程序結(jié)束" << endl ; } int main(void) { Student stu1("YYX",23,'N',86); Student stu2(stu1); stu1.disp() ; stu2.disp() ; return 0 ; }
運(yùn)行結(jié)果:
YYX
23
N
86
YYX
23
N
86
程序結(jié)束
程序結(jié)束
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
C語(yǔ)言簡(jiǎn)明講解操作符++和--的使用方法
++和--運(yùn)算符分別是+=1和-=1的簡(jiǎn)寫。設(shè)計(jì)這樣兩個(gè)運(yùn)算符的本意是?便程序員,但i++和++i使?不恰當(dāng)有時(shí)候會(huì)造成混淆,反倒令剛?cè)腴T的C程序員有點(diǎn)混亂2022-04-04C語(yǔ)言雙向鏈表的表示與實(shí)現(xiàn)實(shí)例詳解
這篇文章主要介紹了C語(yǔ)言雙向鏈表的表示與實(shí)現(xiàn),對(duì)于研究數(shù)據(jù)結(jié)構(gòu)域算法的朋友有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-07-07C語(yǔ)言動(dòng)態(tài)與靜態(tài)分別實(shí)現(xiàn)通訊錄詳細(xì)過程
這篇文章主要為大家介紹了C語(yǔ)言動(dòng)態(tài)與靜態(tài)分別實(shí)現(xiàn)通訊錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02C++ 17標(biāo)準(zhǔn)正式發(fā)布! 更簡(jiǎn)單地編寫和維護(hù)代碼
C++ 17 標(biāo)準(zhǔn)正式發(fā)布:終于能更簡(jiǎn)單地編寫和維護(hù)代碼了!本文為大家介紹了C ++ 17 主要特性,感興趣的小伙伴們可以參考一下2017-12-12

C++實(shí)現(xiàn)圖的鄰接表存儲(chǔ)和廣度優(yōu)先遍歷實(shí)例分析

C指針原理教程之編譯原理-小型計(jì)算器實(shí)現(xiàn)