Wednesday

Important Questions of Inheritance - CSE- Class 12 (Part 2)

 Question 11:

Consider the following class State: All India 2014C

class State 
{
protected:
int tp: //no. of tourist places 
public:
State()
{
tp = 0;
}
void inctp()
{
tp++;
}
int gettp()
{
return tp;
}
}:

Write a code in C++ to publically derive another class ‘District’ with the following additional members derived in the public visibility’mode.
Data Members
• distname – char (50)
• population – long
Member functions
• dinput() — To enter distname and population.
• doutput() — To display distname and population on screen.
Answer:

class State
{
protected:
int tp; //No. of tourist places public: 
state()
{
tp = 0;
}
void inctp() 
{
tp++;
int gettp()
{
return tp;
}
};
class District : public State
{
public:
char distname[50]; 
long population;
District()
{
distname = " "; 
population = 0;
{
void dinput!)
{
cout<<"Enter distname and population”; 
cin>>distname>>population;
}
void doutput()
{
cout<<"Di stname: "<<distname; cout<<"Population:"
}
};

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

class Personal
{
int Class, Rno; 
char Section; 
protected:
char Name[20]; 
public:
Personal(); 
void Pentry(); 
void Pdisplay();
}:
class Marks : private Personal 
{
float M[5]; 
protected:
char Grade[5]; 
public:
Marks(); 
void Mentry(); 
void Mdisplay():
};
class Result : public Marks 
{
float Total, Agg; 
public:
char FinalGrade, Comments[20]; 
ResultO() .
void Rcalculate(); 
void RdisplayO;
};

(i) Which type of inheritance is shown in the above example?
(ii) Write the names of those data members, which can be directly accessed from the objects of class Result.
(iii) Write the names of those member functions, which can be directly accessed from the objects of class Result.
(iv) Write the names of those data members, which can be directly accessed from the Mentry() function of class Marks.

Answer:

(i) Multilevel Inheritance
(ii) FinalGrade, Comments [20]
(iii) Rcalculate( ), Rdisplay( ),
Mentry( ), Mdisplay( )
(iv) M[5], Grade[5], Name[20]

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

class Student 
{
int Class, Rno; 
char Section; .
protected:
char SName[20];
public:
Student(); 
void Stentry(); 
void Stdisplay();
};
class Score : private Student
{
float Marks[5];
protected:
char Grade[5]; 
public:
Score(); 
void Sentry(); 
void Sdisplay();
};
class Report : public Score
{
float Total, Avg; 
public:
char OverallGrade, Remarks[20]; 
Report(); 
void REvaluate(); 
void RPrint]);
};

(i) Which type of inheritance is shown in the above example?
(ii) Write the names of those data members, which can be directly accessed from the objects of class Report.
(iii) Write the names of those member functions, which can be directly accessed from the objects of class Report.
(iv) Write the names of those data members, which can be directly accessed from the Sentry() function of class Score.

Answer:

(i) Multilevel Inheritance
(ii)  OverallGrade, Remarks[20].
(iii) REvaluate( ), RPrint( ), Sentry( ),
Sdisplay( ).
(iv) Marks[5], Grade[5], SName[20]

Question 14:
Consider the following and answer the questions given below: Delhi 2013C

class ITEM 
{
char ICodet10]; 
protected:
char IName[20]; 
public:
ITEM(); 
void Enter(); 
void Display();
}:
class SUPPLIER 
{
char SCode[10];
protected:
char SName[25];
 public:
SUPPLIER(): 
void TEnter(); 
void TDisplay();
}:
class SHOP : private SUPPLIER,
public ITEM
{
char SH0PADDRESSC[15],SEmai1[25]; 
public:
SHOP(); 
void Enter(); 
void Display();
}:

(i) Which type of inheritance is shown in the above example?
(ii) Write the name of all the member functions accessible from Enter]) function of class SHOP.
(iii) Write the names of all the member functions accessible through an object of class SHOP.
(iv) What will be the order of execution for the constructors ITEM]), SUPPLIER() and SHOP(), when an object of class SHOP is declared?

Answer:

(i) Multiple Inheritance
(ii) TEnter( ) and TDisplay( ) of class SUPPLIER Enter( ) and Display( ) of both class ITEM and SHOP
(iv) Enter( ) and Display( ) of both class SHOP and ITEM.
(iv) SUPPLIER() → ITEM( ) → SHOP( )

Question 15:
Answer the questions (i) to (iv) based on the following: All India 2012

class COMPANY
{
char location[20]; 
double budget, income; 
protected:
void Accounts(); 
public:
COMPANY(): 
void Register(); 
void Show();
}:
class FACTORY : public COMPANY
{
char location[20];
int workers:
protected:
double Salary; 
void Computer(); 
public:
FACTORY(); 
void Enter(); 
void Show();
};
class SHOP : private COMPANY 
{
char location[20]; 
float area; 
double sale; 
public:
SHOP(); 
void Input(); 
void Output();
};

(i) Name the type of inheritance illustrated in the above C++ code.
(ii) Write the names of data members, which are accessible from the member functions of class SHOP.
(iii) Write the names of all the member functions, which are accessible from objects belonging to class FACTORY.
(iv) Write the names of all the members, which are accessible from objects of class SHOP.

Answer:

(i) Hierarchical Inheritance
(ii) location[20], area, sale
(iii) Enter( ), Register() and Show( ) of both
class FACTORY and COMPANY
(iv) Data Members
None
Member Functions
Input( ), Output( )

Question 16:
Answer the questions (i) to (iv) based on the following: Delhi 2012

class ORGANISATION 
{
char Address[20]; 
double budget,Income; 
protected:
void Compute(); 
public:
ORGANISATION(); 
void Get(); 
void Show();
};
class WORKAREA : public ORGANISATION 
{
char Address[20]; 
int staff; 
protected: 
double pay;
void Calculate(); 
public:
WORKAREA(); 
void Enter(); 
void Display();
};
class SHOWROOM : private ORGANISATION
{
char Address[20]; 
float Area; 
double Sale; 
public:
SHOWROOM();
void Enter!(); 
void Show();
};

(i) Name the type of inheritance illustrated in the above C++ code.
(ii) Write the names of data members, which are accessible from member functions of class SHOWROOM.
(iii) Write the names of the member functions, which are accessible from objects belonging to class WORKAREA.
(iv) Write the names of all the members, which are accessible from objects of class SHOWROOM.

Answer:

(i) Hierarchical Inheritance
(ii) Address[20], Area, Sale
(iii) Enter( ), Display) ), Get( ), Show( )
(iv) Data Members
None
Member Functions
Enter( ), Show( )

Question 17:
Answer the questions (i) to (iv) based on the following: Delhi 2011

class Student
{
int Rno; 
char name[20]; 
float Marks; 
protected:
void Result(); 
public:
Student(); 
void Register();
void Display ();
};
class Faculty 
{
long Fcode; 
char Fname[20]; 
protected: 
float Pay; 
public:
Faculty();
void Enter(); 
void Show();
};
class Course : public Student,
private Faculty
{
long CCode[10]; 
char CourseName[50]; 
char StartDate[8],EndDate[8]; 
public:
Course();
void Commerce();
void CDetai1();
};

(i) Which type of inheritance is illustrated in the above C++ code?
(ii) Write the names of the data members, which is/are accessible from the member function Commerce of class Course.
(iii) Write the name of all the member functions, which are accessible from object of class Course.
(iv) Write the name of all the member function’s, which are accessible from object of class Faculty.

Answer:

(i) Multiple Inheritance
(ii) CCode[10], CourseName[50], StartDate[8], EndDate[8], Pay
(iii) Register( ), Display) ), Commerce) ), CDetail()
(iv) Enter( ), Show( )

Question 18:
Answer the questions (i) to (iv) based on the following: All India 2011

class Student
{
int Roll no; 
char Sname[20]; 
float Marksl; 
protected:
void Result(); 
public:
Student(); 
void Enroll() ; 
void Display!);
}:
class Teacher
{
long Tcode;
char Tname[20]; 
protected:
float Salary; 
public:
Teacher();
void Enter(); 
void Show();
}:
class Course : public Student,
private Teacher
{
long CCode[10]; 
char CourseName[50];
char StartDate[8]; 
char EndDate[8]; 
public:
Course(); 
void Commerce(); 
void CDetail();
}:

(i) Write the name of all the member functions, which are accessible from object of class Course.
(ii) Write the name of the data members, which is/are accessible from the member function Commerce of class Course.
(iii) Write the name of all the member functions, which are accessible from object of class Teacher.
(iv) Which type of inheritance is illustrated in the above C++ code?

Answer:

(i) Enroll( ), Display) ), Commerce) ),
CDetail) )
(ii) CCodeflO], CourseName[50], StartDate[8], EndDate[8], Salary
(iii) Enter(), Show()
(iv) Multiple Inheritance

Question 19:
Answer the questions (i) to (iv) based on the following: All India 2010

class Director 
{
long DID; 
char Name[20]; 
protected:
char Description[40]: 
void Allocate(); 
public:
Director();
void Assign();
void Show();
}: 
class Factory : public Director
{
int FID;
char Address[20]; 
protected: 
int NOE: 
public:
Factory();
void Input(); 
void Output()
}:
class ShowRoom : private Factory
{
int SID: 
char City[20]; 
public:
ShowRoom(); 
void Enter();
void Display!
}:

(i) Which type of inheritance out of the following is illustrated in the above C++ code?
• Single Level Inheritance
• Multilevel Inheritance
• Multiple Inheritance
(ii) Write the names of data members, which are accessible by objects of class type ShowRoom.
(iii) Write the name of all member functions, which are accessible by objects of class type ShowRoom.
(iv) Write the names of all members, which are accessible from member function of class Factory.

Answer:

(i) Multilevel Inheritance
(ii) None
(iii) Enter(), Display()
(iv) Data Members
FID, Address[20], NOE, Description[40]
Member Functions
Input(), Output(), Assign(), Show(), Allocate()

Question 20:
Answer the questions (i) to (iv) based on the following: Delhi 2010

class Chairperson 
{
long CID;
//Chairperson Identification Number
char Cname[20];
protected:
char Description[40]: 
void Allocate(); 
public:
Chairperson(); 
void Assign(); 
void Show();
}:
class Director 
{
char profile[30];
public:
Director();
vateFactory();
voidlnput();
void Output();
}:
class Company : private Chairperson, public Director
{
int CID; //Company ID
char City[20], Country[20]; 
public:
Company(); 
void Enter(); 
void Display();
}:

(i) Which type of inheritance out of the following is illustrated in the above C++ code?
• Single Level Inheritance
• Multilevel Inheritance
• Multiple Inheritance
(ii) Write the names of data members, which are accessible by objects of class type Company.
(iii) Write the name of all member functions, which are accessible by objects of class type Company.
(iv) Write the names of all members, which are accessible from member functions of class Director.

Answer:

(i) Multiple Inheritance
(ii) None
(iii) Enter(), Display(), Input(), Output()
(iv) Data Members
DID, Dname[20], profile[30]
Member functions
Input(), Output()

Question 21:
Answer the questions (i) to (iv) based on the following: Delhi 2009C

class QUALITY 
{
private:
char Material [30]; 
float Thickness; 
protected:
char Manufacturer[20]; 
public:
QUALITY(); 
void Reading(); 
void Printing(); 
};
class QUANTITY : public 
QUALITY
long Order; 
protected: 
int Stock; 
public:
QUANTITY(); 
void Read_Data(); 
void Print_Data();
};
class FABRIC : public QUANTITY 
{
int Fabric_code, Cost; 
public;
FABRIC(); 
void Read(); 
void Show();
};

(i) Write the data members that are accessible by an object of type class FABRIC.
(ii) What type of inheritance is illustrated in the above example?
(iii) Write the names of all the members that can be accessed by the member functions of class FABRIC.
(iv) How many bytes will be required by an object of type FABRIC?

Answer:

(i) None
(ii) Multi Level Inheritance
(iii) Data Members
Fabric_code, Cost, Stock, Manufacturer[20]
Member Functions
Read(), Show(),Read_Data( ), Print_Data( ), Reading)), Printing))
(iv) 64 Bytes

Question 22:
Answer the questions (i) to (iv) based on the following: Delhi 2009

class FacetoFace
{
char CenterCode[10]; 
public:
void Input(); 
void Output();
};
class Online 
{
char Website[50]; 
public:
void SiteIn(); 
void SiteOut();
};
class Training : public FacetoFace, private Online 
{
long Tcode; 
float charge; 
int period; 
public:
void Register(); void Show();
};

(i) Which type of inheritance is shown in above example?
(ii) Write names of all member functions accessible from Show( ) function of class Training.
(iii) Write names of all members accessible through an object of class Training.
(iv) Is function Output() is accessible in function SiteOut()? Justify your answer.

Answer:

(i) Multiple Inheritance
(ii) Register(), Show(), Input(), Output(), Siteln(), SiteOut()
(iii) Data Members None Member Functions Register(), Show(), Input(), Output()
(iv) Yes, it can be accessed by using the object of FacetoFace class, because it is a public member.

Question 23:
Answer the questions (i) to (iv) based on the following; All India 2009

class Regular
{
char SchoolCode[10]; 
public:
void InRegular(); 
void OutRegular();
};
class Distance
{
char StudyCenter[5]; 
public:
void InDistance();
void OutDistance!);
};
class Course : public Regular,
private Distance
{
char Code[5]; 
float Fees; 
int Duration; public:
void InCourse();
void OutCourse();
}:

(i) Which type of inheritance is shown in above example?
(ii) Write names of all member functions accessible from OutCourse( ) function of class Course.
(iii) Write names of all members accessible through an object of class Course.
(iv) Is function InRegular( ) is accessible in function InDistance()? Justify your answer.

Answer:

(i) Multiple Inheritance
(ii) InCourse(), Outcourse(), InDistance(), OutDistance(), InRegular(), OutRegular()
(iii) Data Members None Member Functions InCourse(), OutCourse(), InRegular(), OutRegular()
(iv) Yes, it can be accessed by using.the object of class Regular because it is a public member.

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.

Wednesday

Important Questions for Class 12 Computer Science (C++) – Constructor and Destructor (Part 3)

 Question 26:

Answer the questions (i) and (ii) after going through the following class: Delhi 2013

class Motor
{
int MotorNo, Track; 
public:
Motor();          //Function 1
Motor(int MN);    //Function 2
Motor(Motor &M);  //Function 3
void Allocate()   //Function 4
void Move();
};
void main() 
{
Motor M;
  :
  :
}
  1. Out of the following, which of the option is correct for calling Function 2?
    Option 1 – Motor N(M);
    Option 2 – Motor P(10) ;
  2. Name the feature of object oriented programming, which is illustrated by Function 1, Function 2 and Function 3 combined together.

Аnswer:

  1. Option 2-Motor P( 10) is correct.
  2. Constructor overloading.

Question 27:
Answer the questions (i) and (ii) after going through the following class: Delhi 2012

class Tour 
{
int LocationCode;
char Location[20]; 
float charges;
public:
Tour()    //Function 1
{
LocationCode = 1; 
strcpy(Location,"PURI"); 
charges = 1200;
}
void TourPlan(float C) //Function 2
{
cout<<LocationCode<<":"<< Location<<":”<<charges<<endl; 
charges += 100;
}
Tour(int LC, char L[], float C)  //Function 3
{
LocationCode=LC; 
strcpy(Location,L); 
charges = C;
}
~Tour()    //Function 4
{
cout<<"TourPlan Cancelled"<<endl;
}
};
  1. In object oriented programming, what are Function 1 and Function 3 combined together as?
  2. In object oriented programming, which concept is illustrated by Function 4? When is this function called/invoked?

Аnswer:

  1. Function 1 and Function 3 combined together referred as constructor overloading, i.e. polymorphism.
  2. Function 4 indicates destructor. This function is called/invoked whenever an object goes out of scope.

Question 28:
Answer the questions (i) and (ii) after going through the following class: All India 2012

class Travel 
{
int PlaceCode; 
char Place[20]; 
float Charges; 
public:
Travel()    //Function 1
{
PlaceCode = 1; 
strcpy(Place, "DELHI");
Charges = 1000;
}
void TravelPlan(float C) //Function 2 
{
cout<<PlaceCode<<":"<<Place<<":"<<Charges<<endl;
}
∼Travel()    //Function 3
{
cout<<"TravelPlan Cancelled"<<endl;
}
Travel(int PC, char P[], float C)    //Function 4 
{
PlaceCode = PC; 
strcpy(Place, P):
Charges = C;
}
};
  1. In object oriented programming, what are Function 1 and Function 4 combined together as ?
  2. In object oriented programming, which concept is illustrated by Function 3? When is this function called/invoked?

Аnswer:

  1. Function 1 and Function 4 combined together referred as constructor overloading, i.e. polymorphism.
  2. Function 3 indicates destructor/ This function is called/invoked whenever an object goes out of scope.

Question 29:
Find the output of the following program: Delhi 2012

#include<iostream.h> 
class Train 
{ 
int TNo.TripNo.PersonCount;
public:
Train(int TN = 1)
{
TNo = TN:
TripNo=0;
PersonCount=0;
}
void Trip(int TC=100)
{
TripNo++;
PersonCount+=TC;
}
void Show()
{
cout<<TNo<<":"<<TripNo<<":"<<PersonCount<<endl;
}
};
void main()
{
Train T(10),N;
N.Trip();
T.Show();
N.Trip(70);
N.Trip(40);
N.Show();
T.Show();
}

Аnswer:
Output of the given program would be:
10:0:0
1:3:210
10:0:0

Question 30:
Find the output of the following program: All India 2012

#include<iostream.h> 
class METRO 
{
int Mno,TripNo,PassengerCount; 
public:
METR0(int Tmno=1)
{
Mno=Tmno:
TripNo=0;
PassengerCount=0;
}
void Trip(int PC=20)
{
TripNo++;
PassengerCount+=PC;
}
void StatusShow()
{
cout<<Mno<<":"<<TripNo<<":"<<PassengerCount<<endl;
};
void main()
{
METRO M(5),T;
M.Trip();
T.Trip(50);
M.StatusShow();
M.Trip(30);
T.StatusShow(); 
M.StatusShow();
}

Аnswer:
Output of the given program would be:
5:1:20
1:1:50
5:2:50

Question 31:
Rewrite the following program after removing the syntactical errors (if any). Underline each correction. Delhi 2011c

#inc1ude<iostream.h> 
#include<stdio.h> 
class AUTO 
{
char Model[20]; 
float Price;
AUTO()
{
Price = 0;
strcpy(Model,"NULL");
}
public:
void GetInfo()
{
cin>>Price; 
gets(Model);
}
void PutInfo()
{
cout<<setw(10)<<Price<<setw(10)<<Model<,<endl;
}
}
void main()
{
AUTO Car; 
Car.GetInfo(); 
Car.PutInfo();
}

Аnswer:

#include<iostream.h>
#include<stdio.h> 
#include<string.h>
#inc1ude<iomanip.h> 
class AUTO
{
char Model[20]; 
float Price; 
public:
AUTO()
{
Price = 0;
strcpy(Model, "NULL”);
}
void Getlnfo()
{
cin>>Price; 
gets(Model);
}
void Putlnfo()
{
cout<<setw(10)<<Price<<setw(10)<<Model<<endl;
}
};
void main()
{
AUTO Car;
Car.Getlnfo();
Car.Putlnfo();
}

Question 32:
Answer the questions (i) and (ii) after going through the following class; All India 2010

class Exam 
{
int Rno, MaxMarks, MinMarks, Marks; 
public:
Exam()    //Module 1
{
Rno - 101; MaxMarks = 100; 
MinMarks = 40; Marks = 75;
}
Exam(int Prno.int Pmarks) //Module 2
{
Rno = Prno; MaxMarks = 100; 
MinMarks = 40; Marks = Pmarks;
}
∼ExamO    //Module 3
{
cout<<"Exam over"<<endl;
}
void show()    //Module 4
{
cout<<Rno<<":"<<MaxMarks<<":"<<MinMarks<<endl; 
cout<<"[MarksGot]"<<Marks<<endl;
}
};
  1. As per object oriented programming, which concept is illustrated by Module 1 and Module 2 together?
  2. What is Module 3 specifically referred as, when do you think Module 3 will be invoked/called?

Аnswer:

  1. Constructor overloading or polymorphism.
  2. Function 3 is referred to as destructor. It is invoked or called, when scope of an object gets over.

Question 33:
Answer the questions (i) and (ii) after going through the following class: Delhi 2016

class TEST
{
int Regno, Max, Min, Score; 
public:
TESTO    //function 1
{
Regno = 101; Max = 100;
Min = 40; Score = 75;
}
TEST(int Pregno.int Pscore) //Function 2
{
Regno = Pregno; Max = 100; 
Min = 40; Score = Pscore;
}
~TEST()    //Function 3
{
cout<<"TEST over"<<endl;
}
void Display()   //Function 4
{
cout<<Regno<<":"<<Max<<":"<<Min<<endl; 
cout<<"[Score]"<<Score<<endl;
}
};
  1. As per object oriented programming, which concept is illustrated by Function 1 and Function 2 together?
  2. What is Function 3 specifically referred as, when do you think Function 3 will be invoked/called?

Аnswer:

