復(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 ; }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站vcdvsql.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁題目:關(guān)于C++復(fù)制構(gòu)造函數(shù)的實(shí)現(xiàn)講解-創(chuàng)新互聯(lián)
文章路徑:http://vcdvsql.cn/article14/djjcde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、品牌網(wǎng)站建設(shè)、品牌網(wǎng)站制作、域名注冊(cè)、云服務(wù)器、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容