Computer Science (C++) - Class Design
Questions Asked in Board Papers on Class Design
Q1(CBSE 2013): Define a class Tourist in C++ with the following specifications:Data Members:
- CarNo - to store bus number
- Origin - to store Place name
- Destination - to store Place name
- Type - to store Car Type such as 'E' for Economy
- Distance - to store the Distance in Kilometers
- Charge - to store the Car Fare
- A constructor function to initialize Type as 'E' and Freight as 250
- A function CalcCharge( )
to calculate Fare as per the following
criteria:
Type Charge 'E' 16*Distance 'A' 22*Distance 'L' 30*Distance
- A function Enter( ) to allow user to enter values for CarNo, Origin, Destination, Type and Distance. Also this function should call CalcCharge( ) to calculate Fare.
- A function Show( ) to display the content of all the data members on screen.
const int BUFFSIZE = 40; class Tourist { // private data members int carNum; char origin[BUFFSIZE]; char destination[BUFFSIZE]; char carType; int distance; int charges; public: // member functions // constructor Tourist() { carType = 'E'; charges = 250; } // calculate charges void CalcCharges() { if (Type == 'E') { charges = 16*distance; } else if (Type == 'A') { charges = 22*distance; } else if (Type == 'C') { charges = 30*distance; } }; // enter values void Tourist::Enter() { cout << "Enter CarNo, Origin, Destination, Type, Distance:"; cin >> carNum; gets(origin); gets(destination); cin >> carType >> Distance; CalcCharges(); } // show/display details void Show() { cout << "Car No: " << carNum << "\nOrigin: " << origing << "\nDestination: " << destination << "\nType: " << carType << "\nDistance: " << distance << "\nCharges: " << charges << endl; } };
Q2(CBSE 2012): Define a class Flat
in C++ with the following description:
Private Members:
Private Members: