## 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
Member Functions:
• 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:

• FNo       - Data member for storing flat number
• FCatg     - Data member for storing flat category
• FRate     - Data member for storing flat rate per square feet
• Area       - Data member for storing Area covered.
Public Members
• FCost( )   -  A function to calculate and return cost of flat as follows:
 FCatg FCost LIG FRate * Area MIG FRate * 1.5 * Area HIG FRate * 3 * Area
• Register( )  - A function to enter values FNo, FCatg, FRate and Area
• Display( )   - A function to display FNo, FCatg, FRate, Area and Cost [Cost to be displayed by calling function FCost( )]

``` class Flat
{
int FNo;
char FCatg[5];
float FRate;
float FArea;

public:
float FCost( )
{
float retval = 0.0;
if (strcmp(FCatg, "LIG") == 0)
retval = FRate * Area;
else if (strcmp(FCat, "MIG") == 0)
retval = FRate * 1.5 * Area;
else if (strcmp(FCatg, "HIG") == 0)
retval = FRate * 3.0 * Area;
return retval;
}

void Register()
{
cout << "Enter Flat Num, Flat Category (LIG, MIG or HIG), Rate, Area:";
cin >> FNo;
gets(FCatg);
cin >> FRate >> Area;
}

void Display()
{
cout << "Flat Number: " << FNo
<< "\nCategory: " << FCatg
<< "\nRate : " << FRage
<< "\nArea : " << FArea
<< "\nCost : " << FCost() << endl;
}
};```

Q3(CBSE 2009): Define a class HOTEL in C++ with the following description:
Private Members
• Rno          -Data Member to store Room No
• Name       -Data Member to store customer Name
• Tariff        -Data Member to store per day charge
• NOD       -Data Member to store Number of days
• CALC     -A function to calculate and return amount as NOD*Tariff and
if the value of NOD*Tariff is more than 10000 then as 1.05*NOD*Tariff
Public Members:
Checkin( )  -A function to enter the content RNo,Name, Tariff and NOD
Checkout() -A function to display Rno, Name, Tariff, NOD and Amount [Amount to be displayed by calling function CALC( )]

``` #include <iostream.h>

class HOTEL
{
unsigned int Rno;
char Name[25];
unsigned int Tariff;
unsigned int NOD;

int CALC()
{
int x;
x = NOD*Tariff;
if(x>10000)
return(1.05*NOD*Tariff);
else
return(NOD*Tariff);
}

public:
void Checkin()
{
cin>>Rno>>Name>>Tariff>>NOD;
}

void Checkout()
{
cout <<"Room No:" << Rno
<<"Name :" << Name
<< "Tarrif: "<< Tariff
<<"NOD: " << NOD
<<"Amount:" << CALC();
}
};

void main()
{
HOTEL h1;
h1.Checkin();
h1.Checkout();
}```
` `
```
```
Q4(CBSE 2010): Define a class ITEM in C++ with the following description:
Private Members:
• Code of type integer (Item Code)
• IName of type string (Item Name)
• Price of type float (Price of each item)
• Qty of type integer (Quantity of item in stock)
• Offer of type float (Offer percentage on the item)
• A member function GetOffer( ) to calculate Offer percentage as per the following rule:
• if Qty <= 50     Offer is 0
• if 50 < Qty <= 100    Offer is 5
• if Qty > 100   Offer is 10
Public Members:
• A function GetStock( ) to allow user to enter values for Code, IName, Price, Qty and call function GetOffer( ) to calculate the offer
• A function ShowItem( ) to allow user to view the content of all the data members
``` class Item
{
int ItemCode;
char IName[25];
float Price;
int Qty;
float Offer;
void GetOffer( )
{
if (Qty <= 5) Offer = 0.0;
else if (Qty > 50 && Qty <= 100) Offer = 5.0;
else if (Qty > 100) Offer = 10.0;
}
public:
void GetStock( )
{
cout << "Enter Item Code: "; cin >> ItemCode;
cout << "Enter Item Name: "; gets(IName);
cout << "Enter Price: "; cin >> Price;
cout << "Enter Qty: "; cin >> Qty;
GetOffer();
}```
```   void ShowItem()
{
cout << ItemCode << "\t" << IName << "\t" << Price << "\t" << Qty << "\t" << Offer;
}
};```