Thursday

Important Questions for Class 12 Computer Science (C++) – Inheritance (Extending Classes)

 

Previous Years Examination & Important Questions
2 Marks Questions

Question 1:
Differentiate between protected and private members of a class in context of Object Oriented Programming. Also give a suitable example illustrating accessibility/non-accessibility of each using a class and an object in C++. All India 2017

or

What is the difference between protected and private members of a class? Give a suitable example in C++ to illustrate with its definition within a class. All India 2015C

Answer:
Private visibility A member declared as private can be accessed only in class. It means that it cannot be accessed outside the class.
Protected visibility A member declared as protected can be accessed inside the class as well as inside its sub class only.

e.g.
class Super 
{
private: 
int x; 
protected: 
int y;
};
class Sub : protected Super 
{
private: 
int z;
public:
void disp()
{
cout<<x<<y<<z;
/*Here y and z can be accessed but x cannot be accessed because it is a private member of Super class*/
}
}:

Question 2:
Differentiate between members, which are present within the private visibility mode with those which are present within the public visibility modes. Delhi 2011
Answer:
Private visibility A member declared as private can be accessed only in class. It means that it cannot be accessed outside class.
Public visibility A member declared as public can be access inside the class as well as outside the class with object of that class.

e.g. class Super 
{
private 
int x; 
public: 
int y;
}:
class sub : private super 
{
private: 
int z; 
public:
void show()
{cout<<x<<y<<z;
/*Here y and z can be accessed because it a private member of super class*/
}
};

Question 3:
Differentiate between public and protected visibilities in context of object oriented programming giving suitable examples for both. Delhi 2008C
Answer:
Public visibility A member declared as public can be access inside the class as well as outside the class with object of that class.
Protected visibility A member declared as protected can be accessed inside the class as well as inside its sub class only. It cannot be accessed outside the class through the object of that class in which it is declared,

e.g. class Super 
{
public: 
int y; 
protected: 
int x; 
protected:
void input()
{
cin>>x>>y;
}
}:
class Sub: public Super
{
public: int z;
void Show()
{
x = 10;
cout<<x<<y<<z;
}
}:
void main()
{
Super SI: 
cin>>Sl.x;
SI.input();/*It cannot be accessed here because it is protected member*/
Sub S2;
cin>>S2.y>>S2.z;/*y and z can be accessed here because these are public members*/
S2.Show( );/*Show( ) can be accessed here because it is public member of Sub class*/
}

4 Marks Questions

Question 4:
Answer the questions (i) to (iv) based on the following:

class First 
{
int X1; 
protected: 
float X2;
public:
First(); 
void Enter1(); void Display1();
};
class Second : private First 
{
int Y1; 
protected:
float Y2; 
public:
Second(); 
void Enter2(); 
void Display();
};
class Third : public Second 
{
int Z1; 
public:
Third(); 
void Enter3();
void Display(); 
}:
void main()
{
Third T; //Statement 1
: _______ //Statement 2
}

(i) Which type of Inheritance out of the following is illustrated in the above example?
Single Level Inheritance, Multilevel Inheritance, Multiple Inheritance
(ii) Write the names of all the member functions, which are directly accessible by the object T of class Third as declared in main() function.
(iii) Write Statement 2 to call function Display!) of class Second from the object T of class Third.
(iv) What will be the order of execution of the constructors, when the object T of class Third is declared inside main()?

Answer:

(i) Multiple Inheritance
(ii) Enter3(), Display!) of class Third, Enter2(),
(iii) Statement2 T.Second::Display():
(iv) First( )→Second( )→Third()

Question 5:
Answer the questions (i) to (iv) based on the following : Delhi 2016

class PRODUCT 
{
int Code: 
char Item[20];
protected: 
float Qty; 
public:
PRODUCT ( );
void GetIn( ); void Show( ):
};
class WHOLESALER
{
int WCode; 
protected:
char Manager[20]; 
public:
WHOLESALER(); 
void Enter(); 
void Display ();
};
class SHOWROOM : public PRODUCT, 
private WHOLESALER
{
char Name[20],City[20];
public:
SHOWROOM();
void Input ();
void View ( );
};

(i) Which type of Inheritance out of the following is illustrated in the above example?
• Single Level Inheritance
• Multilevel Inheritance
• Multiple Inheritance
(ii) Write the names of all the data members, which are directly accessible from the member functions of class SHOWROOM.
(iii) Write the names of all the member functions, which are directly accessible by an object of class SHOWROOM.
(iv) What will be the order of execution of the constructors, when an object of class SHOWROOM is declared?

Answer:

(i) Multiple Inheritance
(ii) Name[20], City[20], Manager[20], Qty
(iii) Input(), View( ), Getln( ), Show( )
(iv) PRODUCT()→ WHOLESALER()
→ SHOWROOM()

Question 6:
Answer the questions (i) to (iv) based on the following: All India 2016

class ITEM
{
int Id;
char IName [20]; 
protected: 
float Qty; 
public:
ITEM();
void Enter(); void View();
};
class TRADER
{
int DCode; 
protected:
char Manager[20]; 
public:
TRADER(); 
void Enter(); 
void View();
};
class SALEPOINT : public ITEM,
private TRADER
{
char Name[20],
Location[20]; 
public:
SALEPOINT(); 
void EnterAll(); 
void ViewAll();
};

