create a sub class of car class and name it as truck. the truck class has the following fields and methods. a) int weight; b) double get sale price (); //if weight > 3000, 40% discount. otherwise, 25% discount.
Answers
Complete Question:
a) Create a supper class called Car, which has the following fields and methods:
- int speed;
- double regularPrice;
- string color;
- double getSalePrice() // will return the regular price value
b) Create a sub class of Car class and name it as Truck which has the following fields and methods:
- int weight;
- double get sale price () //if weight > 3000, 40% discount. otherwise, 25% discount.
Program in C++:
#include<iostream>
#include<string.h>
using namespace std;
class Car
{
protected:
int speed;
double regularPrice;
string color;
public:
Car(int s, double r, string c)
{
speed = s;
regularPrice = r;
color = c;
}
double getSalePrice()
{
return regularPrice;
}
};
class Truck : public Car
{
private:
int weight;
public:
Truck(int s, double r, string c, int w) : Car(s, r, c)
{
weight = w;
}
double getSalePrice()
{
double discount;
if(weight > 3000)
{
discount = regularPrice * 40.0 / 100.0;
}
else
{
discount = regularPrice * 25 / 100;
}
return (regularPrice - discount);
}
};
int main()
{
int speed, weight;
double regularPrice;
string color;
cout<<"Enter the Speed: ";
cin>>speed;
cout<<"Enter the Regular Price: ";
cin>>regularPrice;
cout<<"Enter the Color: ";
cin>>color;
cout<<"Enter the weight: ";
cin>>weight;
Truck T(speed, regularPrice, color, weight);
cout<<"Sale Price = "<<T.getSalePrice();
return 0;
}