  1. Constructor overloading or polymorphism.
  2. Function 3 is referred to as destructor. It is invoked or called, when scope of an object gets over.

Question 34:
Answer the questions (i) and (ii) after going through the following class: HOTS; Delhi 2009

class WORK
{ 
int Workld; char WorkType; 
public:
∼WORK()    //Function 1
{
cout<<"Un-allocated"<<endl;
}
void status()    //Function 2
{
cout<<WorkId<<":"<<WorkType<<endl;
}
WORK()    //Function    3
{
Workld = 10; WorkType = 'T';
}
WoRK(WORK &W)    //Function 4
{
WorkId = W.WorkId+12;
WorkType = W.WorkType+1;
}
};
  1. Which member function, out of Function 1, Function 2, Function 3 and Function 4 shown in the above definition of class WORK is called automatically, when the scope of an object gets over? Is it known as constructor or destructor or overloaded function or copy constructor?
  2. WORK W; //Line 1
    WORK Y(W); //Line 2
    Which member function, out of Function 1, Function 2, Function 3 and Function 4 shown in the above definition of class WORK will be called on execution of statement written as Line 2? What is this function specifically known as out of destructor or copy constructor or default constructor?

Аnswer:

  1. Function 1 is called, when the scope of an object gets over. It is called destructor.
  2. Function 4 will be called and this function is referred to as copy constructor.

Question 35:
Answer the questions (i) and (ii) after going through the following class: Delhi 2009C

class Factory
{
char Name[20]; 
int Workers; 
public:
Factory()    //Function 1
{
strcpy(Name, "Default”); 
Workers = 0;
}
void Details() //Function 2
{
cout<<Name<<endl<<Workers<<endl;
}
Factory(char*act_Name,int No);  //Function 3 
Factory(Factory & F);   //Function 4
};
  1. In object oriented programming, what is function 4 referred as? Also, write a statement which will invoke this function.
  2. In object oriented programming, which concept is illustrated by Function 1, Function 3 and Function 4 together?

Аnswer:

  1. Function 4 is referred to as copy constructor.
    The statement to invoke it as follows:
    Factory F2(“ABC”, 101);
    Factory F3(F2); //Invoking Function 4
  2. Function 1, Function 3 and Function 4 illustrate the concept of constructor overloading; i.e. polymorphism

Question 36:
Answer the questions (i) and (ii) after going through the following class: All India 2009

class Job 
{
int JobId; 
char JobType; 
public:
∼Job()    //Function 1
{
cout<<"Resigned"<<endl;
}
Job()    //Function 2
{
JobId = 10; JobType = 'T';
}
void TellMe()   //Function 3
{
cout<<JobId<<":"<<JobType<<endl;
}
Job(Job &J)    //Function 4
{
JobId = J.JobId+10;
JobType = J.JobType+1;
}
};
  1. Which member function out of Function 1, Function 2, Function 3 and Function 4 shown in the above definition of class Job is called automatically, when the scope of an object gets over? Is it known as constructor or destructor or overloaded function or copy constructor?
  2. Job P; // Line 1
    Job Q(P1 ; // Line 2
    Which member function out of Function 1, Function 2, Function 3 and Function 4 shown in the above definition of class Job will be called on execution of statement written as Line 2? What is this function specifically known as out of destructor or copy constructor or default constructor?

Аnswer:

  1. Function 1 is called, when the scope of an object getsover. It is called destructor.
  2. Function 4 is called, when Line 2 is executed. It is called copy constructor.

Question 37:
Rewrite the following C++ program code after removing the syntax error(s) (if any). Underline each correction. Delhi 2009

#inClude<iostream.h> 
#include<stdio.h> 
class Employee 
{
int EmpId=901; 
char EName[20]; 
public
Employee(){} 
void Joining()
{
cin>>EmpId; 
gets(EName);
}
void List()
{
cout<<EmpId<<":"<<EName<<endl;
}
};
void main()
{
Employee E;
Joining.E();
E.List();
}

Аnswer:

#include<iostream.h> 
#include<stdio.h>
class Employee 
{
int Empld:   //cannot initialise data member here
char EName[20]; 
public:    //: symbol is required after public 
Employee(){EmpId = 901;} 
void Joining()
{ 
cin>>EmpId; 
gets(EName);
}
void List() 
{
cout<<EmpId<<":"<<EName<<endl;
}
};
void main()
{
Employee E;
E.Joining(); //object name is used before members 
E.List();
}

Question 38:
Rewrite the following program after removing the syntactical error(s) (if any). Underline each correction. All India 2009

#include<iostream.h> 
#include<stdio.h> 
class MyStudent 
{
int StudentId=1001; 
char Name[20]; 
public
MyStudent(){}
void Register() 
{
cin>>StudentId;
gets(Name);
}
void Display()
{
cout<<StudentId<<":"<<Name<<endl;
}
};
void Main()
{
Mystudent MS;
Register.MSC();
MS.Display();
}

Аnswer:

#include<iostream.h>
#1nclude<stdio.h> 
class MyStudent
{
int Studentld; 
char Name[20]; 
public:
MyStudent(){Studentld=1001;}
void Register()
{
cin>>StudentId; 
gets(Name);
}
void Display!)
{
cout<<StudentId<<":"<<Name<<endl;
}
};
void main()
{
MyStudent MS;
MS.Reaister();
MS.Display();
}

4 Marks Questions

Question 39:
Define a class CABS in C++ with the following specification: Delhi 2014
Data members

  • CNo – to store Cab No
  • Type – to store a character ‘A’, ‘B’ or ‘C as City Type
  • PKM – to store per Kilometre charges
  • Dist – to store Distance travelled (in KM)

Member functions

  • A constructor function to initialise Type as ‘A’ and CNo as ‘1111’
  • A function Charges( ) to assign PKM as per the following table:
  • A function Register( ) to allow administrator to enter the values for CNo and Type. Also, this function should call Charges( )to adding PKM charges.
  • A function ShowCab( ) to allow user to enter the value of Dist and display CNo, Type, PKM, PKM Dist (as Amount) on screen.
TypePKM
‘A’25
‘B’20
‘C15

Аnswer:

class CABS 
{
int CNo; 
char Type; 
float PKM; 
float Dist; 
public;
CABS()
{
 Type = 'A';
 CNo = 1111;
}
void Charges!)
{
if(Type == 'A')
PKM = 25;
else if(Type == 'B')
PKM = 20;
else if(Type == 'C')
PKM = 15;
}
void Register()
{
cout<<"Enter value for CNo:"; 
cin>>CNo;
cout<<"Enter value for Type:"; 
cin>>Type;
Charges();
}
void ShowCab()
{
cout<<"Enter the value of Distance:”; cin>>Dist; 
cout<<"\nCab Number: "<<CNo; 
cout<<"\nType:"<<Type; 
cout<<"\nPer Kilometre Charges:"<<PKM; 
cout<<"\nAmount:"<<PKM*Dist;
}
};

Question 40:
Define a class Tourist in C++ with the following specification: All India 2014
Data members

  • CNo – to store Cab No
  • CType – to store a character A B or C as City Type
  • PerKM – to store per Kilometre charges
  • Distance – to store Distance travelled (in KM)

Member functions

  • A constructor function to initialise CType as A and CNo as ‘0000’
  • A function CityChargesO to assign PerKM as per the following table:
  • A function RegisterCab( ) to allow administrator to enter the values for CNo and CType. Also, this function should call CityChargesO to assign PerKM Charges.
  • A function Display( ) to allow user to enter the value of Distance and display CNo, CType, PerKM, PerKM*Distance (as Amount) on screen.
CTypePerKM
A20
B18
C15

Аnswer:

class Tourist 
{
int CNo; 
char CType; 
float PerKM; 
float Distance; 
public:
Tourist()
{
CType = 'A';
CNo = 0000; 
}
void CityCharges()
{
if(CType == 'A')
PerKM = 20;
else if(CType == 'B')
PerKM = 18;
else if(CType == 'C')
PerKM = 15;
}
void RegisterCab()
{
cout<<"Enter the Cab Number:"; 
cin>>CNo;
cout<<"Enter the Cab Type:"; 
Cin>>CType;
CityCharges();
}
void Display()
{
cout<<"Enter the Distance:";
cin>>Distance;
cout<<"Registered details are\n"; 
cout<<"Cab Number:"<<CNo<<endl; 
cout<<"Cab Type:"<<CType<<endl; 
cout<<"Charges per km :"<<PerKM<<endl;
cout<<"Amount:"<<PerKM*Distance<<endl;
}
};

Question 41:
Define a class CONTEST in C + + with the following description: All India 2014C
Private Data Members
Eventno – integer
Description – char (30)
Score – integer
qualified –  char
Public Member functions

  • A constructor to assign initial values Eventno as 11, Description as “School level”, Score as 100, qualified as ‘N’.
  • Input( )-To take the input for Eventno, description and score.
  • Award (int cutoffscore)- To assign qualified as ‘Y’, if score is more than the cutoffscore that is passed as argument to the function, else assign qualified as ‘N’.
  • Displaydata( )-to display all data members.

Аnswer:

class CONTEST
{
private:
int Eventno; 
char Description[30]; 
int Score; 
char qualified; 
public:
CONTEST()
{
Eventno=11;
Description-"School level";
Score-100;
qua1ified='N';
}
void input()
{
cout<<”Enter the event no, description and score"; 
cin>>Eventno>>Description; 
cin>>Score;
}
void Award(int cutoffscore) 
{
if(score>cutoffscore) 
qualified='Y'; 
else
qualified='N';
}
void Displaydata()
{
cout<<"Eventno:"<<Eventno; 
cout<<endl; 
cout<<"Description:";<<Description<<endl; 
cout<<"Score:"<<Score<<endl; 
cout<<"Qualified:"cout<<qualified<<endl;
}
};

Question 42:
Define a class Bus in C++ with the following specifications: HOTS; All India 2013
Data members

  • Busno – to store Bus Number
  • From – to store Place name of origin
  • To – to store Place name of destination
  • Type – to store Bus Type such as ‘O’ for ordinary
  • Distance – to store the Distance in Kilometre
  • Fare – to store the Bus Fare

Member functions

  • A constructor function to initialise Type as ‘O’ and Freight as 500.
  • A function CalcFare( ) to calculate Fare as per the following criteria:
  • A function Allocate! ) to allow user to enter values for Busno, From, To, Type and Distance. Also, this function should call CalcFare( ) to calculate Fare.
  • A function Show( ) to display the content of all the data members on screen.
TypePKM
‘O’15* Distance
‘E’20* Distance
‘L’24* Distance

Аnswer:

class Bus 
{
int Busno; 
char From[25]; 
char To[25]; 
char Type; 
float Distance; 
float Fare; 
public:
Bust()
{
Type = ' '; Fare = 500;
}
void CalcFare()
{
if(Type == ' ')
{
Fare = 15*Distance;
}
else if(Type == 'E')
{
Fare = 20*Distance;
} 
else if(Type == 'L')
{ 
Fare = 24*Distance;
}
}
void Allocated()
{
cout<<"Enter the values for Busno, From, To, Type and Distance";
cin>>Busno; 
cin>>From; 
cin>>To; 
cin>>Type; 
cin>>Distance;
Call C Fared();
}
void Show()
{
cout<<"\nBus No:"<<Busno; 
cout<<"\nFrom:"<<From; 
cout<<"\nTo:"<<To; 
cout<<"\nType:"<<Type; 
cout<<"\nDistance:"<<Distance;
cout<<"\nFare:"<<Fare;
}
};

Question 43:
Define a class Tourist in C++ with the following specification: Delhi 2013
Data members

  • Carno-to store Bus No
  • Origin-to store Place name
  • Destination-to store Place name
  • Type-to store Car Type such as ‘E’ for Economy
  • Distance-to store the Distance in Kilometere
  • Charge-to store the Car Fare

Member functions

  • A constructor function to initialise Type as ‘E’ and Freight as 250
  • A function CalcChargef) to calculate Fare as per the following criteria:
  • A function Enter( ) to allow user to enter values for Carno, Origin, Destination, Type and Distance. Also, this function should call CalcCharge( ) to calculate Fare.
  • A function Show( ) to display the content of all the data members on screen.
TypeCharge
‘E’16* Distance
‘A’22* Distance
‘L’30* Distance

Аnswer:

class Tourist 
{
int Carno;
char Origin[20];
char Destination[20];
char Type;
float Distance;
float Charge;
public:
Tourist()
{
Type='E';
Charge=250;
}
void CalcCharge()
{
if(Type=='E')
Charge=16*Distance; 
else if(Type=='A') 
Charge=22*Distance; 
else if(Type='L') 
Charge=30*Distance;
}
void Enter() 
{
cout<<"Enter Carno, Origin, Type, Destination and Distance"; 
cin>>Carno; 
gets(Origin); 
gets(Destination); 
cin>>Type>>Distance; 
CalcCharge();
}
void Show()
{
cout<<"Car No:"<<Carno; 
cout<<"0rigin;"<<0rigin; 
cout<<"Destination:"<<Destination; 
cout<<"Type:"<<Type; 
cout<<" Distance:"<<Distance; 
cout<<"Charge:"<<Charge;
}
};