C++day6(菱形继承、虚继承、多态、抽象类、模板)

一、Xmind整理:

二、上课笔记整理:

1.虚继承解决菱形继承问题

#include 

using namespace std;

//公共基类
class Furniture
{
private:
    string color;
public:
    //无参构造函数
    Furniture()
    {
        cout << "家具的无参构造" << endl;
    }
    //有参构造函数
    Furniture(string c):color(c)
    {
        cout << "家具的有参构造" << endl;
    }
};

//中间子类
class Sofa:virtual public Furniture
{
private:
    string sitting;
public:
    //无参构造函数
    Sofa()
    {
        cout << "沙发的无参构造" << endl;
    }
    //有参构造函数
    Sofa(string s,string c):Furniture(c),sitting(s)
    {
        cout << "沙发的有参构造" << endl;
    }
    void display()
    {
        cout << sitting << endl;
    }
};

class Bed:virtual public Furniture
{
private:
    string sleep;
public:
    //无参构造函数
    Bed()
    {
        cout << "床的无参构造" << endl;
    }
    //有参构造函数
    Bed(string(s),string(c)):Furniture(c),sleep(s)
    {
         cout << "床的有参构造" << endl;
    }
    void display()
    {
        cout << sleep << endl;
    }
};


//汇聚子类
//封装 沙发床类 继承于沙发和床
class Sofa_Bed:public Bed,public Sofa
{
private:
    int w;
public:
    //无参构造函数
    Sofa_Bed()
    {
        cout << "沙发床的无参构造" << endl;
    }
    //有参构造函数
    Sofa_Bed(string sit, string s, int w, string c):Furniture(c),Bed(s,c),Sofa(sit,c),w(w)   //需要在汇聚子类中显性调用公共基类的有参构造
    {
         cout << "沙发床的有参构造" << endl;
    }
};
int main()
{
//    Sofa_Bed(s);
    Sofa_Bed s1("可坐","可躺",123,"pink");
//    s1.Sofa::display();
//    s1.Bed::display();
    return 0;
}

2.多态--->虚函数

#include 

using namespace std;

class Xu
{
private:
    string name;
    int age;
public:
    //无参构造函数
    Xu()
    {

    }
    //有参构造函数
    Xu(string name,int age):name(name),age(age)
    {

    }
    //虚函数
    virtual void speak()
    {
        cout << "阿巴阿巴阿巴。。。" << endl;
    }
};

class Student:public Xu
{
private:
    int id;
public:
    //无参构造函数
    Student()
    {

    }
    //有参构造函数
    Student(string name,int age,int id):Xu(name,age),id(id)
    {

    }
    void speak()
    {
        cout << "别打扰我,正在学习中。。。" << endl;
    }
};

class Player:public Xu
{
private:
    string game;
public:
    //无参构造函数
    Player()
    {

    }
    //有参构造函数
    Player(string name,int age,string game):Xu(name,age),game(game)
    {

    }
    void speak()
    {
        cout << "兄弟们,一波啦!!!" << endl;
    }
};

int main()
{
    Student s("xjx",22,001);
    Xu *p;              //父类的指针
    p = &s;             //父类的指针,指向子类对象
    p->speak();

    Player g("xjx",22,"王者");
    p = &g;
    p->speak();

    return 0;
}

3.虚析构函数

#include 

using namespace std;

class Person
{
private:
    string name;
public:
    //无参构造函数
    Person()
    {

    }
    //有参构造函数
    Person(string name):name(name)
    {

    }
    //虚析构函数
    virtual ~Person()
    {
        cout << "Person::析构函数" << endl;
    }
};

class Stu:public Person
{
private:
    int id;
public:
    //无参构造函数
    Stu()
    {

    }
    //有参构造函数
    Stu(string name,int id):Person(name),id(id)
    {

    }
    //虚析构函数
    ~Stu()
    {
        cout << "Stu::析构函数" << endl;
    }

};

int main()
{
    Person *p = new Stu("xjx",001);
    delete p;
    return 0;
}

4.抽象类

#include 

using namespace std;

class A
{
private:
    int a;
public:
    A()
    {

    }
    virtual void show() = 0;
};

class B:public A
{
public:
    B()
    {

    }
    void show()
    {

    }
};


int main()
{
    B b;
    return 0;
}

5.模板

#include 

using namespace std;

//创建函数模板
template  
void fun(T &a, T &b)
{
    T temp;
    temp = a;
    a = b;
    b = temp;
}

int main()
{
    int a = 10, b = 20;
    fun(a,b);
    cout << a << " " << b << endl;

    double c = 1.3,d = 1.4;
    fun(c,d);
    cout << c << " " << d << endl;

    return 0;
}

你可能感兴趣的:(C++,c++,开发语言)