Chapter – 5. CONSTRUCTORS & DESTRUCTORS

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:

* A constructer to assign initial values of TravelCode as 201, Place as “Nainital”, Number_of_travellers as 10, Number_of_buses as 1

* A function NewTravel() which allows user to enter TravelCode, 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 ShowTravel() 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 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();
}