首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++中私有承袭和公有继承的特点

2012-11-22 
c++中私有继承和公有继承的特点当类的继承方式为私有继承时,基类中的公有成员和保护成员都以私有成员的身

c++中私有继承和公有继承的特点

当类的继承方式为私有继承时,基类中的公有成员和保护成员都以私有成员的身份出现在派生类中,而基类中的私有成员在派生类中不可直接访问。上面继承类对象调用的函数都是派生类自身的公有成员,因为私有继承不可访问基类中的成员

#include <iostream>using namespace std;class point {public:void xpoint(int x=0,int y=0){this->x=x;this->y=y;}void move(int oof,int ooh){x+=oof;y+=ooh;}int getx(){return x;}int gety(){return y;}private:int x,y;};class ccc:private point{public:void xccc(int a,int b,int c,int d){xpoint(a,b);this->c=c;this->d=d;}void move(int oof,int ooh){point::move(oof,ooh);}int getx(){return point::getx();}int gety(){return point::gety();}int getm(){return c;} int getd(){return d;} private:int c,d;};int main(){ccc mm;mm.xccc(2,3,10,20);mm.move(10,10);cout<<mm.getx()<<"," <<mm.gety()<<"," <<mm.getm()<<"," <<mm.getd()<<endl;return 0;}


公有继承。基类的公有成员和保护成员的访问属性在派生类中不变,而基类的私有成员不可直接访问

#include<iostream>using namespace std;class point {public:void xpoint(int x=0,int y=0){this->x=x;this->y=y;}void move(int oof,int ooh){x+=oof;y+=ooh;}int getx(){return x;}int gety(){return y;}private:int  x,y;};class ccc:public point{public:void xccc(int a,int b,int c,int d){xpoint(a,b);this->c=c;this->d=d;}int  getm(){return c;}    int getd(){return d;} private:int c,d;};int main(){ccc mm;mm.xccc(2,3,10,20);mm.move(10,10);cout<<mm.getx()<<","   <<mm.gety()<<","   <<mm.getm()<<","   <<mm.getd()<<endl;}


 

热点排行