c++快速简易入门教程_006继承

来源:互联网 发布:百度云vip账号淘宝 编辑:程序博客网 时间:2024/05/04 12:05

1 继承

c++提供了一种重要的机制,继承。派生类继承基类的成员变量和成员方法

#include <iostream>using namespace std;class animal{public:animal(){cout<< "animal construct" << endl;}~animal(){cout << "animal destruct" << endl;}void eat(){cout << "animal eat" << endl;}void sleep(){cout << "animal sleep" << endl;}void breathe(){cout << "animal breathe" << endl;}};class fish :animal{public:fish(){cout << "fish construct" << endl;}~fish(){cout << "fish destruct" << endl;}};void main(){fish fh;printf("hello....");system("pause");return;}

运行结果:


运行结果分析:
    可以看到当构造fish类的对象fh时,animal类的构造函数也要被调用,而且在fish类的构造函数调用之前被调用。

    在析构时正好相反。

2 在子类中调用父类的带参数的构造函数

#include <iostream>using namespace std;class animal{public:animal(int height,int weigth){cout << "animal construct\t" <<"height:\t" << height<<"\tweigth:\t"<<weigth<< endl;}~animal(){cout << "animal destruct" << endl;}void eat(){cout << "animal eat" << endl;}void sleep(){cout << "animal sleep" << endl;}void breathe(){cout << "animal breathe" << endl;}};class fish :public animal{public:fish():animal(300,200){cout << "fish construct" << endl;}~fish(){cout << "fish destruct" << endl;}};void main(){fish fh;printf("hello....");system("pause");return;}
运行结果:

    解析:
    在fish类的构造函数后,加一个冒号(:),然后加上父类的带参数的构造函数。这样,在子类的构造函数
    被调用时,系统就会去调用父类的带参构造函数去构造对象。这种方式,还常用来对类中的常量(const)
    成员进行初始化,如下面的代码:

#include <iostream>using namespace std;class student{public:student() :name("FreeBamb"), age(20){}void getVar(){cout << "name:\t" << name << "\tage\t" << age << endl;}private:const char* name;const int age;};class animal{public:animal(int height,int weigth){cout << "animal construct\t" <<"height:\t" << height<<"\tweigth:\t"<<weigth<< endl;}~animal(){cout << "animal destruct" << endl;}void eat(){cout << "animal eat" << endl;}void sleep(){cout << "animal sleep" << endl;}void breathe(){cout << "animal breathe" << endl;}};class fish :public animal{public:fish():animal(300,200){cout << "fish construct" << endl;}~fish(){cout << "fish destruct" << endl;}};void main(){fish fh;student stu;stu.getVar();printf("hello....");system("pause");return;}


当然类中的普通的成员变量也可以采用这种方式来进行初始化,然而这就没有必要了。

3 类的继承及类中成员的访问特性

    public:定义的成员可以在任何地方访问
    protect:定义的成员只能在该类及子类中访问
    private:定义的成员只能在该类自身中访问
    如果在定义派生类时没有指定如何继承访问权限,则默认为private。如果派生类以public访问权限继承基类,
    则基类的成员在派生类中仍以原来的访问权限在派生类中出现。如果派生类以protected访问权限继承基类,则
    基类中的public和protected成员在派生类中都变成了protected类型的访问权限。

4 多重继承

#include <iostream>using namespace std;class B1{public:    void output1();};void B1::output1(){    cout << "call the class B1" << endl;}class B2{public:    void output2();};void B2::output2(){    cout << "call the class B2" << endl;}class A :public B1, public B2{public:    void show();};void A::show(){    cout << "call the class A" << endl;}void main(){    A a;    a.output1();    a.show();    printf("hello....");    system("pause");    return;}
运行结果:


解析:
    4.1基类的说明顺序一般没有重要的意义,除非在某些情况下要调用构造函数和析构函数时会有一些影响。
    由构造函数引起的初始化发生的顺序。如果你的代码依赖于A的B2部分要在B1部分之前初始化,则此说明顺序很重要,
    你可以在继承表中把B2类放在B1类的前面,初始化是按基类表中的说明顺序进行初始化的。
    4.2激活析构函数以做清除工作的顺序。同样,当类的其它部分需要被清除时,如果某些特别部分需要保留,
    则该顺序也很重要。析构函数的调用是按说明顺序的反向进行调用的。

0 0
原创粉丝点击