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

in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
In C language, Write a program called minishell that creates two child processes: one to execute...
In C language, Write a program called minishell that creates two child processes: one to execute 'ls -al' and the other to execute ‘grep minishell.c’. After the forks, the original parent process waits for both child processes to finish before it terminates. The parent should print out the pid of each child after it finishes. The standard output of 'ls -al' process should be piped to the input to the 'grep minishell.c' process. Make sure you close the unnecessary open...
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...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must create a list. The list is composed of integer double pairs. There is one pair per line. The integers are in sequence (0, 1, 2, 3, ...) beginning with zero and ending with some random value between 512 and 1024. The doubles should be random values between 50.000 and 90.000. The doubles only have 3 decimal places. The file should look like this (of...
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 !=...
In C++ write a program with a base class thet has a pure virtual function SALARY,...
In C++ write a program with a base class thet has a pure virtual function SALARY, and two derived classes. In the first derived class salary is increased by 20%, in the second derived class salary is increased by 30%
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT