您的位置:首页 > 编程语言 > C语言/C++

c++ 继承 多重继承 多态性 虚函数

2016-03-05 21:48 295 查看
从继承的角度来看,(<son> y)所继承到的成员(<parent> x 的数据成员)的访问属性:

x作为y 的成员,属性是由继承方式决定的,访问属性可由普通成员属性推理理解

多重继承的访问属性:

可按照(上边)继承加递归的思想理解

多态性

virtual

this

编译器帮助我们简化操作,通过虚函数

方便多多

总结

c++ 让编译器多做点工作,方便了代码的编写

#include<iostream>

using namespace std;

class parent

{

protected:

int a;

public:

virtual void display();

parent(int =1);

};

void parent:: display()

{

cout << "a=" << a<<endl;

}

parent::parent(int m)

{

a=m;

}

class son:public parent

{

private:

int b;

public:

void display();

son(int=2);

};

void son::display()

{

cout <<"a="<<a<<"b="<<b<<endl;

}

son::son(int n)

{

b=n;

}

int main()

{

parent x;

son y;

parent * finger;

finger=&x;

finger->display();

finger=&y;

finger->display();

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: