-
help with inheritance!
I am learning Object-Oriented Programming and I missed the class on inheritance. I am going through some problems in my textbook and I came across a few questions which I don't understand. There is a table given and you have to list all the private, protected, and public members on each Class. If someone could please help me to understand what gets passed to each. If someone could explain what gets passed to Class_B, I might be able to figure out the rest.
TABLE
----------------------------------------------
Base Class Member Access Category
----------------------------------------------
private
protected
public
----------------------------------------------
Derived Class Public Base Class
----------------------------------------------
private
protected
public
----------------------------------------------
Derived Class Private Base Class
----------------------------------------------
private
private
private
-----------------------------------------------
class Class_A
{
private:
int A_private;
protected:
int A_protected;
public:
Class_A(int, int);
void A_Pub_Func();
};
class Class_B : public Class_A
{
private:
int B_private;
protected:
int B_protected;
public:
Class_B(int, int);
void B_Pub_Func();
};
class Class_C : private Class_A
{
private:
int C_private;
protected:
int C_protected;
public:
Class_C(int, int);
void C_Pub_Func();
};
class Class_D : public Class_B
{
private:
int D_private;
protected:
int D_protected;
public:
Class_D(int, int);
void D_Pub_Func();
};
class Class_E : public Class_C
{
private:
int E_private;
protected:
int E_protected;
public:
Class_E(int, int);
void E_Pub_Func();
};
-
class derived-class-name: access base-class-name{
// body
};
Access of base class members inside of derived class is determined by access (above). Access may be public, private, or protected ( private is default ).
When access is public,
-public members become public,
-protected members become protected,
-private members are not accessible in derived class.
When access is private:
-All public and protected members become private
-private members are not accessible in derived class
When access is protected:
-All public and protected members become protected
-private members are not accessible in derived class
So:
B_class inherits:
A_protected (as protected member)
Class_A(int, int) (public)
void A_Pub_Func() (public)
Hope this helps.
-
Thanks a bunch!!!
It helps a lot.