In: Computer Science
C++
The following is a specification of three classes:
Class Vehicle:
Attributes:
Age, an integer à The age of the vehicle
Price, a float à The price of the vehicle
Behaviors:
Vehicle() à default constructor sets age=0, and price=0.0
setAge() à Takes an integer parameter, returns nothing
setPrice() à Takes a float parameter, returns nothing
getAge() à Takes no parameters, returns the vehicle’s age
getPrice() à Takes no parameters, returns the vehicle’s price
End Class Vehicle
Class Car:
Attributes:
An object of type Car has all the attributes of an
object of type Vehicle
Additionally, Cars have attributes that Vehicles do not:
RaceCarStatus, a boolean à true or false
Behaviors:
An object of type Car has all the behaviors of an object of
type Vehicle
Additionally, Cars have behaviors that Vehicles do not:
Car() à default constructor sets RaceCarStatus=false
setRaceCarStatus() à Takes an boolean parameter, returns nothing
getRaceCarStatus() à Takes no parameters, returns the car’s race car status
End Class Car
Class Truck:
Attributes:
An object of type Truck has all the attributes of an
object of type Vehicle
Additionally, Trucks have attributes that Vehicles do not:
DieselTypeStatus, a boolean à true or false
Behaviors:
An object of type Truck has all the behaviors of an object of
type Vehicle
Additionally, Cars have behaviors that Vehicles do not:
Truck() à default constructor sets DieselTypeStatus=false
setDieselTypeStatus() à Takes a boolean parameter, returns nothing
getDieselTypeStatus() à Takes no parameters, returns the Truck’s diesel type status
End Class Truck
It would be a horrible waste of valuable programming time to redefine all of the functions in Car or Truck when many of them are already defined. This assignment is to reinforce the application of inheritance.
Base & Derived Classes
A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form:
class derived-class: access-specifier base-class
Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.
Consider a base class Shape and its derived class Rectangle as follows:
#include <iostream>
using namespace std;
// Base class
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
}
When the above code is compiled and executed, it produces the following result: Total area: 35
Please find attached program and output
/*********************************vehicle.cpp**************************/
#include <iostream>
using namespace std;
// Base class Vehicle
class Vehicle
{
private:
int age;
double price;
public:
Vehicle(){
this->age = 0;
this->price = 0.0;
}
//getter and setter
void setAge(int age){
this->age =
age;
}
void setPrice(double price){
this->price =
price;
}
int getAge(){
return
age;
}
double getPrice(){
return
price;
}
};
// Derived class Car
class Car: public Vehicle{
public:
Car(){
this->raceCarStatus = false;
}
//getter and setter
void setRaceCarStatus(bool
b){
this->raceCarStatus = b;
}
bool getRaceCarStatus(){
return
raceCarStatus;
}
//private instance variable
private:
bool
raceCarStatus;
};
//derived class Truck
class Truck : public Vehicle
{
public:
//constuctor
Truck(){
this->dieselTypeStatus = false;
}
//getter and setter
void setDieselTypeStatus(bool
b){
this->dieselTypeStatus = b;
}
bool getDieselTypeStatus(){
return
dieselTypeStatus;
}
private:
bool
dieselTypeStatus;
};
int main(void){
//create two instance of truck and car
Car car;
Truck truck;
//set variables
car.setAge(10);
car.setPrice(2000.0);
car.setRaceCarStatus(1);
truck.setAge(2);
truck.setPrice(30000.0);
truck.setDieselTypeStatus(0);
//print car information
cout<<"Car Age:
"<<car.getAge()<<endl;
cout<<"Car Price:
$"<<car.getPrice()<<endl;
cout<<"Race Car Status:
"<<car.getRaceCarStatus()<<endl;
//print Truck information
cout<<"Truck Age:
"<<truck.getAge()<<endl;
cout<<"Truck Price:
$"<<truck.getPrice()<<endl;
cout<<"Diesel Type Status:
"<<truck.getDieselTypeStatus()<<endl;
return 0;
}
/*********************output*******************/
Car Age: 10
Car Price: $2000
Race Car Status: 1
Truck Age: 2
Truck Price: $30000
Diesel Type Status: 0
-------------------------------
Please let me know if you have any doubt or modify the answer, Thanks :)