OUTSIDE DELHI 2005:
1.a) Differentiate between a default constructer and copy constructer, giving suitable examples of each.
Ans: A default constructor also called as nonparameterized constructor will take no argument and initialize the object with the predefined values in that constructor,
Where as a copy constructor willtake an already created object of that class and stores that object values into the newly created object of that class. A copy constructor takes a reference to an object of the same class as an argument.
2.b) Answer the following questions (i)and (ii) after going through the following class.
class Exam
{ int Marks;
char Subject[20];
public:
Exam() //Function 1
{ strcpy(Subject,”Computer”);
Marks=0;
}
Exam(char S[]) //Function 2
{ strcpy(Subject,S);
Marks=0; }
Exam(int M) //Function 3
{ strcpy(Subject,”Computer”);
Marks=M;
}
Exam(char S[],int M) //Function4
{ Strcpy(Subject,P);
Marks=M;
}
};
(i) Write statements in C++ that would execute Function 3 and Function 4 of class Exam.
(let char name[20];
int X=60;
strcpy(name,”COMPUTERSCIENCE”); are declared in the program)
(i) Exam A(X);//Will execute Funciton 3
(ii) Exam B(name,X)//Will execute Function 4
(ii) 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).
2.c) Define a class Travel in C++ with thefollowing descriptions:
Private Members:
Travelcode of type long
Place of type character array(string)
Number_of_travellers of type integer
Number_of_buses of type integer
Public Members:
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
Ans:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class Travel
{ long TravelCode;
char Place[21];
int No_of_travellers,No_of_buses;
public:
Travel( )
{ TravelCode=201;
strcpy(Place,"Nainital");
No_of_travellers=5;
No_of_buses=1;
}
void NewTravel( )
{ cout<<"\nEnter the Travel Code: ";
cin>>TravelCode;
cout<<"\nEnter the Place to Travel: ";
gets(Place);
cout<<"\nEnter the Number of Travellers: ";
cin>>No_of_travellers;
if(No_of_travellers>=40)
No_of_buses=3;
else if(No_of_travellers>=20)
No_of_buses=2;
else
No_of_buses=1;
}
void ShowTravel( )
{ cout<<"\nThe Plan Code: "<<TravelCode;
cout<<"\nThe Place of Travel: "<<Place;
cout<<"\nNumber of Travellers: “ <<No_of_travellers;
cout<<"\nNumber of Buses: “<<No_of_buses;
}
};
void main( )
{ clrscr( );
Travel T;
T.NewTravel( );
T.ShowTravel( );
getch();
}
CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )