Chapter – 5. CONSTRUCTORS & DESTRUCTORS

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

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

(ii) How would Member Function 1 and Member 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 Maths. Member function 2 will be executed at the time of destruction of an object of class Maths.

2. c. Define a class Tour in C++ with the description given below4

Private Members:

TCode of type string
No of Adults of type integer
No of Kids of type integer
Kilometers of type integer
TotalFare of type float

Public Members:

• A constructor to assign initial values as follows:
TCode with the word “NULL”
No of Adults as 0
No of Kids as 0
Kilometers 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 Kid the above Fare will be 50% of the Fare mentioned in the above table For Example:
If Kilometers is 850, Noofadults =2 and NoofKids =3
Then TotalFare should be calculated as
Numof Adults *300+ NoofKids *150 i.e., 2*300+ 3 *150 =1050

• A function EnterTour() to input the values of the data members TCode, NoofAdults, NoofKids and Kilometers ; and invoke the AssignFare() function.

• A function ShowTour() which displays the content of all the data members for a Tour.