(i) Which type of Inheritance out of the following is illustrated in the above example?
• Single Level Inheritance
• Multilevel Inheritance
• Multiple Inheritance
(ii) Write the names of all the data members, which are directly accessible from the member functions of class SALEPOINT.
(iii) Write the names of all the member functions, which are directly accessible by an object of class SALEPOINT.
(iv) What will be the order of execution of the constructors, when an object of class SALEPOINT is declared?

Answer:

(i) Multiple Inheritance
(ii) Name[20], Location[20], Qty, Manager[20]
(iii) EnterAll( ), ViewAll( ), Enter ( ) and View( ) of class ITEM
(iv) ITEM( ) → TRADER() → SALEPOINT( )

Question 7:
Answer the questions (i) to (iv) based on the following: Delhi 2015

class Exterior
{
int OrderId; 
char Address[20]; 
protected:
float Advance;
public:
Exterior(); 
void Book();
void View();
};
class Paint : public Exterior
{
intWallArea, ColorCode; 
protected: 
char Type; 
public:
Paint(); 
void PBook(); 
void PView();
};
class Bill : public Paint
{
float Charges; 
void Calculate(); 
public:
Bill();
void Bi11ing(); 
void Print();
};

(i) Which type of inheritance out of the following is illustrated in the above example?
• Single Level Inheritance
• Multilevel Inheritance
• Multiple Inheritance
(ii) Write the names of all the data members, which are directly accessible from the member functions of class Paint.
(iii) Write the names of all the member functions, which are directly accessible from an object of class Bill.
(iv) What will be the order of execution of the constructors, when an object of class Bill is declared?

Answer:

(i) Multilevel Inheritance
(ii) WallArea, ColorCode, Type, Advance
(iii) Billing! b Print! b PBook( ), PView( ), Book( b View( )
(iv) Exterior() → Paint! ) → Bill( )

Question 8:
Answer the questions (i) to (iv) based on the following: All India 2015

class Interior 
{
int orderId; 
char Address[20]; 
protected:
float Advance; 
public;
Interior(); 
void Book(); 
void View();
}:
class Painting : public Interior
{
int WallArea, ColorCode; 
protected: char Type; 
public:
Painting(); 
void PBook(); 
void PView();
};
class Billing : public Painting 
{
float Charges; 
void Calculate(); 
public:
Billing(); 
void Bill(); 
void BillPrint();
};

(i) Which type of Inheritance out of the following is illustrated in the above example?
• Single Level Inheritance
• Multilevel Inheritance
• Multiple Inheritance
(ii) Write the names of all the data members, which are directly accessible from the member functions of class Painting.
(iii) Write the names of all the member functions, which are directly accessible from an object of class Billing.
(iv) What will be the order of execution of the constructors, when an object of class Billing is declared?

Answer:

(i) Multilevel Inheritance
(ii) WallArea, ColorCode, Type, Advance
(iii) Bill( b BillPrint( ), PBook( ), PView( ), Book( b View! )
(iv) Interior! ) → Painting! ) → Billing! )

Question 9:
Consider the following C++ code and answer the questions from (i) to (iv). Delhi 2014

class Campus
{
long Id;
char City[20];
protected:
char Country[20]; 
public:
Campus(); 
void Register(); 
void Display();
};
class Dept : private Campus
{
long DCode[10]; 
char HOD[20]; 
protected:
double Budget; 
public:
Dept();. 
void Enter(); 
void Show();
};
class Applicant : public Dept
{
long RegNo;
char Name[20]; 
public:
Applicant();
void Enroll (C); 
void View();
};

(i) Which type of inheritance is shown in the above example?
(ii) Write the names of those member functions, which are directly accessed from the objects of class Applicant.
(iii) Write the names of those data members, which can be directly accessed from the member functions of class Applicant.
(iv) Is it possible to directly call function Display( ) of class University from an object of class Dept? (Answer as Yes or No).

Answer:

(i) Multilevel Inheritance
(ii) Enroll! b View ( ), Enter ( ), Show( ).
(iii) RegNo, Name[20], Budget.
(iv) No, because in the given program there is no class named University.

Question 10:
Consider the following C++ code and answer the questions from (i) to (iv). All India 2014

class University 
{
long Id;
char City[20];
protected:
char Country[20]; 
public:
University();
void Register(); 
void Display(); 
};
class Department : private University 
{
long DCode[10]; 
char HOD[20]; 
protected:
double Budget; 
public:
Department(); 
void Enter(); 
void Show();
};
class Student : public Department 
{
long Roll No; 
public:
Student();
void Enroll();
void View();
};

(i) Which type of inheritance is shown in the above example?
(ii) Write the names of those member functions, which are directly accessed from the objects of class Student.
(iii) Write the names of those data members, which can be directly accessible from the member functions of class Student.
(iv) Is it possible to directly call function Display! ) °f class University from an object of class Department?
(Answer as Yes or No).

Answer:

(i) Multilevel inheritance
(ii) Enroll() View( ), Enter( ), Show( ).
(iii) RollNo, Budget.
(iv) No, it is not possible because class Department is inheriting from class University privately. So, all the public and protected members of the class University will become private in class Department and objects cannot access private members of a class.

0 comments:

Post a Comment

Thanks for leaving a comment. As soon as it's approved, it will appear.