Assignment 4 - Inheritance
itsLu
c_cpp
2 years ago
1.9 kB
21
Indexable
#include <iostream>
using namespace std;
class vehicle
{
protected:
int manufacturerID, noCylinders;
public:
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) : vehicle(mID, 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 v1(1, 9);
cout << "Vehicle 1's Manufacturer ID: " << v1.getmID() << "\tVehicle 1's # of cylinders: " << v1.getnCyl() << endl;
v1.setmID(2);
v1.setnCyl(6);
cout << "Vehicle 1's Manufacturer ID: " << v1.getmID() << "\tVehicle 1's # of cylinders: " << v1.getnCyl() << endl;
truck t1(0, 7, 2.5, 5.4);
cout << "Truck 1's Manufacturer ID: " << t1.getmID() << "\tVehicle 1's # of cylinders: " << t1.getnCyl() << endl;
cout << "Truck 1's Load Capacity: " << t1.getlCap() << "\tTruck 1's Towing Capacity: " << t1.gettCap() << endl;
t1.setmID(4);
t1.setnCyl(7);
t1.setlCap(7.5);
t1.settCap(5.8);
cout << "Truck 1's Manufacturer ID: " << t1.getmID() << "\tVehicle 1's # of cylinders: " << t1.getnCyl() << endl;
cout << "Truck 1's Load Capacity: " << t1.getlCap() << "\tTruck 1's Towing Capacity: " << t1.gettCap() << endl;
}
Editor is loading...
Leave a Comment