Saturday 04th May 2024
REGISTRATION
Online Fashion Store

CBSE Computer Science - Revision Tour(Solved)

CBSE Guess > eBooks > Class XII > CBSE Computer Science Constructors And Destructors Solved Revision Tour By Mr. Ravi Kiran

COMPUTER SCIENCE CONSTRUCTORS AND DESTRUCTORS

Previous Index Next

DELHI 2007:

2.a) Differentiate between Constructor and Destructor function in context of lasses
and Objects Using C++?

2

Ans:
Constructor: A constructor is used to intitialize the objects of that class type with a legal initial value.If a class has a constructor,each object of that class will be initialized before any use is made of the object.

(A member function with the same name as its class is called Constructor and it
is used to initialize the objects of that class type with a legal initial value. )

Destructor: A destructor is used to destroy the objects that have been created by a
constructor. A destructor destroys the values of the object being destroyed.

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

class Maths
{ char Chapter[20]
int Marks;
public:
Maths() //Member Function 1
{ strcpy (Chapter, “Geometry”);
Marks=10;
cout <<”Chapter Initialised “;
}
-Maths() //Member Functions 2
{ cout<<”Chapter Over”;
}
};

Constructor Destructor
Purpose: Is used to intitialize the objects of that class type with a legal initial value
Purpose: Is used to destroy the objects that have been created by a constructor
Name: The name of the class
Name:The name of the class preceded by a ~.
Calling: It will be
called automatically at the time of creation of the object. Ie Implicite calling
Calling: It will be called automatically at the time of destruction of an object.Ie mplicite calling
Return Type: No return type not even void
Return Type: No return type not even void

(i) Name the specific features of class shown by member Function 1 and Member 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 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 below.

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() whichcalculates 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.

4

Ans:

#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<iostream.h>
class Tour
{ char TCode[21];
int NoofAdults,NoofKids,Kilometres;
float TotalFare;
public:
Tour( )
{ strcpy(TCode,"NULL");
NoofAdults=NoofKids=Kilometres=TotalFare=0;
}
void AssignFare( )
{ if(Kilometres>=1000)
TotalFare=NoofAdults*500+NoofKids*250;
else if(Kilometres>=500)
TotalFare=NoofAdults*300+NoofKids*150;
else
TotalFare=NoofAdults*200+NoofKids*100;
}
void EnterTour( )
{ cout<<"\nEnter the Tour Code: ";
gets(TCode);
cout<<"\nEnter the Number of Adults: ";
cin>>NoofAdults;
cout<<"\nEnter the Number of Kids: ";
cin>>NoofKids;
cout<<"\nEnter the Number of Kilometres: ";
cin>>Kilometres;
AssignFare( );
}
void ShowTour( )
{ cout<<"\nThe Tour Code: "<<TCode;
cout<<"\nThe Number of Adults:” <<NoofAdults;
cout<<"\nThe Number of Kids: "<<NoofKids;
cout<<"\nThe Number of Kilometres: “ <<Kilometres;
cout<<"\n\nThe Total Fare: "<<TotalFare;
}
};
void main( )
{ clrscr();
Tour T;
T.EnterTour( );
T.ShowTour( );
getch();
}

 

Previous Index Next

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