Chapter – 6. INHERITANCE

OUTSIDE DELHI 2008

2.d. Answer the questions (i) to(iv) based on the following code :

class Toys
{
char Tcode[5];
protected: float
Price; void Assign(float);
public: Toys();
void Tentry();
void Tdisplay();
};
class SoftToys:public Toys
{
char STName[20];
float Weight; public:
SoftToys();
void STentry();
void STDisplay();
};
class ElectronicToys:public Toys
{
char ETName[20];
int No_of_Batteries;
public: ElecronicToys();
void ETEntry();
void ETDisplay();
};

(i) Which type of Inheritance is shown in the above example?

Ans: Hierarchical Inheritance. Since the sub classes are derived from a single base class(Dolls).

(ii) How many bytes will be required by an object of the class SoftToys ?

Ans: 33 Bytes (Explonation: The memory will be reserved as follows: char Tcode[5]; //5 Bytes float Price; //4 Bytes char STName[20]; //20 Bytes float Weight; // 4 Bytes Total = 33 Bytes)

(iii) Write name of all data members accessible from member function of the class SoftToys.

Ans: Toys::Price, SoftToys::STName, SoftToys::Weight

(iv) Write name of member functions accessible an object of the class ElectronicToys ?

Ans: ElectronicToys::ETEntry( ), Electronic Toys::ETDisplay( ), Toys::TEntry( ), Toys::TDisplay( )

DELHI 2007

2.d. Answer the questions (i) to(iv) based on the following code:4

class Trainer
{
char TNo[5],Tname[20],specialization[10];
int Days;
protected : float Remuneratoin;
void AssignRem(float);
public: Trainer();
void TEntry();
void TDisplay();
};
class Learner
{
char Regno[10],LName[20],Program[10];
protected: int Attendance,grade; public: Learner();
void LEntry();
void LDisplay();
};
class Institute:public Learner,public Trainer
{
char ICode[10],IName[20];
public: Institute();
void IEntry();
void IDisplay();
};

(i) Which type of inheritance is depicted by above example ?

Ans: Multiple Inheritance. Since here the class Institute is deriving from the classes Learner and Trainer.

(ii) Identify the member function(s) that cannot be called directly from the objects of class Institute from the following TEntry() LDisplay() IEntry()

Ans: All the above 3 member functions can be called directly from the objects of class Institute.

(iii) Write name of all member(s) accessible from member functions of class institute.

Ans: Data Members – Trainer::Remuneration,
Learner::Attendance,
Learner::Grade, Institute::ICode, Institute::IName

Member functions – Trianer::AssignRem( ),
Trainer::TEntry( ),
Trainer::TDisplay( ),
Learner:: LEntry( ),
Learner::LDisplay( ),
Institute::IEntry ( )
(LDisplay can call IEntry( ))
Institute::LDisplay( ) (IEntry can call LDisplay( ))

(iv) If class institute was derived privately from class Learner and privately from class Trainer, then name the member function(s)that could be accessed through Objects of class Institute.

Ans: Institute::IEntry( ), Institute:: IDisplay( ),