Question

In: Computer Science

C++ The following is a specification of three classes: Class Vehicle:       Attributes:       Age, an...

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

Solutions

Expert Solution

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 :)


Related Solutions

(JAVA) Create three classes, Vehicle, Truck, and Person. The Vehicle class has a make and model,...
(JAVA) Create three classes, Vehicle, Truck, and Person. The Vehicle class has a make and model, test emissions per mile, and a driver/owner (= Person object). (Each vehicle can only have one driver/owner.) The class Truck is a derived class from the Vehicle class and has additional properties load capacity in tons (type double since it can contain a fractional part) and a towing capacity in pounds (type int). Be sure that your classes have appropriate constructors, accessors, mutators, equals(),...
C++ Code Vehicle Class The vehicle class is the parent class of a derived class: locomotive....
C++ Code Vehicle Class The vehicle class is the parent class of a derived class: locomotive. Their inheritance will be public inheritance so reflect that appropriately in their .h files. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +setName(s:string):void +getName():string +getMap():char** +getSize():int +setMap(s: string):void +getMapAt(x:int, y:int):char +~vehicle() +operator−−():void +determineRouteStatistics()=0:void The class variables are as follows: • map: A 2D array of chars, it will represent the...
C++ Code Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive....
C++ Code Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive. Their inheritance will be public inheritance so reect that appropriately in their .h les. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +getSize():int +setName(s:string):void +getName():string +getMap():char** +setMap(s: string):void +getMapAt(x:int, y:int):char +~vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of chars, it will represent the map...
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a...
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a derived class: locomotive. Their inheritance will be public inheritance so react that appropriately in their .h les. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +setName(s:string):void +getName():string +getMap():char** +getSize():int +setMap(s: string):void +getMapAt(x:int, y:int):char +vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of chars, it will...
Enlistee and Private Classes Write an Enlistee class that keeps data attributes for the following pieces...
Enlistee and Private Classes Write an Enlistee class that keeps data attributes for the following pieces of information: Enlistee name Enlistee number Next, write a class named Private that is a subclass of the Enlistee class. The Private class should keep data attributes for the following information: Platoon number (an integer, such as 1, 2, or 3) Years of service (also an integer) Write the appropriate accessor and mutator methods for each class. Once you have written the classes, write...
C++ Code (I just need the dieselLocomotive Class) Vehicle Class The vehicle class is the parent...
C++ Code (I just need the dieselLocomotive Class) Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive. Their inheritance will be public inheritance so reflect that appropriately in their .h files. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +getSize():int +setName(s:string):void +getName():string +getMap():char** +setMap(s: string):void +getMapAt(x:int, y:int):char +~vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of...
C++ Consider a class Movie that information about a movie. The class has the following attributes:...
C++ Consider a class Movie that information about a movie. The class has the following attributes: • The movie name • The MPAA rating (for example, G, PG, PG-13, R) • Array of size 5 called Ratings, each index will hold the following. [0] The number of people that have rated this movie as a 1 (Terrible) [1] The number of people that have rated this movie as a 2 (Bad) [2] The number of people that have rated this...
Create a super class named Vehicle, which has the following attributes: VIN, engine size, Year, Speed....
Create a super class named Vehicle, which has the following attributes: VIN, engine size, Year, Speed. Check Add a subclass named Car, which has the following additional attributes: Wheel size, Model, Miles (OOD). Check Add another subclass to the vehicle superclass named Boat which has the following additional attributes: Hours, Gas type, Type (Fishing Boat, Deck boat, bowrider boat), and Check Write a method named DisplayInfo() in your main to display the information of the Vehicle. In this method print...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string • speed : float Create a FigherPlane class that inherits from the Airplane class and adds the following attributes: • numberOfMissiles : short
Given the following list of classes, attributes and methods, - identify which items are classes, which...
Given the following list of classes, attributes and methods, - identify which items are classes, which items are attributes and which items are methods; - identify which class each attribute and method belongs to; and - suggest a class hierarchy given your list of classes. *Note - no particular capitalization scheme is used in the list below to differentiate between classes, methods, and attributes. LandOnStatue, NumberOfLegs, Height, ShoeSize, Eat, Animal, Speak, WingSpan, Age, Peck, Sleep, Horse, LengthOfMane, Move, BeakLength, LengthOfTail,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT