C++(例题集—简单解析-继承)

继承与派生的概念

派生类的声明:

#include<iostream>
using namespace std;
class Base{
    int b;
public:
    void display(){
        cout<<b<<endl;
    }
};
class Derived:public Base{
    double d;
public:
    void show(){
        display();
        cout<<d<<endl;
    }
};

继承方式与访问控制:

1、公有继承方式:

2、保护继承方式:

3、私有继承方式:


6-4-1例题:公有继承——1:

#include<iostream>
using namespace std;
class Base{
private:
    int x;
public:
    double y;
public:
    void setX(int a){
        x=a;
    }
};
class Derived:public Base{
    char z;
public:
    void setD(int a,double b,char c){
        setX(a);
        y=b;
        z=c;
        //x=a;
    }
    void show(){
        cout<<" "<<y<<" "<<z<<" "<<endl;
    }
};
int main(){
    Derived d;
    d.setX(2);
    d.y=6;
    d.setD(1,3,'5');
    d.show();
    return 0;
}

例题6-1 人与学生共有继承关系的访问属性:

#include<iostream>
#include<string.h>
using namespace std;
class Person{
    char name[20];
    char sex;
    int age;
public:
    void setPerson(char nm[],char s,int a){
        strcpy(name,nm);
        sex=s;
        age=a;
    }
    void display(){
        cout<<name<<":"<<sex<<","<<age<<"岁\n";
    }
};
class Student:public Person{
    int number;
    double score;
public:
    void setStudent(int n,double scr){
        number=n;
        score=scr;
    }
    void show(){
        cout<<"学号:"<<number<<",分数:"<<score<<endl;
    }
};
int main()
{
    Student stu;
    stu.setPerson("LiJuan",'f',18);
    stu.display();
    stu.setStudent(20141010,12);
    stu.show();
    return 0;
}

例题6-2         人与学生类同名成员覆盖

#include<iostream>
#include<cstring>
using namespace std;
class Person{
    char name[20];
    char sex;
    int age;
protected:
    void Set(char nm[],char s,int a){
        strcpy(name,nm);
        sex=s;
        age=a;
    }
public:
    void display(){
        cout<<"姓名:"<<name<<",性别:"<<sex<<",年龄:"<<age<<"岁\n";
    }
};
class Student:public Person{
    int number;
    double score;
public:
    void Set(char nm[],char s,int a,int n,double scr){
        Person::Set(nm,s,a);
        number=n;
        score=scr;
    }
    void display(){
        cout<<"学号:"<<number<<endl;
        Person::display();
        cout<<"分数:"<<score<<endl;
    }
};
int main()
{
    Student stu;
    stu.Set("Gaofeng",'m',20,201711023,93);
    stu.Person::display();
    stu.display();
    return 0;
}

保护继承:

#include<iostream>
#include<string.h>
using namespace std;
class Base{
private:
    int x;
protected:
    double y;
public:
    int z;
    void setX(int a){
        x=a;
    }
};
class Derived:protected Base{
    double u;
public:
    void setD(int a,int b,int c,int d){
        setX(a);
        y=b;
        z=c;
        u=d;
    }void show(){
        cout<<" b:"<<y<<" c:"<<z<<" d: "<<u<<endl;
    }
};
int main()
{
      Derived d;
      //d.setX(2);
      //d.z=6;
      d.setD(1,3,5,7);
      d.show();
      return 0;
}

保护继承的访问属性。

#include<iostream>
#include<string.h>
using namespace std;
class Person{
    char name[20];
    char sex;
    int age;
public:
    void setPerson(char nm[],char s,int a){
        strcpy(name,nm);
        sex=a;
        age=a;
    }
    void display(){
        cout<<name<<","<<sex<<','<<age<<"岁\n";
    }
};
class Student :protected Person{
    int number;
    double score;
public:
    void setStudent(int n,double scr){
        number=n;
        score=scr;
    }
    void show(){
        cout<<"学号:"<<number<<",分数"<<score<<endl;
    }
};
int main()
{
     Student stu;
     //stu.setPerson("Lijuan",'f',18);
     //stu.display();
     stu.setStudent(20141010,12);
     stu.show();
     return 0;
}

派生类的构造函数和析构函数

例题6-4    派生类默认构造函数例题

#include<iostream>
using namespace std;
class Base{
    int b;
public:
    Base(){
        cout<<"Base default constructor .\n";
    }
    void show(){
        cout<<"b="<<b<<endl;
    }
};
class Derived:public Base{
    int d;
public:
    void display(){
        show();
        cout<<"d="<<d<<endl;
    }
};
int main()
{
    Derived d;
    d.display();
    return 0;
}

例题    默认构造 函数初始化

#include<iostream>
using namespace std;
class Base{
    int x;
public:
    Base(int a){
        x=a;
        cout<<"Base constructor.\n";
    }void show(){
        cout<<"x="<<x<<endl;
    }
};
class D:public Base{
    int y;
public:
    D(int s,int t):Base(s){
        y=t;
        cout<<"D constructor.\n";
    }
    void display(){
        show();
        cout<<"y="<<y<<endl;
    }
};
int main(){
    D d(1,2);
    d.display();
    return 0;
}

例题6-5     具有构造函数的学生派生类

#include<iostream>
#include<string>
#include<string.h>
using namespace std;
class Person{
    char name[20];
    char sex;
    int age;
public:
    Person(char nm[],char s,int age);

