0

If I have a program like this

#include <iostream>
#include <string>
using namespace std;
class Vehicle {
protected:
    int Number;
    int Year;
    string Make;
    string BodyStyle;
    string Model;
    string Color;
    int Cost;
public:
    Vehicle() {}
    Vehicle(int num, int year1, string make1, int bStyle1,
            int model1, int color1, int cost1)
    {
        Number = num;
        Year = year1;
        Make = make1;
        BodyStyle = bStyle1;
        Model = model1;
        Color = color1;
        Cost = cost1;
    }
    void display() {
        cout << "---This is Vehicle Class.---" << endl;
        cout << "Number: " << Number << endl;
        cout << "Year: " << Year << endl;
        cout << "Make: " << Make << endl;
        cout << "BodyStyle: " << BodyStyle << endl;
        cout << "Model: " << Model << endl;
        cout << "Color: " << Color << endl;
        cout << "Cost: " << Cost << endl;
    }
};

Vehicle class is implemented by three classes

  1. Truck Vehicle
  2. Motorcycle Vehicle
  3. Trailer Vehicle.

First class that implements Vehicle class.

class TruckVehicle : Vehicle {
protected:
    int Passengers;
    int Mileage;
    int GrossWeight;
    string PoweredBy;
    int TempGross;
public:
    TruckVehicle(int number, int year, string make, string model,
                string bStyle, string color, int cost,
                int passengers, int mileage, int gWeight,
                string pBy, int tGross)
    {
        Number = number;
        Year = year;
        Make = make;
        BodyStyle = bStyle;
        Model = model;
        Color = color;
        Cost = cost;
        Passengers = passengers;
        Mileage = mileage;
        GrossWeight = gWeight;
        PoweredBy = pBy;
        TempGross = tGross;
    }
    void display() {
        cout << "---This is TruckVehicle class.---" << endl;
        cout << "Number: " << Number << endl;
        cout << "Year: " << Year << endl;
        cout << "Make: " << Make << endl;
        cout << "BodyStyle: " << BodyStyle << endl;
        cout << "Model: " << Model << endl;
        cout << "Color: " << Color << endl;
        cout << "Cost: " << Cost << endl;
        cout << "Passenger: " << Passengers << endl;
        cout << "Mileage: " << Mileage << endl;
        cout << "GrossWeight: " << GrossWeight << endl;
        cout << "PoweredBy: " << PoweredBy << endl;
        cout << "TempGross: " << TempGross << endl;
    }
};

Second class that implements Vehicle class.

class MotorcycleVehicle : Vehicle {
protected:
    int Passengers;
    int Mileage;
public:
    MotorcycleVehicle(int number, int year, string make, string model,
                      string bStyle, string color, int cost,
                      int passenger, int mileage) 
    {
        Number = number;
        Year = year;
        Make = make;
        BodyStyle = bStyle;
        Model = model;
        Color = color;
        Cost = cost;
        Passengers = passenger;
        Mileage = mileage;
    }
    void display() {
        cout << "---This is Motor Cycle Class.---" << endl;
        cout << "Number: " << Number << endl;
        cout << "Year: " << Year << endl;
        cout << "Make: " << Make << endl;
        cout << "BodyStyle: " << BodyStyle << endl;
        cout << "Model: " << Model << endl;
        cout << "Color: " << Color << endl;
        cout << "Cost: " << Cost << endl;
    }
};

Third Class that implements Vehicle class.

class TrailerVehicle :  protected Vehicle {
protected:
    int GrossWeight;
public:
    TrailerVehicle() {
        GrossWeight = 0;
    }
    TrailerVehicle(int number, int year, string make, string model,
                   string bStyle, string color, int cost, int gWeight)
    {
        Number = number;
        Year = year;
        Make = make;
        BodyStyle = bStyle;
        Model = model;
        Color = color;
        Cost = cost;
        GrossWeight = gWeight;
    }
    void display() {
        cout << "---This is Trailer Vehicle Class.---" << endl;
        cout << "Number: " << Number << endl;
        cout << "Year: " << Year << endl;
        cout << "Make: " << Make << endl;
        cout << "BodyStyle: " << BodyStyle << endl;
        cout << "Model: " << Model << endl;
        cout << "Color: " << Color << endl;
        cout << "Cost: " << Cost << endl;
        cout << "GrossWeight: " << GrossWeight << endl;
    }
};

Trailer Vehicle class is implemented by TravelTrailerVehicle.

class TravelTrailerVehicle : TrailerVehicle {
protected:
    int Passengers;
    int Mileage;
    string PoweredBy;
    int BodyNumber;
    int Length;
public:
    TravelTrailerVehicle(int number,int year,string make, string model,
                         string bStyle,string color,int cost,
                         int gWeight,int passengers,int mileage)
    {
        GrossWeight = gWeight;
        Passengers= passengers;
        Mileage= mileage;
        Year = year;
        Make = make;
        Model = model;
        BodyStyle = bStyle;
        Color = color;
        Number = number;
        Cost = cost;
    }
    void display() {
        cout << "----This is Travel Trailer Class.----" << endl;
        cout << "Number: " << Number << endl;
        cout << "Year: " << Year << endl;
        cout << "Make: " << Make << endl;
        cout << "Model:" << Model << endl;
        cout << "BodyStyle: " << BodyStyle << endl;
        cout << "Color: " << Color << endl;
        cout << "Cost:" << Cost << endl;
        cout << "GrossWeigth: " << GrossWeight << endl;
        cout << "Passengers: " << Passengers << endl;
        cout << "Mileage: " << Mileage << endl;
    }
};

Main Method.

int main() {
    TruckVehicle Truck1(111111, 2014,"Toyota", "Tacoma","4x4","Black",25000, 2, 15000, 333333,"Gas", 222222);
    MotorcycleVehicle Motorcycle1(2222222,2015,"Honda", "Gold Wing","Coupe","Red",5000, 1, 3250);
    TravelTrailerVehicle Trailer1(123456789, 2015, "Toyota", "Camry","Coupe","Red", 20000, 666666, 5, 9855);
    Truck1.display();
    Motorcycle1.display();
    Trailer1.display();
    system("pause");
    return 0;
}

can someone give me a hint of what is meant by "Use default function parameters and member list to minimize the the code needed for all the constructors".

jotik
  • 17,044
  • 13
  • 58
  • 123
  • First decide for which of the constructor parameters do you intend to provide a default value? Then follow the rule of writing default function parameters in c++. – Biruk Abebe Apr 11 '16 at 06:54
  • 3
    I don't think your question requires this wall of code. You should keep the code shown relevant to the specific question you're asking. – juanchopanza Apr 11 '16 at 06:58

1 Answers1

-1

This answer should be helpful:

C++ superclass constructor calling rules

Also, for the display method you should consider using a virtual function to achieve polymorphism, see the following links for detailed explanation:

C++ referece: Polymorphism

Community
  • 1
  • 1
Lotayou
  • 162
  • 11