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

C++之继承

2016-03-04 21:04 211 查看
#include <iostream>

using namespace std ;

class  Animal
{
private:
int age ;

protected:
int id ;

public:
int Height ;
void Say_hi(void)
{
cout << "this is hello" <<endl ;
}
};

//无论哪种继承,父类私有成员在子类不可访问
//公有继承,父类的公有跟受保护权限到了子类权限不变
//受保护继承,父类的公有成员变成子类的受保护成员,受保护成员权限不变
//私有继承,父类的公有成员及父类的受保护成员变成子类的私有成员

class People : public  Animal
{
private:
int aa ;

protected:
int cc ;

public :
int a ;

void Say_hi(void)
{
cout << "hi " << endl ;
}

void Say_cc(void)
{
cout << "cc : " << cc << endl ;
cout << "id : " << id << endl ;
//cout << "age : " << age << endl ;
cout << "aa : " << aa << endl ;
}
};

int main(void)
{

People  pp ;
//	pp.Height = 100 ;
//	pp.Say_cc();
//	pp.cc = 200 ;    //受保护的变量不允许直接访问
//	pp.id = 200 ;
//	pp.Say_hi();

pp.Say_hi();
//两者皆可
pp.Animal::Say_hi();
pp.People::Say_hi();

return 0 ;
}

运行结果:

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