COMPUTER SCIENCE CONSTRUCTORS AND DESTRUCTORS

OUTSIDE DELHI: 2007:

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

class Science
{ char Topic[20] ;
int Weightage ;
public :Science () //Function 1
{ strcpy (Topic, “Optics”) ;
Weightage =30
cout<<”Topic Activated”;
}
~Science() //Function 2
{ cout<<”Topic Deactivated”; }
};

(i) Name the specific features of class shown by Function 1 and Function 2 in the above example.

2

Ans: Member function 1 is a (nonparameterized or default) constructor (,which will be executed automatically at the time of creation of an object of class Science). Member function 2 is a destructor (,which will be executed automatically at the time of destruction of an object of class Science).

(ii) How would Function 1 and Function 2 get executed ?

Ans: They will be executed automatically. Member function 1 will be executed at the time of creation of an object of class Science.Member function 2 will be executed at the time of destruction of an object of class Science.

2.c) Define a class Travel in C++ with the description given below :

Private Members:

T_Code                 of type string
No_ of_ Adults      of type integer
No _of _Children of type integer
Distance                of type integer
TotalFare              of type float

Public Members:

  • A constructor to assign initial values as follows:
  • TCode A constructor to assign initial values as follows:with the word “NULL”

No _of_ Adults as 0
No_ of_Children as 0
Distance as 0
TotalFare as 0

  • A function AssignFare() which calculates and assigns the value of the data member Totalfare as follows For each Adult
Fare (Rs) For Kilometers
500 >=1000
300 <1000 & >=500
200 <500

For each Child the above Fare will be 50% of the Fare mentioned in the above table

For Example: If Distance is 750, No_of_adults =3 and No_of_Children =2 .Then TotalFare should be calculated as Num_of _Adults *300+ No_of_Children *150 i.e., 3*300+ 2 *150 =1200

  • A function EnterTour() to input the values of the data members T_Code, No_of_Adults, No_of_Children and Distance ; and invoke the AssignFare() function.
  • A function ShowTravel() which displays the content of all the data members for a Travel.

#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<iostream.h>
class Travel
{ char T_Code[21];
int No_of_Adults,No_of_Children,Distance;
float TotalFare;
public:
Travel( )
{ strcpy(T_Code,"NULL");
No_of_Adults=No_of_Children=Distance= TotalFare=0;
}
void AssignFare( )
{ if(Distance>=1000)
TotalFare=No_of_Adults*500+No_of_Children* 250;
else if(Distance>=500)
TotalFare=No_of_Adults*300+No_of_Children* 150;
else
TotalFare=No_of_Adults*200+No_of_Children* 100;
}
void EnterTravel( )
{ cout<<"\nEnter the Travel Code: ";
gets(T_Code);
cout<<"\nEnter the Number of Adults: ";
cin>>No_of_Adults;
cout<<"\nEnter the Number of Children: ";
cin>>No_of_Children;
cout<<"\nEnter the Distance in Kilometres: ";
cin>>Distance;
AssignFare( );
}
void ShowTravel( )
{ cout<<"\nThe Travel Code: “ <<T_Code;
cout<<"\nThe Number of Adults: “ <<No_of_Adults;
cout<<"\nThe Number of Children: “ <<No_of_Children;
cout<<"\nThe Distance in Kilometres: "<<Distance;
cout<<"\n\nThe Total Fare: "<<TotalFare;
}
};
void main( ) {
clrscr();
Travel T;
T.EnterTravel( );
T.ShowTravel( );
getch();
}

4

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