Chapter – 5. CONSTRUCTORS & DESTRUCTORS

DELHI 2005

2.b. Answer the following questions (i) and (ii) after going through the following class.

class Test
{
char Paper[20];
int Marks public:
Test() //Function 1
{
strcpy(Paper,”Computer”);
Marks=0; } //Function 2
Test(char P[])
{
strcpy(Paper,P);
Marks=0; } //Function 3
Test(int M)
{
strcpy(Paper,”Computer”);
Marks=M;
}
Test(char P[],int M) //Function 4
{
strcpy(Paper,P); Marks=M;
}
};

(i) Which feature Object Oriented Programming is demonstrated using Function 1, Function 2, Function 3 and Function 4 in the above class text?

Ans: Function overloading (here it is constructor overloading).

(ii) Write statements in C++ that would execute Function 2 and Function 4 of class Text.

Ans: (let char name[20]; int X=60; strcpy(name,”COMPUTERSCIENCE”); are declared in the program)

(i) Test A(name); //Will execute Funciton 2
(ii) Test B(name,X); //Will execute Function 4

2.c. Define a class Travelplan in C++ with the following descriptions:

Private Members:

Plancode of type long
Place of type character array(string)
Number_of_travellers of type integer
Number_of_buses of type integer

Public Members:

*A constructer to assign initial values of PlanCode as 1001, Place as “agra”,Number_of_travellers as 5,Number_of_buses as 1

* A function NewPlan() which allows user to enter PlanCode, Place and Number_of travelers. Also, assign the value of Number_of_buses as per the following conditions:

Number_of_travellers Number_of_buses
Less than 20 1
Equal to or more than 20 and less than 40 2
Equal to 40 or more than 40 3

* A function ShowPlan() to display the3 content of all the data members on the screen.