// inherit6.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program illustrates the need of having default constructors. // As you have some constructors for Vehicle (and for Car), the system // will not generate any default constructors for you. Now, if non of your // constructors is a default one, the system may create a compilation error // whenever a need for this default constructor is encountered, or in other cases, // the program may behave unexpectedly. // Key Points: 1) Default Constructors (again!) #include "inherit6.h" // The Vehicle class Vehicle::Vehicle(double pr, int ms) { cout << "\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() { cout << " Creating objects from the 4 classes will start" << "\n"; Vehicle v1(10000, 100); cout << endl; Bus b1(20000, 200, 8); cout << endl; Car c1(30000, 300, "BMW"); cout << endl; Convertible cv1(40000, 400, "Benz", softtop); cout << endl; cout << "\n Finished Creating objects from the 4 classes. Will start modifying then showing" << " their information." << "\n\n"; v1.showInfo(); b1.showInfo(); c1.showInfo(); cv1.showInfo(); return 0; } /* The result of the program's compilation Compiling... inherit6.cpp inherit6.cpp(66) : error C2512: 'Vehicle' : no appropriate default constructor available inherit6.cpp(157) : error C2512: 'Car' : no appropriate default constructor available Error executing cl.exe. inherit1.exe - 2 error(s), 0 warning(s) */