Assignment 4 - Inheritance (get method)

 avatar
itsLu
c_cpp
a year ago
3.0 kB
15
Indexable
// Create a class called vehicle that has the manufacturer’s id and number of cylinders
// in the engine. Then, create a class called truck that is derived from vehicle and has the
// following additional proprieties (load capacity in tons (type double) and towing capacity
// in pound (type double)). Be sure that your class has constructor, setter and getter method.
// Write a program to test all your methods.

#include <iostream>
using namespace std;

class vehicle
{
protected:
    int manufacturerID, noCylinders;

public:
//Constructor:
    vehicle (int mID = 0, int nCyl = 0)
    {
        manufacturerID = mID;
        noCylinders = nCyl;
    }

//Setters:
    void set_mID (int mID)
    {
        manufacturerID = mID;
    }
    void set_nCyl (int nCyl)
    {
        noCylinders = nCyl;
    }

//Getters:
    int get_mID()
    {
        return manufacturerID;
    }
    int get_nCyl()
    {
        return noCylinders;
    }
};

class truck : public vehicle
{
    double loadCapacity, towingCapacity;
public:
//Constructor:
    truck(int mID = 0, int nCyl = 0, double lCap = 0, double tCap = 0)
    {
        set_mID(mID);
        set_nCyl(nCyl);
        loadCapacity = lCap;
        towingCapacity = tCap;
    }

//Setters:
    void set_lCap(double lCap)
    {
        loadCapacity = lCap;
    }
    void set_tCap(double tCap)
    {
        towingCapacity = tCap;
    }

//Getters:
    double get_lCap()
    {
        return loadCapacity;
    }
        double get_tCap()
    {
        return towingCapacity;
    }
};

int main ()
{
    vehicle v1;
    cout << "Vehicle 1's Manufacturer ID: " << v1.get_mID() << "\tVehicle 1's Number of Cylinders: " << v1.get_nCyl() << endl;
    vehicle v2(1, 2);
    cout << "Vehicle 2's Manufacturer ID: " << v2.get_mID() << "\tVehicle 2's Number of Cylinders: " << v2.get_nCyl() << endl;
    v2.set_mID(3);
    v2.set_nCyl(4);
    cout << "Vehicle 2's Manufacturer ID: " << v2.get_mID() << "\tVehicle 2's Number of Cylinders: " << v2.get_nCyl() << endl;
    truck t1;
    cout << "Truck 1's Manufacturer ID: " << t1.get_mID() << "\tTruck 1's Number of Cylinders: " << t1.get_nCyl() << endl;
    cout << "Truck 1's Load Capacity: " << t1.get_lCap() << " tons\tTruck 1's Towing Capacity: " << t1.get_tCap() << " pounds" << endl;
    truck t2(3, 5, 2.9, 7.8);
    cout << "Truck 2's Manufacturer ID: " << t2.get_mID() << "\tTruck 2's Number of Cylinders: " << t2.get_nCyl() << endl;
    cout << "Truck 2's Load Capacity: " << t2.get_lCap() << " tons\tTruck 2's Towing Capacity: " << t2.get_tCap() << " pounds" << endl;
    t2.set_mID(8);
    t2.set_nCyl(10);
    t2.set_lCap(3.1);
    t2.set_tCap(0.1);
    cout << "Truck 2's Manufacturer ID: " << t2.get_mID() << "\tTruck 2's Number of Cylinders: " << t2.get_nCyl() << endl;
    cout << "Truck 2's Load Capacity: " << t2.get_lCap() << " tons\tTruck 2's Towing Capacity: " << t2.get_tCap() << " pounds" << endl;
}
Leave a Comment