C++ publicprotectedprivate inheritance.
class A
{
public:
int a;
private:
int b;
protected:
int c;
};
//
// public inheritance:
// - data access type not change
// - B cannot access A's private member
//
class B: public A
{
public:
void test()
{
a = 1;
//b = 1; //Fail
c = 1;
}
};
//
// protected inheritance:
// - data access type: public -> protected
// - C cannot access A's private member
//
class C: protected A
{
public:
void test()
{
a = 1;
//b = 1; //Fail
c = 1;
}
};
//
// private inheritance:
// - data access type: public , protected -> private
// - D cannot access A's private member
//
class D: private A
{
public:
void test()
{
a = 1;
//b = 1; //Fail
c = 1;
}
};
class E: public C
{
public:
void test()
{
a = 1;
//b = 1;
c = 1;
}
};
class F: public D
{
void test()
{
//a = 1;
//b = 1;
//c = 1;
}
};
int main()
{
{
B b;
b.test();
b.a = 1;
//b.b = 1;
//b.c = 1;
}
{
C b;
b.test();
//b.a = 1;
//b.b = 1;
//b.c = 1;
}
{
D b;
b.test();
//b.a = 1;
//b.b = 1;
//b.c = 1;
}
{
E b;
b.test();
//b.a = 1;
//b.b = 1;
//b.c = 1;
}
}
{
public:
int a;
private:
int b;
protected:
int c;
};
//
// public inheritance:
// - data access type not change
// - B cannot access A's private member
//
class B: public A
{
public:
void test()
{
a = 1;
//b = 1; //Fail
c = 1;
}
};
//
// protected inheritance:
// - data access type: public -> protected
// - C cannot access A's private member
//
class C: protected A
{
public:
void test()
{
a = 1;
//b = 1; //Fail
c = 1;
}
};
//
// private inheritance:
// - data access type: public , protected -> private
// - D cannot access A's private member
//
class D: private A
{
public:
void test()
{
a = 1;
//b = 1; //Fail
c = 1;
}
};
class E: public C
{
public:
void test()
{
a = 1;
//b = 1;
c = 1;
}
};
class F: public D
{
void test()
{
//a = 1;
//b = 1;
//c = 1;
}
};
int main()
{
{
B b;
b.test();
b.a = 1;
//b.b = 1;
//b.c = 1;
}
{
C b;
b.test();
//b.a = 1;
//b.b = 1;
//b.c = 1;
}
{
D b;
b.test();
//b.a = 1;
//b.b = 1;
//b.c = 1;
}
{
E b;
b.test();
//b.a = 1;
//b.b = 1;
//b.c = 1;
}
}