COMPUTER SCIENCE CONSTRUCTORS AND 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       less than 20
Number_of_buses            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 the  content of all the data members on the screen.

Ans:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class TravelPlan
{ long PlanCode;
char Place[21];
int Number_of_travellers,Number_of_buses;
public:
TravelPlan( )
{ PlanCode=1001;
strcpy(Place,"Agra");
Number_of_travellers=5;
Number_of_buses=1;
}
void NewPlan( )
{ cout<<"\nEnter the Plan Code: ";
cin>>PlanCode;
cout<<"\nEnter the Place to Travel: ";
gets(Place);
cout<<"\nEnter the Number of Travellers: ";
cin>>Number_of_travellers;
if(Number_of_travellers>=40)
Number_of_buses=3;
else if(Number_of_travellers>=20)
Number_of_buses=2;
else
Number_of_buses=1;
}
void ShowPlan( )
{ cout<<"\nThe Plan Code: "<<PlanCode;
cout<<"\nThe Place of Travel: "<<Place;
cout<<"\nNumber of Travellers: “<<Number_of_travellers;
cout<<"\nNumber of Buses: “ <<Number_of_buses;
}
};
void main( )
{ clrscr( );
TravelPlan T;
T.NewPlan( );
T.ShowPlan( );
getch();
}

 

CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )