#include<iostream>
using namespace std;
class ClassA
{
public:
int x;
ClassA(int x = 1, int y = 2, int z = 3);
~ClassA();
protected:
int y;
private:
int z;
};
class ClassB :protected ClassA
{
public:
int a;
void PublicShow();
void OtherShow();
ClassB(int a = 3, int b = 4, int c = 5, int x = 1, int y = 2, int z = 3);
~ClassB();
protected:
int b;
void ProtectedShow();
private:
int c;
void PrivateShow();
};
int main()
{
ClassB number(4, 5, 6, 1, 2, 3);
number.OtherShow();
return 0;
}
/*********A:构造************************************************/
ClassA::ClassA(int x, int y, int z)
{
this->x = x;
this->y = y;
this->z = z;
}
ClassA::~ClassA()
{
}
/*********B:构造************************************************/
ClassB::ClassB(int a, int b, int c, int x, int y, int z) :ClassA(x, y, z)
{
this->a = a;
this->b = b;
this->c = c;
}
ClassB::~ClassB()
{
}
/*********B:实现************************************************/
void ClassB::PublicShow()
{
cout << x << "+" << y << "+" << a << "+" << b << "+" << c << endl;
}
void ClassB::ProtectedShow()
{
cout << x << "+" << y << "+" << a << "+" << b << "+" << c << endl;
}
void ClassB::PrivateShow()
{
cout << x << "+" << y << "+" << a << "+" << b << "+" << c << endl;
}
void ClassB::OtherShow()
{
ProtectedShow();
PrivateShow();
}
/*Public继承总结
* 1.ClassB无权限操作ClassA的private属性和方法
* 2.ClassB可以公开调用ClassA的public属性和方法,ClassA的protected,仅限在ClassB的内部交流用
* 3.public方法中,如果子类和父类方法相同,优先调用子类自己的方法,PublicShow()
*/
/*protected继承总结
* 1.ClassB无权限操作ClassA的private属性和方法
* 2.ClassA中的public和protected的属性和方法在ClassB中权限降为ClassB中protected
*/
/*private继承总结
* 1.ClassB无权限操作ClassA的private属性和方法
* 2.ClassA中的public和protected的属性和方法在ClassB中权限降为ClassB中private
*/
Private/Protected继承
最新推荐文章于 2022-11-15 15:19:53 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)