In C++ console program Create a class named Vehicle. The class has the following
ID: 3818306 • Letter: I
Question
In C++ console program
Create a class named Vehicle. The class has the following five member variables:
• Vehicle Name
• Vehicle number
• Sale Tax
• Unit price
• Total price
Include set (mutator) and get (accessor) functions for each field except the total price field. The set function prompt the user for values for each field. This class also needs a function named computePrice() to compute the total price (quantity times unit price + salesTax) and a function to display the field values. [Note: Sale tax is 10%, sale tax = Unit price * saleTax)
Create a subclass named VehicleOrder that overrides computePrice() function by adding a shipping and handling charge of $12.00.
Write an application named UseVehicle that instantiates an object of each of these classes.
Prompt the user for data for the Vehicle object, and display the results, then prompt the user for data for the VehicleOrder object, and display the results.
Explanation / Answer
//UseVehicle.CPP
#include <iostream.h>
#include<conio.h>
class Vehicle { //main class
protected:
int Vnumber;
char* Vname;
float UnitPrice,SaleTax,TotalPrice;
public:
void set() {
cout<<" Enter Vehicle Name:";
cin>>Vname;
cout<<" Enter Vehical Number :";
cin>>Vnumber;
cout<<" Enter Sale Tax, Unit Price :";
cin>>SaleTax>>UnitPrice;
getch();
}
void get() {
cout << "Vehicle Name :"<<Vname <<endl;
cout << "Vehicle Number :"<<Vnumber <<endl;
cout << "Sale Tax :"<<SaleTax <<endl;
cout<<"Unit Price :"<<UnitPrice<<endl;
// return 0;
getch();
}
void computePrice()
{
SaleTax=UnitPrice*0.1;
TotalPrice=UnitPrice +SaleTax;
cout<<" Total Price :"<<TotalPrice;
}
};
class VehicleOrder: public Vehicle { // Subclass
Protected :
int SC=12,HC=12;//SC=Shipping charge and HC=Handling charge
public:
void computePrice() // Function Override
{
SaleTax=UnitPrice*0.1;
TotalPrice=UnitPrice +SaleTax+SC+HC;
cout<<" Total Price :"<<TotalPrice;
}
};
// Main function for the program
void main( ) {
clrscr();
Vehicle v; // object of main class
VehicleOrder v1; //object of subclass
v.set();
v.get();
v.computePrice();
v1.set();
v1.get();
v1.computePrice();
getch();
}