// inherit8.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program gives an introduction "to the need" of virtual functions. // In fact, the program does not mention anything about virtual functions; // rather it illustrates the need of virtual functions. // In addition, this program is NOT working as expected. Why? // What could we modify in order to make it work? // Key points: 1) Virtual functions. Why are they needed. // 2) Array of class objects. #include "inherit7.h" // The Vehicle class Vehicle::Vehicle(double pr, int ms) { cout << "\n\n Veh...Executing the 'parameterized' constructor of the base class. " << "\n"; price = pr; maxSpeed = ms; } void Vehicle::setPrice(double pr) { price = pr; } double Vehicle::getPrice( ) { return price; } void Vehicle::setMaxSpeed(int ms) { maxSpeed = ms; } int Vehicle::getMaxSpeed() { return maxSpeed; } void Vehicle::showInfo() { cout << " The vehicle price is " << price << "$, and its maximum speed is " << maxSpeed << "km." << endl << endl; } // The Bus class Bus::Bus() { // This is line 66 cout << "\n BS...Executing the default constructor of the Bus class. " << "\n"; price = 0; maxSpeed = 0; cargoCapacity = 0; } Bus::Bus(double pr, int ms, double cc): Vehicle(pr, ms) { cout << "\n BS...Executing the 'parameterized' constructor of the Bus class. " << "\n"; price = pr; maxSpeed = ms; cargoCapacity = cc; } void Bus::setCargoCapacity(double cc) { cargoCapacity = cc; } double Bus::getCargoCapacity() { return cargoCapacity; } void Bus::showInfo() // This functions overrides the showInfo function from the base class { cout << " The bus price is " << price << "$, and its maximum speed is " << maxSpeed << "km. The bus has a cargo capacity of " << cargoCapacity << "." << endl << endl; } // The Car class Car::Car(double pr, int ms, char* md) : Vehicle(pr, ms) { cout << "\n CR...Executing the 'parameterized' constructor of the Car class. " << "\n"; price = pr; maxSpeed = ms; model = new char [20]; strcpy(model, md); } void Car::setModel(char* md) { strcpy(model, md); } char* Car::getModel() { return model; } void Car::showInfo() // This functions overrides the showInfo function from the base class { cout << " The car price is " << price << "$, and its maximum speed is " << maxSpeed << "km. The car model is " << model << "." << endl << endl; } Car::~Car() { delete [] model; } // The Convertible car class Convertible ::Convertible() { // This is line 157 cout << "\n CV...Executing the default constructor of the Convertible class." << "\n"; price = 0; maxSpeed = 0; strcpy(model, "Not Yet Known"); top = softtop; } Convertible::Convertible(double pr, int ms, char* md, toptype tt): Car(pr, ms, md) { cout << "\n CV...Executing the 'parameterized' constructor of the Convertible class." << "\n"; price = pr; maxSpeed = ms; strcpy(model, md); top = tt; } void Convertible::setTopType(toptype tt) { top = tt; } char* Convertible::getTopType() { if (top == softtop) return "soft top"; else return "hard top"; } void Convertible::showInfo() // This functions overrides both showInfo functions from the parent classes { cout << " The price of the convertible car is " << price << "$, and its maximum speed is " << maxSpeed << "km. The convertible car model is " << model << ". The car has a " << getTopType() << "." << endl << endl; } int main() { // Create some objects from the different classes // We want to create 6 different vehicle objects // ( 1 vehicle, 2 buses, 2 cars & 2 convertibles; // the order of creation does not matter here), then // we want to show the information for each of these objects. // Note that the following part of code is commented out. // The reason is that it is long and so we wish to replace it // with the smaller code that follows it. // cout << " Creating a Vehicle object *********************** " << "\n"; // Vehicle v1( 10000, 210 ); // // cout << " \n Creating a Bus object *********************** " << "\n"; // Bus bs1( 20000, 220, 8 ); // // cout << " \n Creating a Car object *********************** " << "\n"; // Car c1( 30000, 230, "BMW" ); // // cout << " \n Creating a Convertible object *********************** " << "\n"; // // Convertible cv1( 40000, 240, "Chrysler", softtop ); // // cout << " \n Creating a Bus object *********************** " << "\n"; // Bus bs2( 50000, 250, 10 ); // // cout << " \n Creating a Car object *********************** " << "\n"; // Car c2( 60000, 260, "BENZ" ); // Execute each object's version of function ShowInfo // // v1.showInfo( ); // b1.showInfo( ); // c1.showInfo( ); // cv1.showInfo( ); // b2.showInfo(); // c2.showInfo(); // Instead of doing all of the above, which is long, we wish to // create an array of these objects then iterate through using a for // loop to show the information of these different objects. // Declare array of 6 objects Vehicle Vec_arr[6] = { Vehicle( 10000, 210 ), Bus( 20000, 220, 8 ), Car( 30000, 230, "BMW" ), Convertible( 40000, 240, "Chrysler", softtop ), Bus( 50000, 250, 10 ), Car( 60000, 260, "BENZ" )}; // Notice that we did assign different objects // to the array. However, what are the real objects // in the array now? and why? cout << endl << "Here is the information of the different objects" << endl; cout << "*************************************************" << endl << endl; for ( int i = 0; i < 6; i++ ) Vec_arr[ i ].showInfo( ); return 0; } /* The output of the program Veh...Executing the 'parameterized' constructor of the base class. Veh...Executing the 'parameterized' constructor of the base class. BS...Executing the 'parameterized' constructor of the Bus class. Veh...Executing the 'parameterized' constructor of the base class. CR...Executing the 'parameterized' constructor of the Car class. Veh...Executing the 'parameterized' constructor of the base class. CR...Executing the 'parameterized' constructor of the Car class. CV...Executing the 'parameterized' constructor of the Convertible class. Veh...Executing the 'parameterized' constructor of the base class. BS...Executing the 'parameterized' constructor of the Bus class. Veh...Executing the 'parameterized' constructor of the base class. CR...Executing the 'parameterized' constructor of the Car class. Here is the information of the different objects ************************************************ The vehicle price is 10000$, and its maximum speed is 210km. The vehicle price is 20000$, and its maximum speed is 220km. The vehicle price is 30000$, and its maximum speed is 230km. The vehicle price is 40000$, and its maximum speed is 240km. The vehicle price is 50000$, and its maximum speed is 250km. The vehicle price is 60000$, and its maximum speed is 260km. */