FAQ 17.02 How does C express inheritance?

FAQ 17.02 How does C++ express inheritance?

graphics/new_icon.gif

Here is a typical C++ inheritance declaration.

 #include <iostream> using namespace std; class Vehicle { public:   virtual void startEngine() throw() = 0;   virtual ~Vehicle() throw(); }; Vehicle::~Vehicle() throw() { } class V8Engine { public:   void start() throw(); }; void V8Engine::start() throw() { cout << "starting V8Engine\n"; } class Car : public Vehicle { public:   virtual void startEngine() throw(); protected:   V8Engine engine_; }; void Car::startEngine() throw() { engine_.start(); } 

This relationship can be described in several equivalent ways.

  • Car is a kind-of Vehicle.

  • Car is derived from Vehicle.

  • Car is a subclass of Vehicle.

  • Car is a child class of Vehicle (not common in the C++ community).

  • Vehicle is the base class of Car.

  • Vehicle is the parent class of Car (not common in the C++ community).

  • Vehicle is the super-class of Car (not common in the C++ community).

As a consequence of the kind-of relationship, a Car object can be treated as a Vehicle object. For example, since function f(Vehicle&) accepts any kind-of Vehicle, it can be passed a Car or an object of any other class derived from Vehicle:

 void f(Vehicle& v) throw() { v.startEngine(); } int main() {   Car c;   f(c); } 

UML uses the following notation to show inheritance.

graphics/17fig01.gif



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net