// inherit13.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program illustrates pure virtual functions and abstract base classes. // Sometimes, it is not appropriate (for example, does not make sense in reality) // to explicitly create (have) objects from the base class. If this is the case, // the class should be created as an abstract base class. // For a class to become an abstract base class, all is needed is to have one, or more, // of the virtual functions in that class defined as a pure virtual function (a function // that has no definition). // // Notice that this program does not work (which is good), since we attempt to create // objects from the abstract base class. // Key points: 1) Abstract base classes // 2) Pure virtual functions #include "inherit13.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; } Vehicle::~Vehicle() { cout << endl << "Veh .... ~~~ Executing Vehicle destructor \n\n\n\n"; } // The Bus class Bus::Bus() { 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; } Bus::~Bus() { cout << "Bs .... ~~~ Executing Bus destructor \n"; } // 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() { cout << "Cr .... ~~~ Executing Car destructor \n"; 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; } Convertible::~Convertible() { cout << "CV .... ~~~ Executing Convertible destructor \n\n"; } int main() { // Declare array of 6 pointers to the different Vehicle objects Vehicle *Vec_arr[6] = { new Vehicle( 10000, 210 ), new Bus( 20000, 220, 8 ), // This is line 230 new Car( 30000, 230, "BMW" ), new Convertible( 40000, 240, "Chrysler", softtop ), new Bus( 50000, 250, 10 ), new Car( 60000, 260, "BENZ" )}; 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( ); // Do NOT memory leak; delete the objects for ( i = 0; i < 6; i++ ) delete Vec_arr[i]; return 0; } /* The results of compiling the program Compiling... inherit13.cpp inherit13.cpp(230) : error C2259: 'Vehicle' : cannot instantiate abstract class due to following members: inherit13.cpp(230) : warning C4259: 'void Vehicle::showInfo(void)' : pure virtual function was not defined inherit13.cpp(230) : error C2259: 'Vehicle' : cannot instantiate abstract class due to following members: inherit13.cpp(230) : warning C4259: 'void Vehicle::showInfo(void)' : pure virtual function was not defined Error executing cl.exe. inherit13.exe - 2 error(s), 2 warning(s) */