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);
}

猜你喜欢

转载自blog.csdn.net/qq_56350439/article/details/124434609