Assignment 4 - Inheritance - Alt. Solution 2

 avatar
itsLu
c_cpp
2 years ago
2.2 kB
8
Indexable
#include <iostream>
using namespace std;

class vehicle
{
protected:
    int manufacturerID, noCylinders;
public:
    vehicle()
    {
        manufacturerID = 0;
        noCylinders = 0;
    }
    vehicle(int mID, int nCyl)
    {
        manufacturerID = mID;
        noCylinders = nCyl;
    }
    void setmID (int mID)
    {
        manufacturerID = mID;
    }
    void setnCyl (int nCyl)
    {
        noCylinders = nCyl;
    }
    int getmID ()
    {
        return manufacturerID;
    }
    int getnCyl ()
    {
        return noCylinders;
    }
};

class truck: public vehicle
{
    double loadCapacity, towingCapacity;
public:
    truck(int mID, int nCyl, double lCap, double tCap)
    {
        setmID(mID);
        setnCyl(nCyl);
        loadCapacity = lCap;
        towingCapacity = tCap;
    }
    void setlCap(double lCap)
    {
        loadCapacity = lCap;
    }
    void settCap(double tCap)
    {
        towingCapacity = tCap;
    }
    double getlCap()
    {
        return loadCapacity;
    }
    double gettCap()
    {
        return towingCapacity;
    }
};

int main()
{
    vehicle vehicle1(9, 3);
    cout << "Vehicle 1's Manufacturer ID: " << vehicle1.getmID() << "\tVehicle 1's Number of Cylinders: " << vehicle1.getnCyl() << endl;
    vehicle1.setmID(5);
    vehicle1.setnCyl(8);
    cout << "Vehicle 1's Manufacturer ID: " << vehicle1.getmID() << "\tVehicle 1's Number of Cylinders: " << vehicle1.getnCyl() << endl;
    truck truck1(1, 8, 9.4, 5.5);
    cout << "Truck 1's Manufacturer ID: " << truck1.getmID() << "\tVehicle 1's Number of Cylinders: " << truck1.getnCyl() << endl;
    cout << "Truck 1's Load Capacity: " << truck1.getlCap() << "\tTruck 1's Towing Capacity: " << truck1.gettCap() << endl;
    truck1.setmID(6);
    truck1.setnCyl(8);
    truck1.setlCap(1.2);
    truck1.settCap(7.9);
    cout << "Truck 1's Manufacturer ID: " << truck1.getmID() << "\tVehicle 1's Number of Cylinders: " << truck1.getnCyl() << endl;
    cout << "Truck 1's Load Capacity: " << truck1.getlCap() << "\tTruck 1's Towing Capacity: " << truck1.gettCap() << endl;
}
Editor is loading...
Leave a Comment