国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C++ > 正文

關(guān)于C++復(fù)制構(gòu)造函數(shù)的實(shí)現(xiàn)講解

2020-05-23 13:23:54
字體:
供稿:網(wǎng)友

復(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ì)VEVB武林網(wǎng)的支持。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 万州区| 高州市| 阿鲁科尔沁旗| 祁门县| 镇宁| 黔南| 潼南县| 湄潭县| 遂川县| 安西县| 襄樊市| 罗定市| 商洛市| 离岛区| 贵州省| 教育| 济阳县| 茂名市| 湖口县| 大新县| 定兴县| 陇南市| 宜阳县| 宁都县| 宁阳县| 依安县| 沙坪坝区| 通榆县| 二连浩特市| 永平县| 灌云县| 巨野县| 宜宾县| 岳阳市| 文成县| 中西区| 靖江市| 苗栗县| 林西县| 雅安市| 瑞丽市|