C++第4章 派生类与继承练习

5513、

題目內容:

P140 例5.2 定义person类,其中含有私有数据成员age和构造函数、成员函数setage以及show函数。定义派生类student私有继承person,派生类中含有私有数据成员cridit和构造函数、成员函数setage_cre以及show函数。主函数中定义派生类对象stu1,初值为19和166。利用对象调用成员函数setage_cre,实参为20和168。输出为stu1对象的成员值。

输入输出说明:

输出:
age is 20
credit is 168
#include<iostream>
using namespace std;
class person{
protected:
    int age;
public:
    person(int age){
        this->age=age;
    }
    void setage(int age){
        this->age=age;
    }
    void show(){
        cout<<"age is "<<age<<endl;
    }
};
class student :public person{
protected:
    int credit;
public:
    student(int credit,int age1)  : person(age1){
        this->credit=credit;
    }
    void setage_cre(int credit,int age){
        this->credit=credit;
        this->age=age;
    }
    void show(){
        person::show();
        cout<<"credit is "<<credit<<endl;
    }
};
int main(){
    student stu1(166,19);
    stu1.setage_cre(168,20);
    stu1.show();
}

5518、

題目內容:

 定义基类B含有私有数据成员i,定义构造函数和析构函数,都要包含输出语句以保证知道其被调用。还有一个成员函数输出i的值。

定义派生类D含有私有数据成员j,定义构造函数和析构函数,都要包含输出语句以保证知道其被调用。还有一个成员函数输出j的值。

主函数定义派生类对象obj,初值为50、60。输出obj数据成员的值。

输入输出说明:

输出:
c base
c derived 
60
50
d derived
d base

 

#include<iostream>
using namespace std;
class B{
protected:
    int i;
public:
    B(int i){
        this->i=i;
        cout<<"c base"<<endl;
    }
    ~B(){
        cout<<"d base"<<endl;
    }
    void output(){
        cout<<i<<endl;
    }
};
class D : public B{
protected:
    int j;
public: 
    D(int i,int j):B(i){
        this->j=j;
        cout<<"c derived"<<endl;
    }
    ~D(){
        cout<<"d derived"<<endl;
    }
    void output(){
        B::output();
        cout<<j<<endl;
    }
};
int main(){
    D obj(60,50);
    obj.output();
}

5519、

題目內容:

P158 例5.11

定义基类base1和base2,分别含有私有数据成员x、y。都要定义构造函数和成员函数以取得数据成员的值。

定义派生类derived,公有继承base1,私有继承base2。含有数据成员z。定义构造函数,以及成员函数。

主函数中定义派生类对象obj,初值为1、3、5。实现数据成员的输出。

输入输出说明:

输出:
x=1
y=3
z=5

 

#include<iostream>
using namespace std;
class base1{
protected:
    int x;
public:
    base1(int x){
        this->x=x;
    }
    void output(){
        cout<<"x="<<x<<endl;
    }
    
};
class base2{
protected:
    int y;
public:
    base2(int y){
        this->y=y;
    }
    void output(){
        cout<<"y="<<y<<endl;
    }
    
};
class derived :public base1,private base2{
protected:
    int z;
public:
    derived(int x,int y,int z):base1(x),base2(y){
        this->z=z;
    }
    void output(){
        base1::output();
        base2::output();
        cout<<"z="<<z<<endl;
    }
};
int main(){
    derived obj(1,3,5);
    obj.output();
}

5520、

題目內容:

P165 例5.15

定义虚基类base,含有数据成员a以及构造函数。

定义虚基类base的派生类base1和base2分别含有数据成员b、c以及构造函数。

定义base1和base2的派生类derived含有数据成员d以及构造函数。

要求构造函数中都要有输出语句,以知道其是否被调用。

主函数中定义derived的对象obj。

输入输出说明:

输出:
c base
c base1
c base2
c derive

 

#include<iostream>
using namespace std;
class base{
protected:
    int a;
public:
    base(int a){
        this->a=a;
        cout<<"c base"<<endl;
    }
    base(){
        cout<<"c base"<<endl;
    }
};
class base1:virtual public base{
protected:
    int b;
public:
    base1(int a,int b):base(a){
        this->b=b;
        cout<<"c base1"<<endl;
    }
};
class base2:virtual public base{
protected:
    int c;
public:
    base2(int a,int c):base(a){
        this->c=c;
        cout<<"c base2"<<endl;
    }
};
class derived :public base1,public base2{
protected:
    int d;
public:
    derived(int a1,int a2,int b,int c,int d):base1(a1,b),base2(a2,c){
        this->a=a;
        this->d=d;
        cout<<"c derived"<<endl;
    }
};
int main(){
    derived obj(1,2,3,4,5);
}

4 MFC实用技术 93 4.1 MFC常用宏应用 94 0188 获取32位整数的低字节和高字节数据 94 0189 将两个16位数组合为一个32位数 94 4.2 MFC常用函数 94 0190 MFC常用调试函数 94 0191 判断某个句柄是否关联一个窗口 95 0192 MFC应用程序信息和管理函数 95 0193 Internet URL解析全局函数 95 4.3 MFC框架技术 96 0194 在的定义时使其具有运行时型识别的功能 96 0195 运行时判断某个对象是否是指定的型 96 0196 禁止文档/视图应用程序运行时显示视图选择窗口 96 0197 多个窗口消息共享同一个消息处理函数 98 0198 遍历对话框中的子控件 99 0199 在程序中捕捉CException及其派生类的异常 100 0200 扩展消息映射宏 100 0201 THIS_FILE的含义 100 0202 为静态文本控件命名 100 0203 在基于对话框的应用程序中添加文档\视图的支持 101 0204 解析浮动状态下工具栏的父窗口 101 4.4 MFC编程技术 101 0205 根据位图资源ID获取位图大小 101 0206 将某个控件对象关联到对话框中的控件资源 102 0207 将一个全局函数指针关联到对话框的某个方法 102 0208 修改应用程序的图标 102 0209 使用安全数组 103 0210 将子窗口的客户区域映射到父窗口中 103 0211 判断两个时间段的差距 103 0212 重新设置工程名称 103 0213 为dll文件生成lib文件 104 0214 如何将一个工程中的部分资源加到另一个工程中 104 0215 根据句柄获得窗口对象的方法 104 0216 如何共享MSDN 104 0217 从完整的文件名中去除路径 104 0218 从复合字符串中解析子串 105 0219 如何获得应用程序的完整路径 105 0220 修改对话框图标的几种方法 105 0221 将多个具有不同参数的函数赋值为同一个函数指针 105
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李小于

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值