C++ 4장 파생 클래스와 상속 연습

5513,

주제 내용:

P140 Example 5.2는 개인 데이터 멤버 age 및 생성자, 멤버 함수 setage 및 show 함수를 포함하는 person 클래스를 정의합니다. 파생 클래스 학생 개인 상속 개인을 정의하고 파생 클래스에는 개인 데이터 멤버 crit 및 생성자, 멤버 함수 setage_cre 및 show 함수가 포함됩니다. 파생 클래스 객체 stu1은 main 함수에 정의되어 있으며 초기값은 19와 166이다. 객체를 사용하여 멤버 함수 setage_cre를 호출합니다. 실제 매개변수는 20과 168입니다. 출력은 stu1 개체의 멤버 값입니다.

입력 및 출력 설명:

출력: 
나이는 20세 
학점은 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,

주제 내용:

 개인 데이터 멤버 i로 기본 클래스 B를 정의하고, 생성자와 소멸자를 정의하고, 호출되도록 출력 문을 포함합니다. i 값을 출력하는 멤버 함수도 있습니다.

개인 데이터 멤버 j로 파생 클래스 D를 정의하고 생성자와 소멸자를 정의하고 출력 문을 포함하여 호출되도록 합니다. j 값을 출력하는 멤버 함수도 있습니다.

main 함수는 초기값이 50과 60인 파생 클래스 객체 obj를 정의합니다. obj 데이터 멤버의 값을 출력합니다.

입력 및 출력 설명:

출력: 
c 베이스 
c 파생 
60 
50 
d 파생 
d 베이스

 

#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

각각 개인 데이터 멤버 x 및 y를 포함하는 기본 클래스 base1 및 base2를 정의합니다. 데이터 멤버의 값을 얻으려면 생성자와 멤버 함수를 모두 정의해야 합니다.

base1을 공개적으로 상속하고 base2를 비공개로 상속하는 파생 클래스를 정의합니다. 데이터 멤버 z를 포함합니다. 생성자와 멤버 함수를 정의합니다.

파생 클래스 객체 obj는 main 함수에 정의되어 있으며 초기 값은 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

데이터 멤버 a와 생성자를 포함하는 가상 기본 클래스 기반을 정의합니다.

가상 기본 클래스 기반을 정의하는 파생 클래스 base1 및 base2에는 각각 데이터 멤버 b, c 및 생성자가 포함됩니다.

base1 및 base2를 정의하는 파생 클래스에는 데이터 멤버 d와 생성자가 포함되어 있습니다.

호출되었는지 확인하려면 생성자에 출력 문이 필요합니다.

파생 객체 obj는 main 함수에 정의되어 있습니다.

입력 및 출력 설명:

출력: 
c base 
c 
base1 c base2 
c 파생

 

#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