0
点赞
收藏
分享

微信扫一扫

C++ 类的继承和多继承

c++ 继承实例

1.默认是 隐式代码: : private Person
2.私有继承:在子类里面是可以访问父类的成员,但是在类的外面不行
3.必须公开继承,才可以访问父类的成员

#include <iostream>

using namespace std;

class Person {
public:
    char *name;
    int age;

public:
    Person(char *name, int age) : name(name) {
        this->age = age;
        cout << "Person 构造函数" << endl;
    }

    void print() {
        cout << this->name << " , " << this->age << endl;
    }
};

// 1.默认是 隐式代码: : private Person
// 2.私有继承:在子类里面是可以访问父类的成员,但是在类的外面不行
// 3.必须公开继承,才可以访问父类的成员
class Student : public Person {

// 类 默认是私有,注意下

private:
    char * course;

public:
    // :父类 , 给自己子类成员初始化
    Student(char * name, int age, char* course) : Person(name, age) , course(course) {
        cout << "Student 构造函数" << endl;
    }

    void test() {
        cout << name << endl;
        cout << age << endl;
        print();
    }
};

int main() {
    Student stu("李元霸", 99, "C++");

    // 公开继承,才可以拿父类的成员
    stu.name = "李四";

    return 0;
}

C++是有多继承的

Java语言不允许多继承,多继承有歧义,如果Java语言多继承 就会导致代码不健壮,(二义性)
Java多实现:做的非常棒,严格避免出现 二义性问题(歧义)
下面是,第一种简单多继承问题,解决方案。

#include <iostream>

using namespace std;

class BaseActivity1 {
public:
    void onCreate() {
        cout << "BaseActivity1 onCreate" << endl;
    }

    void onStart() {
        cout << "BaseActivity1 onStart" << endl;
    }

    void show() {
        cout << "BaseActivity1 show" << endl;
    }
};

class BaseActivity2 {
public:
    void onCreate() {
        cout << "BaseActivity2 onCreate" << endl;
    }

    void onStart() {
        cout << "BaseActivity2 onStart" << endl;
    }

    void show() {
        cout << "BaseActivity2 show" << endl;
    }
};

class BaseActivity3 {
public:
    void onCreate() {
        cout << "BaseActivity3 onCreate" << endl;
    }

    void onStart() {
        cout << "BaseActivity3 onStart" << endl;
    }

    void show() {
        cout << "BaseActivity3 show" << endl;
    }
};

// 子类 继承 三个父类
class MainActivity1 : public BaseActivity1, public BaseActivity2, public BaseActivity3 {
public:
    void onCreate() {
        cout << "MainActivity1 onCreate" << endl;
    }

    void onStart() {
        cout << "MainActivity1 onStart" << endl;
    }

    void showSonInfo() {
        cout << "MainActivity1 showSonInfo" << endl;
    }

    // 解决方案二: 子类上 重写父类的show函数
    void show() {
        cout << "MainActivity1 show" << endl;
    }

};

int main() {
    // 这个是优先寻找子类的函数,因为特别明确,没有问题,还没有产生歧义(二义性)
    MainActivity1 mainActivity1; // 子类
    mainActivity1.onCreate();
    mainActivity1.onStart();
    mainActivity1.showSonInfo();

    // error: request for member 'show' is ambiguous
    // 不明确,二义性,歧义
    // mainActivity1.show();

    // 解决方案一: 明确指定父类 ::
    mainActivity1.BaseActivity3::show();
    mainActivity1.BaseActivity2::show();
    mainActivity1.BaseActivity1::show();

    // 解决方案二: 子类上 重写父类的show函数
    mainActivity1.show();

    return 0;
}

多继承 二义性2:(钻石问题)

在真实开发过程中,严格避免出现 二义性

#include <iostream>

using namespace std;

// 祖父类
class Object {
public:
    int number;
};

// 父类1
class BaseActivity1 : public Object {

};

// 父类2
class BaseActivity2 : public Object {

};

// 子类
class Son : public BaseActivity1, public BaseActivity2 {

    // 第二种解决方案: 在类中定义同名成员,覆盖掉父类的相关成员
public:
    int number;

};


int main() {
    Son son;

    // error: request for member 'show' is ambiguous  二义性 歧义
    // son.number = 2000;

    // 第一种解决方案: :: 明确指定
    son.BaseActivity1::number  = 1000;
    son.BaseActivity2::number  = 1000;

    // 第二种解决方案: 在类中定义同名成员,覆盖掉父类的相关成员
    son.number = 3000;

    // 第三种解决方案: 【虚基类】 属于 虚继承的范畴

    return 0;
}

【虚基类】 属于 虚继承的范畴

真实C++开始,是很少出现,二义性(歧义) 如果出现, 系统源码(系统用解决方案)

#include <iostream>

using namespace std;

// 祖父类
class Object{
public:
    int number;
    void show() {
        cout << "Object show run..." << endl;
    }
};

// 等下讲 virtual 的原理是什么 ...

// 父类1
class BaseActivity1 : virtual public Object {
// public:int number; // 人为制作二义性  error: request for member 'number' is ambiguous
};

// 父类2
class BaseActivity2 : virtual public Object {
// public:int number;
};

// 子类
class Son : public BaseActivity1, public BaseActivity2 {

};

int main() {
    Object object;
    BaseActivity1 baseActivity1;
    BaseActivity2 baseActivity2;
    Son son;

    object.number = 100;
    baseActivity1.number = 200;
    baseActivity2.number = 300;
    son.number = 400;

    object.show();
    baseActivity1.show();
    baseActivity2.show();
    son.show();

    cout << object.number << endl;
    cout << baseActivity1.number << endl;
    cout << baseActivity2.number << endl;
    cout << son.number << endl;

    return 0;
}

继承构造和析构函数的顺序关系

// 补充点: 继承关系的时候,构造函数和析构函数 的顺序问题

#include <iostream>

using namespace std;

class Person {
public:
    string name;

    Person(string name) : name(name) {cout << "Person构造函数" << endl;}

    ~Person()  {cout << "Person析构函数" << endl;}

    virtual void test() {
        cout << "父 test..." << endl;
    }
};

class Student : public Person {
public:
    string name;

    Student(string name) : Person(name) {
        cout << "Student构造函数" << endl;

        // Person::test();
    }

    ~Student()  {cout << "Student析构函数" << endl;}

    void test() {
        cout << "子 test..." << endl;
    }
};

int main() {
    Student student("David");
    // Person构造函数
    // Student构造函数
    // Student析构函数
    // Person析构函数

    Student student1("A");
    student1.test();

    return 0;
}
举报

相关推荐

0 条评论