您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 其它文档 > 实验九-派生与继承—构造与析构参考答案
实验九派生与继承——构造与析构函数9.1实验目的1.理解派生类中构造函数和析构函数及其调用顺序。2、理解虚基类的应用。9.2实验内容9.2.1程序阅读1.理解下面的程序,并运行查看结果,回答程序后面的问题。#includeiostreamusingnamespacestd;classCBase{public:CBase(inta):a(a){}voidprint(){couta=aendl;}private:inta;};classCDerive:publicCBase{public:CDerive(inta,ints):CBase(a){b=s;}voidprint(){coutb=bendl;}private:intb;};intmain(){CDerived(3,4);d.print();//-----------------------------------------------------①CBaseb(4);b.print();//-----------------------------------------------------②return0;}问题一:请指出①与②两处的print函数调用有何区别?答:①处是调用派生类CDerive的成员函数,在派生类中函数print()与基类中的print()同名,所以它会覆盖基类重中的print()函数,在调用函数时执行派生类的print()函数;而②处是调用CBase类的成员函数。问题二:修改程序,使程序能输出基类的数据成员a。答:1、在①处后面增加代码d.CBase::print();2、在派生类的print函数中增加语句CBase::print();2.理解下面的程序,并运行查看结果,回答程序后面的问题。#includeiostreamusingnamespacestd;classCBase{public:CBase(inta):a(a){coutbasestructureendl;}~CBase(){coutbasedestructureendl;}voidprint(){couta=aendl;}inta;};classCDerive:publicCBase{public:CDerive(inta,intb,intc):CBase(a),b(b),c(c)//-----------------------------------------------------①{coutderivestructureendl;}~CDerive(){coutderivedestructureendl;}voidprint(){CBase::print();coutb.a=b.aendl;coutc=cendl;}private:CBaseb;intc;};intmain(){CDerived(1,2,3);//-----------------------------------------------------②d.print();return0;}问题一:请解释①处的含义?答:派生类构造函数先调用基类CBase的构造函数,对基类构造函数的数据成员初始化;,再调用子对象b的构造函数,对子类的数据成员初始化;最后执行派生类构造函数本身,对派生类数据成员c初始化。问题二:②处语句执行完后,d.b.a的值为多少?答:2问题三:以上程序的输出结果是什么,说明为什么?答:因为派生类构造函数先调用基类CBase的构造函数,将基类构造函数的数据成员a初始化为1;,再调用子对象b的构造函数,将子类的数据成员a初始化为2;再执行派生类构造函数本身,将派生类数据成员c初始化为3;然后执行派生类的析构函数,接着是子类的析构函数,最后是基类的析构函数。3.以下程序实现的继承关系如下图所示,理解下面的程序,并运行查看结果,回答程序后面的问题。#includeiostream#includestringusingnamespacestd;//声明公共基类PersonclassPerson{public:Person(stringnam,chars,inta)//构造函数{name=nam;sex=s;age=a;}protected://保护成员stringname;charsex;intage;};//声明Person的直接派生类TeacherclassTeacher:publicPerson{public:Teacher(stringnam,chars,inta,stringt):Person(nam,s,a)//构造函数{title=t;}protected://保护成员stringtitle;};//声明Person的直接派生类StudentclassStudent:publicPerson{public:Student(stringnam,chars,inta,floatsco)//构造函数:Person(nam,s,a),score(sco){}//初始化表protected://保护成员floatscore;//成绩};//声明多重继承的派生类GraduateclassGraduate:publicTeacher,publicStudent//Teacher和Student为直接基类{public:Graduate(stringnam,chars,inta,stringt,floatsco,floatw)//构造函数:Person(nam,s,a),Teacher(nam,s,a,t),Student(nam,s,a,sco),wage(w){}//初始化表voidshow()//输出研究生的有关数据{coutname:nameendl;coutage:ageendl;coutsex:sexendl;coutscore:scoreendl;couttitle:titleendl;coutwages:wageendl;}private:floatwage;//工资};//主函数intmain(){Graduategrad1(Wang-li,'f',24,assistant,89.5,1234.5);grad1.show();return0;}问题一:以上程序错误的原因是什么?请改正。答:由于多重继承引起的的二义性,程序在执行函数show()输出数据成员时无法区别是基类Person的成员,还是Teacher类从基类继承下来的成员,还是Student类从基类继承下来的成员;改正:应在Teacher类和Student类继承基类前加关键字virtual使程序只保留一份成员:classTeacher:virtualpublicPersonclassStudent:virtualpublicPerson问题二:请修改程序,在构造与析构函数中输出一些信息以理解对象建立的过程。答:#includeiostream#includestringusingnamespacestd;//声明公共基类PersonclassPerson{public:Person(stringnam,chars,inta)//构造函数{name=nam;sex=s;age=a;cout这是Person类的构造函数!endl;}~Person(){cout这是Person类的析造函数!endl;}protected://保护成员stringname;charsex;intage;};//声明Person的直接派生类TeacherclassTeacher:virtualpublicPerson{public:Teacher(stringnam,chars,inta,stringt):Person(nam,s,a)//构造函数{title=t;cout这是Teacher类的构造函数!endl;}~Teacher(){cout这是Teacher类的析造函数!endl;}protected://保护成员stringtitle;};//声明Person的直接派生类StudentclassStudent:virtualpublicPerson{public:Student(stringnam,chars,inta,floatsco)//构造函数:Person(nam,s,a),score(sco){cout这是Student类的构造函数!endl;}//初始化表~Student(){cout这是Student类的析造函数!endl;}protected://保护成员floatscore;//成绩};//声明多重继承的派生类GraduateclassGraduate:publicTeacher,publicStudent//Teacher和Student为直接基类{public:Graduate(stringnam,chars,inta,stringt,floatsco,floatw)//构造函数:Person(nam,s,a),Teacher(nam,s,a,t),Student(nam,s,a,sco),wage(w){cout这是Graduate类的构造函数!endl;}//初始化表~Graduate(){cout这是Graduate类的析造函数!endl;}voidshow()//输出研究生的有关数据{coutname:nameendl;coutage:ageendl;coutsex:sexendl;coutscore:scoreendl;couttitle:titleendl;coutwages:wageendl;}private:floatwage;//工资};//主函数intmain(){Graduategrad1(Wang-li,'f',24,assistant,89.5,1234.5);grad1.show();return0;}9.2.2程序设计1.定义点CPoint类作为基类,在此基础上派生出直线CLine类和圆CCircle类,并要求基类和各派生类具有以下特点:a.CLine类含有计算直线长度和斜率的成员函数;b.CCircle类含有计算圆面积的成员函数。(提示:直线可由两个点确定,圆可由一个点和半径确定)#includeiostream#includemath.husingnamespacestd;classCPoint{public:doublex;doubley;public:CPoint(doublex,doubley):x(x),y(y){}};classCLine:publicCPoint{private:CPointa;public:CLine(doublex,doubley,doublex1,doubley1):CPoint(x,y),a(x1,y1){}voidLength(){doublel;l=sqrt((x-a.x)*(x-a.x)+(y-a.y)*(y-a.y));cout直线长度=l米endl;}voidSlope(){doublek;k=(a.y-y)/(a.x-x);cout直线斜率=kendl;}};classCCircle:publicCPoint{private:doubler;public:CCircle(doublex,doubley,doubler):CPoint(x,y),r(r){}voidarea(){doubleS;S=3.14*r*r;cout圆的面积=S平方米endl;}};intmain(){CLinea(0,0,1,1);a.Length();a.Slope();CC
本文标题:实验九-派生与继承—构造与析构参考答案
链接地址:https://www.777doc.com/doc-4919276 .html