by John
Kopp
Introduction Welcome to About.com's
free tutorial on C++ programming. This lesson covers some
ambiguities that can arise with multiple inheritance, and
their solutions, including virtual inheritance. We will begin
this lesson where we left off in the lesson on multiple
inheritance, designing the JetCar class. As you may
recall, we were designing a JetCar class to support
development of a prototype of my new line of JetCars. We
decided that the JetCar, having properties of both Cars and
Jets needed to inherit from both classes. Here are the classes
for your reference.
class Vehicle
{ public: Vehicle() {cout
<< "Vehicle Constructor" <<
endl;} virtual ~Vehicle()
{cout << "Vehicle Destructor" <<
endl;}
virtual void
accelerate() const {cout << "Vehicle Accelerating"
<< endl;}
void
setAcceleration(double a) {acceleration =
a;} double getAcceleration()
const {return
acceleration;}
protected: double
acceleration; };
class Car: public Vehicle
{ public: Car() {cout
<< "Car Constructor" <<
endl;} virtual ~Car() {cout
<< "Car Destructor" <<
endl;}
virtual void
accelerate() const {cout << "Car Accelerating"
<< endl;} virtual void
drive() const {cout << "Car Driving" <<
endl;}
private: //
Car inherits acceleration accessors,
member };
class Jet: public Vehicle
{ public: Jet() {cout
<< "Jet Constructor" <<
endl;} virtual ~Jet() {cout
<< "Jet Destructor" <<
endl;}
virtual void fly()
const {cout << "Jet flying" <<
endl;} };
class JetCar: public Car, public Jet
{ public: JetCar() {cout
<< "JetCar Constructor" <<
endl;} virtual ~JetCar()
{cout << "JetCar Destructor" <<
endl;}
virtual void
drive() const {cout << "JetCar driving" <<
endl;} virtual void fly()
const {cout << "JetCar flying" <<
endl;} }; | Next
page > Problems
with Multiple Inheritance > Page 1,
2,
3,
4,
5,
6
|