c++之旅:继承

继承

继承有关于权限的继承,多继承和虚继承

权限继承

权限继承有公有继承,保护继承和私有继承

公有继承

公有继承可以继承父类的public和protected属性和方法

#include <iostream>
#include <string>
using std::string;
class Person {
    public:
       int age;
    private:
        string name;
    protected:  
        bool sex;
};

class Worker:public Person {
};

Person类中的age和sex将会被Work类继承下来,而name由于是私有属性,无法被继承。并且age将会是Worker的public属性,而sex是Worker的protected属性

保护继承

保护继承可以继承父类的public和protected属性和方法,但是被继承的属性在子类中将会成为protected

#include <iostream>
#include <string>
using std::string;
class Person {
    public:
       int age;
    private:
        string name;
    protected:  
        bool sex;
};

class Worker:protected Person {
};

Worker继承了Person的age和sex,但是这两个属性在Worker都成为了protected。

私有继承

保护继承可以继承父类的public和protected属性和方法,但是被继承的属性在子类中将会成为private

#include <iostream>
#include <string>
using std::string;
class Person {
    public:
       int age;
    private:
        string name;
    protected:  
        bool sex;
};

class Worker:private Person {
};

age和sex将会被Worker继承下来,但是在Worker类中这两个属性将会变成private

多继承

一个子类有两个以上的父类

class Parent1 {};
class Parent2 {};
class Child : public Parent1, public Parent2 {};

虚继承

虚继承为了防止父类对象冗余的问题,当一个对象被创建时首先会创建其父类对象。但是由于多继承的存在,会导致一个父类被创建两次,出现了数据冗余

class Base{};
class Parent1 : public Base{};
class Parent2 {} : public Base{};;
class Child : public Parent1, public Parent2 {};

上面的代码中,当我们创建Child对象时,会导致Base对象被创建两次,如果使用虚继承,则Base对象只会被创建一次。

class Base{};
class Parent1 : virtual public Base{};
class Parent2 {} : virtual public Base{};;
class Child : public Parent1, public Parent2 {};

注意:虚继承有些副作用,请看链接
http://blog.csdn.net/dqjyong/article/details/8029527

继承的特殊性

同名函数

如果子类中出现了和父类同名的函数(即使参数列表不一样),则父类中的函数将不会被继承

#include <iostream>
#include <string>
using namespace std;

class Person {
	public:
	    void print() {cout << "Person" << endl;}
};

class Worker: public Person {
	public:
	    void print(int x) {cout << "Worker" << endl;}
};

int main(void) {
    Worker* worker = new Worker();
    worker->print();  //编译失败,Worker类中没有该方法
}

析构函数虚化

通常将类的析构函数虚化,是为了释放子类对象

#include <iostream>
#include <string>
using namespace std;

class Person {
    public:
        Person(){};
        ~Person(){cout << "Person" << endl;}
};

class Worker: public Person {
    public:
        Worker(){};
        ~Worker(){cout << "Worker" << endl;}
};

int main(void) {
    Person* person = new Worker();
    delete person;
}

上面的代码中创建了子类对象worker,但是我们将其向上转型赋值给person指针。在利用delete释放该对象时只调用了父类的析构函数,也就是只释放了父类的空间,而并没有释放子类的空间。但将父类的析构函数设置为虚函数后,则会调用子类的析构函数然后调用父类的析构函数,这是利用了析构函数的特性。

posted @ 2017-05-30 16:00  被罚站的树  阅读(192)  评论(0编辑  收藏  举报