面向对象程序设计实验

2026/4/29 20:05:32

}

cout<<\不及格人数:\

}

实验五 派生与继承—单基派生

5.1 实验目的

1.理解继承的概念;

2.理解公有派生、私有派生和保护派生;

3.理解单基派生类中构造函数和析构函数的执行顺序。

5.2 实验内容 5.2.1程序阅读

1.理解下面的程序并运行,然后回答后面的问题。

#include \

12

class CBase {

public: CBase(int a) :a(a) { } protected: void print() { cout<<\ } private: int a; };

class CDerive : public CBase {

public: void print() { CBase::print(); cout<<\ } private: int b; };

void main() { CDerive d; d.print(); CBase b; b.print(); }

问题一:以上程序有两个错误,试指出来,并改正之。

答:派生类CDerive中没有定义CDerive(),主函数中没有给d,b对象赋值。 #include \

13

class CBase {

public: CBase(int a) :a(a) { } void print() { cout<<\ } private: int a; };

class CDerive : public CBase {

public: CDerive(int a,int c) :CBase(a) { b=c; } void print() { CBase::print(); cout<<\ } private: int b; };

void main() { CDerive d(1,3); d.print(); CBase b(2); b.print();

14

}

2.理解下面的程序并运行,然后回答后面的问题。

#include \

class CBase {

public: CBase(int a) :a(a) { cout<<\ } ~CBase() { cout<<\ } void print() { cout<<\ } protected: int a; };

class CDerive : public CBase {

public: CDerive(int a, int b,int c) :CBase(a),b(b),c(c) { cout<<\ } ~CDerive() { cout<<\ } void print() { CBase::print();

15


面向对象程序设计实验.doc 将本文的Word文档下载到电脑
搜索更多关于: 面向对象程序设计实验 的文档
相关推荐
相关阅读
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 10

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219