实验9-继承(二)

合集下载

实验九继承和多态

实验九继承和多态

实验九继承和多态
实验目的:
1.会实现继承机制
2.会在程序中使用多态提高程序的可扩展性
知识准备:
继承和多态
实验内容及要求:
教学示例及教材实例
1.编写一个继承的示例
2.编写一个多态的示例
实践任务
1.创建一个图形类,定义一个draw的方法,创建三个子类分别是圆形,矩形,三
角形,使用多态的思想实现父类的引用指向子类的实例,同时调用draw的方法
但是画出的图形不同。

2.使用继承实现如下功能
案例名称:交通工具的类继承
要点:类继承的实现
父类:Vehicles类,属性交通工具的名称,颜色,座位
子类:car类,ship类,plane类
实现思路:Car类对象分别调用vehicle类(父类)方法showDetail()和本类
方法show()输出汽车的属性值。

拓展实验
1.在项目中学会使用继承和多态,参考课外资料学习
实验总结:。

继承的应用实验报告

继承的应用实验报告

一、实验目的1. 理解继承的概念和作用。

2. 掌握Java中继承的实现方法。

3. 学习如何利用继承提高代码复用性。

4. 通过实验加深对继承的理解和应用。

二、实验环境1. 操作系统:Windows 102. 编译器:Eclipse IDE3. 编程语言:Java三、实验内容1. 创建一个基类:Animal,包含属性name和方法eat()。

2. 创建两个派生类:Dog和Cat,分别继承自Animal类,并添加各自特有的属性和方法。

3. 创建一个主类:TestInheritance,在主函数中创建Animal、Dog和Cat的对象,并调用它们的方法。

四、实验步骤1. 创建基类Animal:```javapublic class Animal {private String name;public Animal(String name) { = name;}public void eat() {System.out.println(name + " is eating."); }}```2. 创建派生类Dog:```javapublic class Dog extends Animal {private String breed;public Dog(String name, String breed) {super(name);this.breed = breed;}public void bark() {System.out.println(name + " is barking."); }}```3. 创建派生类Cat:```javapublic class Cat extends Animal {private String color;public Cat(String name, String color) {super(name);this.color = color;}public void meow() {System.out.println(name + " is meowing."); }}```4. 创建主类TestInheritance:```javapublic class TestInheritance {public static void main(String[] args) {Animal animal = new Animal("Animal");Dog dog = new Dog("Buddy", "Labrador");Cat cat = new Cat("Kitty", "Black");animal.eat();dog.eat();dog.bark();cat.eat();cat.meow();}}```五、实验结果与分析1. 运行主类TestInheritance,观察控制台输出:```Animal is eating.Buddy is eating.Buddy is barking.Kitty is eating.Kitty is meowing.```2. 分析结果:(1)Animal类作为基类,包含一个私有属性name和一个方法eat()。

实验九 继承与派生

实验九  继承与派生

中国矿业大学计算机学院《高级语言程序设计》实验九继承与派生一、实验目的1.掌握继承的含义,掌握派生类的定义方法和实现;2.掌握公有继承、保护继承和私有继承下基类成员对派生类成员和派生类对象的可见性,能正确地访问继承层次中的各种类成员;3.掌握保护成员在继承中的作用,能够在适当的时候选择使用保护成员以便派生类成员可以访问基类的部分非公开的成员;二、实验要求1.写出程序,并调试程序,要给出测试数据和实验结果。

2.整理上机步骤,总结经验和体会。

3.完成实验报告和上交程序三、实验内容1.将例11.2的程序片断补充和改写成一个完整、正确的程序,用私有继承方式。

在程序中应包括输入数据的函数,在程序运行时输入num, name, sex, age, addr的值,程序应输出以上5个数据的值。

2.编写一个程序计算出圆和圆柱体的表面积和体积。

要求:(1)定义一个点(point)类,包含数据成员x,y(坐标点),以它为基类,派生出一个circle类(圆类),增加数据成员r(半径),再以circle作为直接基类,派生出一个cylinder(圆柱体)类,再增加数据成员h(高)。

设计类中数据成员的访问属性。

(2)定义基类的派生类圆、圆柱都含有求表面积和体积的成员函数和输出函数。

(3)定义主函数,求圆、圆柱的面积和体积。

3.开发一个名为Vehicle的类层次。

创建两个类:Taxi和Truck,它们均以public模式从类Vehicle继承而来。

Taxi类应包含一个数据成员,以表明其是否正在载客。

Truck类也应该包含一个数据成员,以表明其是否正在载货。

添加必要的成员函数,以操作和访问类的数据。

编写一个测试程序,将一个Truck对象和一个Taxi对象答应到屏幕上(使用重载的流插入运算符)。

4.编写一个程序用于院校的人事管理,要求能够输入、显示各类人员的信息。

假设院校中有四类人员:学生、教师、员工和在职进修教师,他们都有姓名、性别、联系电话等共同信息,学生还有年级、专业课程和各课成绩的信息,员工还有所在部门、工资的信息,教师还有所在部门、工资和所授课程的信息,在职进修教师兼有教师和学生两类信息。

最新实验二继承机制实验报告

最新实验二继承机制实验报告

最新实验二继承机制实验报告实验目的:本实验旨在探究二继承机制在面向对象编程中的应用和实现,通过具体的编程实践,理解多重继承的概念,掌握如何在现代编程语言中实现二继承,并分析其优缺点以及可能遇到的问题。

实验环境:- 编程语言:Python 3.8- 开发环境:PyCharm 2021- 操作系统:Windows 10实验内容:1. 设计一个基本的类层次结构,包含两个父类和一个子类,子类需要从两个父类继承属性和方法。

2. 实现二继承,确保子类能够访问和重写父类的属性和方法。

3. 通过实例化子类,测试继承后的属性和方法的正确性。

4. 分析二继承在代码重用和系统设计中的优势和潜在的问题。

实验步骤:1. 定义两个父类`ClassA`和`ClassB`,分别具有不同的属性和方法。

2. 创建一个子类`ClassC`,继承自`ClassA`和`ClassB`。

3. 在`ClassC`中重写父类的方法,以展示多态性。

4. 编写测试代码,创建`ClassC`的实例,并调用其继承和重写的方法。

5. 记录实验结果,并对比预期和实际输出。

实验结果:- `ClassC`成功继承了`ClassA`和`ClassB`的属性和方法。

- 重写的方法在`ClassC`的实例中按预期工作,展示了多态性。

- 测试代码中所有的调用均返回了正确的结果。

实验分析:二继承机制允许子类继承多个父类的属性和方法,这在某些情况下可以提高代码的复用性,使得类的设计更加灵活。

然而,它也可能导致一些问题,如菱形继承问题,其中两个父类继承自一个共同的基类,可能会导致歧义和初始化顺序的复杂性。

此外,多重继承也可能使得类的继承关系变得复杂,难以理解和维护。

结论:通过本次实验,我们成功实现了二继承机制,并通过实际测试验证了其功能。

二继承提供了一种有效的方式来组合多个类的属性和行为,但开发者在使用时需要注意避免继承结构过于复杂,以及可能出现的菱形继承问题。

在设计类时,应当权衡二继承带来的便利性和可能引入的复杂性。

实验9 类的继承和派生

实验9 类的继承和派生

实验9 类的继承和派生一、实验目的1、理解继承的含义,掌握派生类的定义方法和实现;2、理解公有继承下基类成员对派生类成员和派生类对象的可见性,能正确地访问继承层次中的各种类成员;3、理解保护成员在继承中的作用,能够在适当的时候选择使用保护成员以便派生类成员可以访问基类的部分非公开的成员;4、条件编译的运用。

多文件结构的进一步使用。

二、实验内容1、编写一个程序计算出球、圆柱和圆锥的表面积和体积。

要求:(1)定义一个基类,至少含有一个数据成员半径,并设为保护成员;(2)定义基类的派生类球、圆柱、圆锥,都含有求表面积和体积的成员函数和输出函数;(3)编写主函数,求球、圆柱、圆锥的表面积和体积。

2、编写一个学生和教师数据输入和显示程序。

其中,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。

要求:(1)将编号、姓名输入和显示设计成一个类person;(2)设计类person的派生类:学生类student和教师类teacher;(3)各个类的声明放在相应的头文件中(*.h),类的实现放在相应的实现文件中(*.cpp):person.h,person.cpp,student.h,student.cpp,teacher.h,teacher.cpp;(4)编写一个主文件(SY4_2.cpp),在该文件中分别定义student、teacher的对象,完成相应功能。

三、实验程序及结果1.#include <iostream>#include <math.h>using namespace std;const double pi=3.1415926;class circle //基类圆{public:circle(double rad){r=rad;}protected:double r;};class ball:public circle //球类{ball(double rad):circle(rad){}void com_surface(){surface=4*pi*r*r;}void com_volume(){volume=4*pi*r*r*r/3;}double get_surface(){return surface;}double get_volume(){return volume;}private:double surface;double volume;};class taper:public circle // 圆锥类{public:taper(double rad,double length):circle(rad){l=length;}void com_surface(){surface=pi*l*r+pi*r*r;}void com_volume(){volume=pi*r*r*sqrt(l*l-r*r)/3;} double get_surface(){return surface;}double get_volume(){return volume;}private:double l; //圆锥侧长double surface;double volume;};class column:public circle //圆柱类{public:column(double rad,double high):circle(rad){h=high;}void com_surface(){surface=pi*r*r*2+2*pi*r*h;} void com_volume(){volume=pi*r*r*h;}double get_surface(){return surface;}double get_volume(){return volume;}private:double h; //圆柱高double surface;double volume;};void main(){_surface();_volume();cout<<"该球的表面积是:"<<b.get_surface()<<endl; cout<<"该球的体积是:"<<b.get_volume()<<endl; cout<<"------------------------------------"<<endl; taper t(3,5);_surface();_volume();cout<<"该圆锥的表面积是:"<<t.get_surface()<<endl; cout<<"该圆锥的体积是:"<<t.get_volume()<<endl; cout<<"------------------------------------"<<endl; column c(3,5);_surface();_volume();cout<<"该圆柱的表面积是:"<<c.get_surface()<<endl; cout<<"该圆柱的体积是:"<<c.get_volume()<<endl; }2、//person.h#include <iostream.h>#include <iomanip.h>const int l=80;class person{ public:void input(){ cout<<"\n input name: ";cin>>name;cout<<"\n certificate no: ";cin>>id;}void print(){ cout<<"\n certificate no: "<<id; }void printname(){ cout<<setw(8)<<name; }private:char name[l],id[l];};//person.cpp#include <iostream.h>#include <iomanip.h>#include<person.h>int main(){person p1;p1.input();p1.print();p1.printname();return 0;}//teacher.h#include <iostream.h>#include <iomanip.h>#include<person.h>class teacher:public person{ public:void input(){ person::input();cout<<" input degree: ";cin>>degree;cout<<" input department: ";cin>>dep;}void print(){ person::print();cout<<"\n degree: "<<"\t"<<degree;cout<<"\n department: "<<"\t"<<dep;}void printname(){ person::printname(); }char degree[l],dep[l];};//teacher.cpp#include <iostream.h>#include <iomanip.h>#include<person.h>#include<teacher.h>int main(){teacher t1;t1.input();t1.print();t1.printname();return 0;}//student.h#include <iostream.h>#include <iomanip.h>#include<person.h>class stud{ public:void input(){ cout<<" input address: ";cin>>addr;cout<<" input telphone no: ";cin>>tel;}void print(){ cout<<"\n address: "<<"\t"<<addr;cout<<"\n telphone no: "<<"\t"<<tel;}private:char addr[l];char tel[l];};class student:private person{ public:void input(){ person::input();cout<<" input years old: ";cin>>old;cout<<" input score no: ";cin>>sno;void print(){ person::print();cout<<"\n tears old: "<<"\t"<<old;cout<<"\n score no: "<<"\t"<<sno;}void printname(){ person::printname(); }private:int old;unsigned long sno;};class score:private student,private stud{ public:void input(){ student::input();stud::input();cout<<"input math score: ";cin>>math;cout<<"input english score: ";cin>>eng;}void print(){ student::print();stud::print();cout<<"\n math score: "<<"\t"<<math;cout<<"\n english svore: "<<"\t"<<eng;cout<<"\n average score: "<<"\t"<<float(math+eng)/2;}void printname(){ student::printname(); }private:int math;int eng;};//student.cpp#include <iostream.h>#include <iomanip.h>#include<person.h>#include<student.h>int main(){stud s1;s1.input();s1.print();student st1;st1.input();st1.print();st1.printname();score sc1;sc1.input();sc1.print();sc1.printname();return 0;}//SY4-2.cpp#include <iostream.h>#include <iomanip.h>#include<person.h>#include<teacher.h>#include<student.h>void main(){ score c1;teacher t1;cout<<"input data for score 1: ";c1.input();cout<<"input data for teacher 1: ";t1.input();cout<<"\n data on student ";c1.printname();c1.print();cout<<"\n data on teacher ";t1.printname();t1.print();}。

实验九继承与派生

实验九继承与派生

实验九:继承与派生一、实验目的与要求(1)了解Visual C++ 6.0 的特点(2)熟悉Visual C++6.0 的开发环境(3)学习用Visual C++6.0编写标准的C++控制台程序(4)掌握类的继承与派生的应用。

二、实验类型设计型实验。

三、实验原理及说明1、声明一个基类Animal,有私有整形成员变量age,构造其派生类dog,在其成员函数SetAge (int n)中直接给age赋值,看看会有什么问题,把age改为公有成员变量,还会有问题吗?编程试试看。

2、完成P251 7-10题编程,上机实现。

3、完成P251 7-9题编程,上机实现。

五、实验内容和步骤1、启动VC++ 6.0 开发环境2、创建一个C++源程序文件3 建立并运行可执行文件4 关闭工作空间六、实验数据处理与分析七、注意事项八、实验报告要求完成程序验证,正确书写试验报告。

九、预习与思考题类的继承与派生。

1.声明一个基类Animal,有私有整形成员变量age,构造其派生类dog,在其成员函数SetAge (int n)中直接给age赋值,看看会有什么问题,把age改为公有成员变量,还会有问题吗?编程试试看#include<iostream>using namespace std;class Animal{public:int age;};class dog : public Animal{public:void SetAge(int n){age=n;cout<<"age="<<age<<endl;}};void main(){dog a;a.SetAge(3);}2.完成P251 7-10题编程,上机实现#include<iostream>using namespace std;class object{int weight;public:object(int n){cout<<"调用object的构造函数"<<endl;weight=n;cout<<"weight="<<weight<<endl;}~object(){cout<<"调用object的析构函数"<<endl;}};class box : public object{int height,width;public:box(int a,int b,int c):object(a){cout<<"调用box的构造函数"<<endl;height=b;width=c;cout<<"height="<<height<<" width="<<width<<endl;}~box(){cout<<"调用box的析构函数"<<endl;}};void main(){box s(1,2,3);}3、完成P251 7-9题编程,上机实现。

继承派实验报告

继承派实验报告

一、实验目的1. 理解继承的概念及其在面向对象编程中的重要性。

2. 掌握不同继承方式(公有继承、私有继承、保护继承)下的基类成员在派生类中的访问权限。

3. 熟悉构造函数和析构函数在继承过程中的调用顺序和时机。

4. 培养动手实践能力,提高编程水平。

二、实验内容1. 创建一个基类`Base`,包含三个成员变量:`int`类型的`a`,`float`类型的`b`和`char`类型的`c`。

2. 创建三个派生类`DerivedPublic`,`DerivedPrivate`和`DerivedProtected`,分别采用公有继承、私有继承和保护继承的方式继承自基类`Base`。

3. 在每个派生类中添加一个构造函数,用于初始化基类成员和派生类特有的成员变量。

4. 在每个派生类中添加一个成员函数`display`,用于打印基类成员和派生类成员变量的值。

5. 在主函数中创建基类对象和派生类对象,并调用`display`函数打印成员变量的值。

三、实验步骤1. 创建基类`Base`,包含三个成员变量和相应的构造函数。

```cppclass Base {public:int a;float b;char c;Base(int a, float b, char c) : a(a), b(b), c(c) {}};```2. 创建三个派生类,分别采用公有继承、私有继承和保护继承的方式继承自基类`Base`。

```cppclass DerivedPublic : public Base {public:int d;DerivedPublic(int a, float b, char c, int d) : Base(a, b, c), d(d) {}void display() {cout << "DerivedPublic: a = " << a << ", b = " << b << ", c = " << c << ", d = " << d << endl;}};class DerivedPrivate : private Base {public:int d;DerivedPrivate(int a, float b, char c, int d) : Base(a, b, c), d(d) {}void display() {cout << "DerivedPrivate: a = " << a << ", b = " << b << ", c = " << c << ", d = " << d << endl;}};class DerivedProtected : protected Base {public:int d;DerivedProtected(int a, float b, char c, int d) : Base(a, b, c), d(d) {}void display() {cout << "DerivedProtected: a = " << a << ", b = " << b << ", c = " << c << ", d = " << d << endl;}};```3. 在主函数中创建基类对象和派生类对象,并调用`display`函数打印成员变量的值。

