欢迎来到天天文库
浏览记录
ID:52906284
大小:105.50 KB
页数:20页
时间:2020-03-31
《实验六 继承与派生.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、继承与组合一、实验目的1.了解继承在面向对象程序设计中的重要作用。2.进一步理解继承与派生的概念。3.掌握通过继承派生出一个新的类的方法。4.了解虚基类的作用和用法。5.掌握类的组合二、实验内容1.请先阅读下面的程序,写出程序运行的结果,然后再上机运行程序,验证自己分析的结果是否正确。(1)#includeusingnamespacestd;classA{public:A(){cout<<"A::A()called.";}virtual~A(){cout<<"A::~A()called.";}};classB:publicA
2、{public:B(inti){cout<<"B::B()called.";buf=newchar[i];}virtual~B(){delete[]buf;cout<<"B::~B()called.";}private:char*buf;};voidfun(A*a){cout<<"Mayyousucceed!"<3、d.A::~A()called.(2)#includeusingnamespacestd;classA{public:A(inta,intb):x(a),y(b){cout<<"Aconstructor..."<4、obj;public:B(inta,intb,intc,intd):A(a,b),i(c),j(d),Aobj(1,1){cout<<"Bconstructor..."<5、2,3,4);b.display();b.Add(1,3,5,7);b.display();return0;}(2)程序运行结果:Aconstructor...Aconstructor...Bconstructor...(1,2)(1,1)(3,4)(2,5)(1,1)(8,11)destructorB...destructorA...destructorA...(3)#includeusingnamespacestd;classA{public:A(inta):x(a){cout<<"Aconstructor..."<6、dl;}intf(){return++x;}~A(){cout<<"destructorA..."<7、endl;}~B(){cout<<"destructorB..."<8、(7,8,9);d.f();d.display();return0;}(3)程序运行结果:Ac
3、d.A::~A()called.(2)#includeusingnamespacestd;classA{public:A(inta,intb):x(a),y(b){cout<<"Aconstructor..."<4、obj;public:B(inta,intb,intc,intd):A(a,b),i(c),j(d),Aobj(1,1){cout<<"Bconstructor..."<5、2,3,4);b.display();b.Add(1,3,5,7);b.display();return0;}(2)程序运行结果:Aconstructor...Aconstructor...Bconstructor...(1,2)(1,1)(3,4)(2,5)(1,1)(8,11)destructorB...destructorA...destructorA...(3)#includeusingnamespacestd;classA{public:A(inta):x(a){cout<<"Aconstructor..."<6、dl;}intf(){return++x;}~A(){cout<<"destructorA..."<7、endl;}~B(){cout<<"destructorB..."<8、(7,8,9);d.f();d.display();return0;}(3)程序运行结果:Ac
4、obj;public:B(inta,intb,intc,intd):A(a,b),i(c),j(d),Aobj(1,1){cout<<"Bconstructor..."<5、2,3,4);b.display();b.Add(1,3,5,7);b.display();return0;}(2)程序运行结果:Aconstructor...Aconstructor...Bconstructor...(1,2)(1,1)(3,4)(2,5)(1,1)(8,11)destructorB...destructorA...destructorA...(3)#includeusingnamespacestd;classA{public:A(inta):x(a){cout<<"Aconstructor..."<6、dl;}intf(){return++x;}~A(){cout<<"destructorA..."<7、endl;}~B(){cout<<"destructorB..."<8、(7,8,9);d.f();d.display();return0;}(3)程序运行结果:Ac
5、2,3,4);b.display();b.Add(1,3,5,7);b.display();return0;}(2)程序运行结果:Aconstructor...Aconstructor...Bconstructor...(1,2)(1,1)(3,4)(2,5)(1,1)(8,11)destructorB...destructorA...destructorA...(3)#includeusingnamespacestd;classA{public:A(inta):x(a){cout<<"Aconstructor..."<6、dl;}intf(){return++x;}~A(){cout<<"destructorA..."<7、endl;}~B(){cout<<"destructorB..."<8、(7,8,9);d.f();d.display();return0;}(3)程序运行结果:Ac
6、dl;}intf(){return++x;}~A(){cout<<"destructorA..."<7、endl;}~B(){cout<<"destructorB..."<8、(7,8,9);d.f();d.display();return0;}(3)程序运行结果:Ac
7、endl;}~B(){cout<<"destructorB..."<8、(7,8,9);d.f();d.display();return0;}(3)程序运行结果:Ac
8、(7,8,9);d.f();d.display();return0;}(3)程序运行结果:Ac
此文档下载收益归作者所有