Question

In: Computer Science

Write a C++ program that creates a base class called Vehicle that includes two pieces of...

Write a C++ program that creates a base class called Vehicle that includes two pieces of information as data members, namely:

  • wheels (type int)
  • weight (type float)

Program requirements (Vehicle class):

  • Provide set and a get member functions for each data member.
  • Your class should have a constructor with two parameters (one for each data member) and it must use the set member functions to initialize the two data members.
  • Provide a pure virtual member function by the name displayData() that returns void.

Program requirements (Car class):

  • Create a derived class called Car that inherits from Vehicle.
  • The Car class must have a new data member called passengerLoad (type int).
  • The Car class must have a new setPassagerLoad member function.
  • The Car class must have a constructor that calls the base class constructor and additionally it must use the setPassengerLoad to initialize the new data member.
  • Provide a virtual member function by the name displayData() to print all the car data.

Program requirements (Truck class):

  • Create a derived class called Truck that inherits from Vehicle.
  • The Truck class must have a new data member called payLoad (type float).
  • The Truck class must have a new setPayLoad member function.
  • The Truck class must have a constructor that calls the base class constructor and additionally it must use the setPayLoad to initialize the new data member.
  • Provide a virtual member function by the name displayData() to print all the truck data.

Write a main program that demonstrates the polymorphism capabilities, by:

  • Creating an object of type Car with the numbers (4, 2.5, 5)
  • Creating an object of type Truck with the numbers (8, 4.4, 9.5)
  • Create ONE pointer to Vehicle
  • Make the pointer point to the Car object
  • Call the function displayData() to display the car information
  • Make the pointer point to the Truck object
  • Call the function displayData() to display the truck information

The following is an example of how your program should run:

Car information

Wheels: 4

Weight: 2.5

PassengerLoad: 5

Truck information

Wheels: 8

Weight: 4.4

PayLoad: 9.5

Solutions

Expert Solution

/*

compile this code in dev c++ you change main method signature if you are using another compiler

*/

#include <iostream>
using namespace std;
class Vehicle
{
   int wheels;
   float weight;
   public:
       Vehicle(int _wheels,float _weight)
       {
           setWheels(_wheels);
           setWeight(_weight);
       }
       void setWheels(int w)
       {
           wheels=w;
       }
       int getWheels()
       {
           return wheels;
       }
       void setWeight(float _weight)
       {
           weight=_weight;
       }
       float getWeight()
       {
           return weight;
       }
       virtual void displayData()
       {
           cout<<"Wheels: "<<getWheels()<<endl;
           cout<<"weight: "<<getWeight()<<endl;
       }
};
class Car:public Vehicle
{
   int passengerLoad;
   public:
       Car(int _wheels,float _weight,int _passengerLoad):Vehicle(_wheels,_weight)
       {
           setPassengerLoad(_passengerLoad);
       }
       void setPassengerLoad(int pass)
       {
           passengerLoad=pass;
       }
       int getPassengerLoad()
       {
           return passengerLoad;
       }
       virtual void displayData()
       {
           cout<<"Car Information"<<endl;
           cout<<"Wheels: "<<getWheels()<<endl;
           cout<<"weight: "<<getWeight()<<endl;
           cout<<"PassengerLoad: "<<getPassengerLoad()<<endl;
       }
};
class Truck: public Vehicle
{
   float payLoad;
   public:
       Truck(int _wheels,float _weight,float _payLoad):Vehicle(_wheels,_weight)
       {
           setPayLoad(_payLoad);
       }
       void setPayLoad(int pass)
       {
           payLoad=pass;
       }
       int getPayLoad()
       {
           return payLoad;
       }
       virtual void displayData()
       {
           cout<<"Truck Information"<<endl;
           cout<<"Wheels: "<<getWheels()<<endl;
           cout<<"weight: "<<getWeight()<<endl;
           cout<<"PayLoad: "<<getPayLoad()<<endl;
       }
};
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
   Car car(4,2.5,5);
   Truck truck(8,4.4,9.5);
   Vehicle* ptrvehicle;
   ptrvehicle=&car;
   ptrvehicle->displayData();
   ptrvehicle=&truck;
   ptrvehicle->displayData();
   return 0;
}

output:-


Related Solutions

Write a program that creates a Singleton class to connect to two databases. The program must...
Write a program that creates a Singleton class to connect to two databases. The program must provide two instances of this class. One instance for connecting to MySQL database and the other for connecting to Oracle database.
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...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
C++ Write a program that creates two rectangular shapes and then animates them. The two shapes...
C++ Write a program that creates two rectangular shapes and then animates them. The two shapes should start on opposite ends of the screen and then move toward each other. When they meet in the middle of the screen, each shape reverses course and moves toward the edge of the screen. The two shapes keep oscillating and bouncing off of each other in the middle of the screen. The program terminates when the shapes meet each other in the middle...
C++ Define a base class called Person. The class should have two data members to hold...
C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and !=...
Write a program (in C, or Java, or C++, or C#) that creates three new threads...
Write a program (in C, or Java, or C++, or C#) that creates three new threads (besides the already existing main thread) and synchronizes them in such a way that each thread displays it's thread id in turn for 5 iterations. The output of the program should look like this: Thread 1 - iteration no. 1 Thread 2 - iteration no. 1 Thread 3 - iteration no. 1 Thread 1 - iteration no. 2 Thread 2 - iteration no. 2...
Create a class called Vehicle that includes four instance variables:      name, type,     tank size and...
Create a class called Vehicle that includes four instance variables:      name, type,     tank size and average petrol consumption. Provide 2 constructors, the first takes name and type as parameter, the second takes four parameters for the four instance variables. (2 pt) Provide also a method called distancePerTank that calculate the average distance that a vehicle can travel if the tank is full (multiplies the tank size by the average petrol consumption), then returns the value. (2 pt) Provide a...
Write C++ a program that shows a class called gamma that keeps track of how many...
Write C++ a program that shows a class called gamma that keeps track of how many objects of itself there are. Each gamma object has its own identification called ID. The ID number is set equal to total of current gamma objects when an object is created. Test you class by using the main function below. int main()    {    gamma g1;    gamma::showtotal();    gamma g2, g3;    gamma::showtotal();    g1.showid();    g2.showid();    g3.showid();    cout <<...
C++ Assignment 1) Write a C++ program specified below: a) Include a class called Movie. Include...
C++ Assignment 1) Write a C++ program specified below: a) Include a class called Movie. Include the following attributes with appropriate types: i. Title ii. Director iii. Release Year iv. Rating (“G”, “PG”, “PG-13”, etc) - Write code that instantiates a movie object using value semantics as text: - Write code that instantiates a movie object using reference semantics: - Write the print_movie method code: - Write Constructor code: - Write Entire Movie class declaration
Task: Write a program that creates a class Apple and a tester to make sure the...
Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple  The class Apple DOES NOT HAVE a main method  Some of the attributes of Apple are o Type: A string that describes the apple. It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith o Weight: A decimal value representing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT