C++实验5_2继承与派生(2)

一、实验目的和要求

 1. 掌握多重继承的定义。

2. 掌握多重继承中构造函数与析构函数的调用顺序。

3. 掌握多重继承中虚基类的作用。

二、实验环境(软、硬件及条件)

一台安装有Visual C++ 6.0的计算机

三、实验步骤

1.阅读下面的程序,找出其中的错误并改正,然后分析该程序的运行结果。

class CBase1

{

public:

CBase1(int a)

:a(a)

{

cout<<"base1 structure..."<<endl;

}

~CBase1()

{

cout<<"base1 destructure..."<<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;

}

void print()

{

cout<<"b="<<b<<endl;

}

protected:

int b;

};

class CDerive : public CBase1, public CBase2

{

public:

CDerive()

{

cout<<"derive structure..."<<endl;

}

~CDerive()

{

cout<<"derive destructure..."<<endl;

}

void print()

{

CBase1::print();

CBase2::print();

b1.print();

b2.print();

cout<<"c="<<c<<endl;

}

private:

CBase1 b1;

CBase2 b2;

int c;

};

void main()

{

CDerive d;

d.print();

}

2、阅读下面的程序,找出其中的错误并改正。

#include "iostream.h"

 

class CBase

{

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)

: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;

}

3、试设计一个基类building,用来存储一座楼房的层数、房间数以及它的总平方数。建立派生类house,继承building,并存储卧室与浴室的数量,另外,建立派生类office,继承building,并存储灭火器与电话的数目。设计一主函数来测试以上类的用法。

四、实验中遇到的问题及解决

 

 

 

 

五、实验结果及分析

1.

#include<iostream>

using namespace std;

class CBase1

{

public:

CBase1()

{

cout<<"base1 structure..."<<endl;

}

~CBase1()

{

cout<<"base1 destructure..."<<endl;

}

void print()

{

cout<<"a="<<a<<endl;

}

protected:

int a;

};

class CBase2

{

public:

CBase2()

{

cout<<"base2 structure..."<<endl;

}

~CBase2()

{

cout<<"base2 destructure..."<<endl;

}

void print()

{

cout<<"b="<<b<<endl;

}

protected:

int b;

};

class CDerive : public CBase1, public CBase2

{

public:

CDerive()

{

cout<<"derive structure..."<<endl;

}

~CDerive()

{

cout<<"derive destructure..."<<endl;

}

void print()

{

CBase1::print();

CBase2::print();

b1.print();

b2.print();

cout<<"c="<<c<<endl;

}

private:

CBase1 b1;

CBase2 b2;

int c;

};

void main()

{

CDerive d;

d.print();

}

2.

#include "iostream.h"

class CBase

{

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)

: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.CDerive1::a<<endl;

}

3.

#include<iostream>

using namespace std;

class building

{public:

void gf(int a,int b,double c){

x=a,y=b,z=c;

cout<<"层数为:"<<a<<endl;

cout<<"房间数为:"<<b<<endl;

cout<<"总平方数为:"<<c<<endl;

}

private:

int x,y;double z;

};

class house: public building

{public:

void gf(int d,int e){

m=d,n=e;

cout<<"卧室数量为:"<<d<<endl;

cout<<"浴室数量为:"<<e<<endl;

}

private:

int m,n;

};

class office:public building

{public:

office(int f,int g){

p=f,q=g;

cout<<"灭火器数量为:"<<p<<endl;

cout<<"电话数量为:"<<q<<endl;}

private:

int p,q;

};

void main()

{

house s;

s.building::gf(20,120,900);

s.gf(120,120);

office o(234,120);

}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值