美文网首页编程语言爱好者
C++ 类的继承和多继承

C++ 类的继承和多继承

作者: zcwfeng | 来源:发表于2021-03-29 12:19 被阅读0次

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;
}

相关文章

  • [C++之旅] 18 公有继承、保护继承和私有继承

    [C++之旅] 18 公有继承、保护继承和私有继承 继承 继承的类称为派生类或子类,被继承的类称为基类或父类。继承...

  • Java学习笔记三:继承、封装、多态

    一、继承 (1)继承知识点 父类也称作超类、基类、派生类等。 Java中只有单继承,没有像C++那样的多继承。多继...

  • C++ 类的继承和多继承

    c++ 继承实例 1.默认是 隐式代码: : private Person2.私有继承:在子类里面是可以访问父类的...

  • JNI基础 -- C++基础知识(继承多态,虚函数 )

    继承 在Java中类的继承只能是单继承,但是在C++中可以多继承。继承是通过一个冒号表示的,如下: C++中的继承...

  • Java--继承使用要点

    1.父类也称作超类、基类、派生类等。2.Java中只有单继承,没有像C++那样的多继承。多继承会引起混乱,使得继承...

  • 2020-02-17-C++虚继承机制

    C++ 中出现多继承,当多继承就会出现 两个父类的复制,这样就出现了虚继承机制。 A 继承 ZB继承 ZC 继承 ...

  • Python-学习之路-08 OOP -02

    单继承和多继承 单继承:每个类只能继承一个类 多继承:每个类可以继承多个类 单继承的多继承的优缺点 菱形继承/钻石...

  • 17-多继承

    多继承 C++允许一个类可以有多个父类(不建议使用,会增加程序的复杂度) 关于C++ 的多继承,定义了以下几个类,...

  • Java 基础 - 继承

    参考 C++多继承有什么坏处,Java的接口为什么可以摈弃这些坏处?C++多继承(钻石继承)的问题和解决C++继承...

  • 多继承 与 多重继承

    多继承 多继承是指一个子类继承多个父类。多继承对父类的个数没有限制,继承方式可以是公共继承、保护继承和私有继承。不...

网友评论

    本文标题:C++ 类的继承和多继承

    本文链接:https://www.haomeiwen.com/subject/kaxqhltx.html