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

一、Xmind整理:

二、上课笔记整理:

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

#include <iostream>

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 <iostream>

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 <iostream>

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 <iostream>

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 <iostream>

using namespace std;

//创建函数模板
template  <typename T>
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值