    void display()const;
};
Person::Person(char nm[],char s,int a){
    strcpy(name,nm);
    sex=s;
    age=a;
}
void Person::display()const{
    cout<<"姓名:"<<name<<",姓名: "<<sex<<",年龄: "<<age<<"岁\n";
}
class Student:public Person{
    string number;
    double score;
public:
    Student(char nm[],char s,int a,string num,double sco):Person(nm,s,a){
        number=num;
        score=sco;
    }void display()const{
        cout<<"学号:"<<number<<endl;
        Person::display();
        cout<<"分数: "<<score<<endl;
    }
};
int main(){
    Student stu("LiJuan",'f',18,"20171101588",98);
    stu.display();
    return 0;
}

继承调用基类构造函数

#include<iostream>
using namespace std;
class Base{
    int x;
public:
    Base(){
        x=0;
        cout<<"Base constructor.\n";
    }
    void show(){
        cout<<"X="<<x<<endl;
    }
};
class D:public Base{
    int y;
public:
    D(int t):y(t){
        cout<<"D constructor.\n";
    }void display(){
        show();
        cout<<"y="<<y<<endl;
    }
};
int main(){
    D d(1);
    d.display();
    return 0;
}

多级继承       初始化子对象参数表

#include<iostream>
using namespace std;
class Base{
    int x;
public:
    Base(int a=0){
        x=a;
        cout<<"Base Constructor.\n";
    }
};
class Myclass{
    float y;
public:
    Myclass(float b=0.0){
        y=b;
        cout<<"Myclass constructor.\n";
    }
};
class D:public Base{
    double z;
    Myclass obj;
public:
    D(int u,float v,double w):Base(u),z(w),obj(v){
        cout<<"D constructor.\n";
    }
};
int main(){
    D d(1,2.3,4);
    return 0;
}

多级继承析构函数顺序

#include<iostream>
using namespace std;
class Base{
public:
    Base(){
        cout<<"Base constructor.\n";
    }
    ~Base(){
        cout<<"Base destructor.\n";
    }
};
class D:public Base{
public:
    D (){
        cout<<"D constructor.\n";
    }
    ~D(){
        cout<<"D destructor.\n";
    }
};
int main(){
    D d;
    return 0;
}

派生类对象替代基类对象

#include<iostream>
using namespace std;
class Base{
    public:
        void show(){
            cout<<"showing base.\n";
        }
};
class D :public Base{
public:
    void show(){
        cout<<"showing D .\n";
    }
};
int main()
{
    Base base;
    D d;
    Base *p=&d;
    p->show();
    Base &r=d;
    r.show();
    base=d;
    base.show();
    return 0;
}

多继承示例

#include<iostream>
#include<string.h>
using namespace std;
class Staff{
protected:
    char level[20];
public:
    Staff(char lvl[]){
        strcpy(level,lvl);
    }
    void show(){
        cout<<"级别:"<<level<<endl;
    }
};
class Teacher{
protected:
    char title[20];
public:
    Teacher(char ttl[]){
        strcpy(title,ttl);
    }
    void show(){
        cout<<"职称:"<<title<<endl;
    }
};
class Staffteacher:public Staff,public Teacher{
    char name[20];
public:
    Staffteacher(char lv[],char tl[],char nm[]):Staff(lv),Teacher(tl){
        strcpy(name,nm);
    }
    void display(){
        cout<<name<<endl;
    }
};
int main(){
    Staffteacher st("正科","讲师","李俊");
    st.display();
    st.Staff::show();
    st.Teacher::show();
    return 0;
}

静态关联

#include<iostream>
using namespace std;
class Base{
protected:
    int b;
public:
    Base (int s=0):b(s){}
    void show(){
        cout<<"B="<<b<<endl;
    }
};
class D :public Base{
    int d;
public:
    D (int u,int v):Base(u),d(v){}
    void show(){
        cout<<"B="<<b<<"  D="<<d<<endl;
    }
};
int main()
{
        Base base;
        D d(2,8);
        Base * p=&base;
        p->show();
        d.show();
        p=&d;
        p->show();
        base=d;
        cout<<"派生类对象赋值给基类对象后:\n";
        base.show();
        return 0;
}

静态关联2

#include<iostream>
using namespace std;
class ball{
public:
     void exe(){
        cout<<"Playing ball.\n";
     }
};
class basketball:public ball{
public:
    void exe(){
        cout<<"Playing basketball.\n";
    }
};
class football:public ball{
public:
    void exe(){
        cout<<"Playing football.\n";
    }
};
void fun(ball &rplayer){
    rplayer.exe();
}
int main(){
    ball player;
    basketball bplayer;
    football fplayer;
    fun(player);
    fun(bplayer);
    fun(fplayer);
    return 0;
}

虚基类

#include<iostream>
using namespace std;
class Base{
protected:
    int a;
public:
    Base(int x):a(x){}
};
class Base1: virtual public Base{
protected:
    int a1;
public:
    Base1(int x,int x1):Base(x),a1(x1){}
};
class Base2:virtual public Base{
protected:
    int a2;
public:
    Base2(int x0,int x2):Base(x0),a2(x2){}
};
class D:public Base1,public Base2{
    int d;
public:
    D(int x0,int x1,int x2,int x):Base(x0),Base1(3,x1),Base2(4,x2),d(x){}
    void show(){
        cout<<"a = "<<a<<endl;
    }
};
int main(){
    D d(1,2,3,4);
    d.show();
    return 0;
}



















猜你喜欢

转载自blog.csdn.net/Z_sea/article/details/80960662
今日推荐