inheritance

please help me with the following question

class Parent

{

char name[10]; protected :

int son;

public:

void inputdata();

void outputdata();

};

classFather : protected Parent

{

int daughter; protected:

int baby;

public:

void readata();

void writedata();

};

class Mother : private Parent

{

int x; public:

void fetchdata();

void displaydata();

};

(i)Which type of inheritance is demonstrated above ?

(ii)Name the base class and the derived class.

(iii)Name the data members that can be accessed from the object of class –

(a)Mother

(b)Father

(iv)Name the members that can be accessed from the member functions of the class Mother.

(v)What all constructors will be invoked and in what sequence if we create an object of the class Mother.

(vi)How many bytes will be allocated to an object of class Mother
(i)Which type of inheritance is demonstrated above ?

father class is protected inheritance
mother class is private inheritance

(ii)Name the base class and the derived class.

base class - parent
derived class - father and mother


(iii)Name the data members that can be accessed from the object of class –
(a)Mother

all, except the private members
(b)Father 

all, except the private members


(iv)Name the members that can be accessed from the member functions of the class Mother.

all members of mother and parent except the name which is private.


(v)What all constructors will be invoked and in what sequence if we create an object of the class Mother.

i am not sure, but as far as i remember.
if you create an object of derived class, the constructor of base class will be called first and then the constructor of derived class, if the object of derived class is destroyed or go out of scope the derived class's destructor will be called first and then the base class's.
EDIT:
so in other words, base on the code, the default constructor of the parent class will be called first and then the default constructor of mother class

(vi)How many bytes will be allocated to an object of class Mother 

i think 18 or so?


Last edited on
(VI)
You can check it's size with sizeof operator, but I think it'd be 20 bytes, since int has align of 4. 10 first bytes are for char array, 2 bytes are wasted to align next int to 4, 4 bytes for int, and second int is already aligned, 'cuz variable before is int, so we don't have to align it and 4 bytes go for that variable. 10 + 2 + 4 + 4 = 20.
Last edited on
Topic archived. No new replies allowed.