C++实验指导 实验08 继承

C++实验指导  实验08 继承

实验八、继承(2学时)1.实习目的及要求:1)掌握继承的实现方法;2)继承中常见问题的处理方法;2.预习:预习派生类的定义和使用、创建派生类对象时构造函数的调用次序、多重继承中的冲突、及虚基类。

3.实验内容:3.1分析下面的程序,指出程序运行的结果:请各位同学用你的学号命名你的作业文件夹,将各题的答案放入(用word文挡或写字板录入均可,要求为每一题建立一个文档),并发到教师机相应目录下(通常是教师机的最后一个硬盘)的作业文件夹中。

具体位置按老师的指示操作。

1) 分析下面的程序,指出程序运行的结果:#include <iostream.h>class CBase{public:void fn1();};void CBase::fn1(){cout<<"调用基类类的函数fn1()\n";}class CDerived:public CBase{public:void fn1();};void CDerived::fn1(){cout<<"调用派生类的函数fn1()\n";}void main(){CDerived d1;CBase *pb=&d1;CBase &pd=d1;d1.fn1();pb->fn1();pd.fn1();答案:/*调用派生类的函数fn1()调用基类类的函数fn1()调用基类类的函数fn1()Press any key to continue*/2 ) 分析下面的程序,指出程序运行的结果:#include<iostream.h>class CBase1{int x ;public:CBase1( ){x=0 ; cout<<"调用构造函数CBase1( )!\n";}CBase1( int a){x=1;cout<<"调用构造函数CBase1( int )!\n";}~CBase1( ){cout<<"调用析构函数~CBase1( )!\n";} };class CBase2{int y;public:CBase2( ){y=0 ;cout<<"调用构造函数CBase2( )!\n";} CBase2(int a){y=a ;cout<<"调用构造函数CBase2(int )!\n";}~CBase2(){cout<<"调用析造函数~CBase2( )!\n";} };class A{ int x;public:A () {x=0 ; cout<<"调用构造函数A( )!\n";}A(int a){x=a;cout<<"调用构造函数A(int )!\n";}~A(){cout<<"调用析构函数~A()!\n";}};class CDerived:public CBase1, virtual public CBase2 {A a;public:CDerived(){cout<<"调用构造函数CDerived( )!\n";}CDerived(int x,int y ,int z):a(x),CBase1(y),CBase2(z) {cout<<"调用构造函数CDerived(int,int )!\n";}~CDerived(){cout<<"调用析造函数~CDerived( )!\n";} };void main(){CDerived*x=new CDerived;CDerived y(2,3,4);delete x;cout<<"main()函数结束!\n";}答案:/*运行结果:调用构造函数CBase2( )!调用构造函数CBase1( )!调用构造函数A( )!调用构造函数CDerived( )!调用构造函数CBase2(int )!调用构造函数CBase1( int )!调用构造函数A(int )!调用构造函数CDerived(int,int )!调用析造函数~CDerived( )!调用析构函数~A()!调用析构函数~CBase1( )!调用析造函数~CBase2( )!main()函数结束!调用析造函数~CDerived( )!调用析构函数~A()!调用析构函数~CBase1( )!调用析造函数~CBase2( )!Press any key to continue*/3.2 编写并调试程序:将各题的源程序(即。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
}
};
class CDerive2 : virtual public CBase
{
public:
CDerive2(int a)
:CBase(a)
{
}
};
class CDerive : public CDerive1,public CDerive2
{
public:
CDerive(int a,int b)
#include<iostream>
using namespace std;
class CBase
{
public:
CBase(int a)
:a(a)
{
}
int a;
};
class CDerive1 : virtual public CBase
{
public:
CDerive1(int a)
:CBase(a)
CBase2 b2;
int c;
};
int main()
{
CDerive d(1,2,3,4,6);
d.print();
}
从运行结果可以看出:先按定义顺序调用基类构造函数,在按定义顺序调用对象成员的构造函数,最后调用派生类的构造函数。析构函数的调用顺序与构造函数相反。
试验二:引起错误的原因是在派生类中有公共基类成员的多份拷贝,派生类对象调用时引起二义性。解决方法:将公共基类设定为虚基类。修改代码如下:
{
public:
CBase1(int a)
:a(a)
{
cout<<"base1 structure..."<<endl;
}
~CBase1()
{
cout<<"base1 destructure..."<<endl;
}
void print()
{
cout<<"a="<<a<<endl;
}
protected:
内蒙古科技大学信息工程学院计算机系
《面向对象程序设计》实验报告
姓名
张国强
学号
1667159127
班级
软件一班
实验日期
第16周(星期五)06月16日第四节
项目号、实验名称
实验9继承(二)




(任课



供)
一、实验目的
1.理解多重派生的定义;
2.理解多重派生中构造函数与析构函数的调用顺序;
3.理解多重派生中虚拟基类的作用;
2、实验报告电子版应该在实验后一周内提交到教务处网站的网络综合平台对应课程下。
3、任课教师要求在收到实验报告的一周内进行批阅,并给出成绩及评语。
4、实验报告电子版由教务处网站服务器保存。
5、表格宽度可以根据实际情况伸缩。
:CDerive1(a),CDerive2(b),CBase(b)
{
}
};
int main()
{
CDerive d(1,2);
cout<<d.a<<endl;
}




(由学


写)
老师问第一个试验:错在哪?
回答:在派生类中,构造函数对基类数据成员没初始化,即没有对基类构造函数完成调用。
说明:
1、每个实验项目填写一份实验报告,电子版命名方式为:学号姓名项目号.doc。例如:1167111182张三3.doc表示张三做的第3个项目的实验报告。
}
protected:
int b;
};
class CDerive : public CBase1, public CBase2
{
public:
CDerive(int a,int b,int i,int d,int e):CBase1(a),CBase2(b),b1(i),b2(d)
{
c=e;
cout<<"derive structure..."<<endl;
CBase2 b2;
int c;
};
void main()
{
CDerive d;
d.print();
}
问题一:改正以上程序中存在的错误,并分析该程序的输出结果。
2.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。
#include "iostream.h"
class CBase
{
}
void print()
{
cout<<"b="<<b<<endl;
}
protected:
int b;
};
class CDerive : public CBase1, public CBase2
{
public:
CDerive()
{
cout<<"derive structure..."<<endl;
}
:CBase(a)
{
}
};
class CDerive : public CDerive1,public CDerive2
{
public:
CDerive(int a,int b)
:CDerive1(a),CDerive2(b)
{
}
};
void main()
{
CDerive d(1,2);
cout<<d.a<<endl;
}
void print()
{
cout<<"a="<<a<<endl;
}
protected:
int a;
};
class CBase2
{
public:
CBase2(int b)
:b(b)
{
cout<<"base2 structure..."<<endl;
}
~CBase2()
{
cout<<"base2 destructure..."<<endl;
int a;
};
class CBase2
{
public:
CBase2(int b)
:b(b)
{
cout<<"base2 structure..."<<endl;
}
~CBase2()
{
cout<<"base2 destructure..."<<endl;
}
void print()
{
cout<<"b="<<b<<endl;
二、实验内容
2.1练习(一):
1.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。
class CBase1
{
public:
CBase1(int a)
:a(a)
{
cout<<"base1 structure..."<<endl;
}
~CBase1()
{
cout<<"base1 destructure..."<<endl;
}
问题一:在不改变原有程序意图的前提下,改正以上程序,并使程序正确输出。


:错误在于在派生类中,构造函数对基类数据成员没初始化,即没有对基类构造函数完成调用。修改后代码如下:
#include<iostream>
using namespace std;
class CBase1
public:
CBase(int a)
:a(a)
{
}
int a;
};
class CDerive1 : public CBase
{
public:
CDerive1(int a)
:CBase(a)
{
}
};
class CDerive2 : public CBase
{
public:
CDerive2(int a)
}
~CDerive()
{
cout<<"derive destructure..."<<endl;
}
void print()
{
CBase1::print();
CBase2::print();
b1.print();
b2.print();
cout<<"c="<<c<<endl;
}
private:
CBase1 b1;
~CDerive()
{
cout<<"derive destructure..."<<endl;
}
void print()
{
CBase1::print();
CBase2::print();
b1.print();
b2.print();
cout<<"c="<<c<<endl;
相关文档
最新文档