100 lines
1.3 KiB
C++
100 lines
1.3 KiB
C++
|
#include <iostream>
|
||
|
#include <cstring>
|
||
|
#include <cstdlib>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
class A
|
||
|
{
|
||
|
public:
|
||
|
int a;
|
||
|
|
||
|
protected:
|
||
|
int b;
|
||
|
|
||
|
private:
|
||
|
int c;
|
||
|
|
||
|
public:
|
||
|
A(int a, int b, int c) : a(a), b(b), c(c) {}
|
||
|
|
||
|
public:
|
||
|
void showA()
|
||
|
{
|
||
|
cout << "A: " << a << ", " << b << ", " << c << endl;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
class B : public A
|
||
|
{
|
||
|
public:
|
||
|
B() : A(1, 3, 2) {}
|
||
|
|
||
|
public:
|
||
|
void showB()
|
||
|
{
|
||
|
showA();
|
||
|
// B 类的成员函数内,可以访问父类的 public 、protected 成员
|
||
|
// a, b 是父类的 public/protected 的成员变量
|
||
|
cout << "B: " << a << ", " << b << endl;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
class C : protected B
|
||
|
{
|
||
|
public:
|
||
|
C() : B() {}
|
||
|
|
||
|
public:
|
||
|
void showC()
|
||
|
{
|
||
|
showA();
|
||
|
showB();
|
||
|
cout << "C: " << a << ", " << b << endl;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
class D : private C
|
||
|
{
|
||
|
public:
|
||
|
D() : C() {}
|
||
|
|
||
|
public:
|
||
|
void showD()
|
||
|
{
|
||
|
showA();
|
||
|
cout << "D: " << a << ", " << b << endl;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
class E : public D
|
||
|
{
|
||
|
public:
|
||
|
E() : D() {}
|
||
|
|
||
|
public:
|
||
|
void showE()
|
||
|
{
|
||
|
showA();
|
||
|
// cout << "E: " << a << ", " << b << endl;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
// B b1;
|
||
|
// b1.showA();
|
||
|
// b1.showB();
|
||
|
|
||
|
// C c1;
|
||
|
// c1.showC();
|
||
|
|
||
|
// D d1;
|
||
|
// d1.showD();
|
||
|
|
||
|
E e1;
|
||
|
e1.showD();
|
||
|
|
||
|
return 0;
|
||
|
}
|