An Example: Class Interface

 < Day Day Up > 



This section presents an example that illustrates the use of the language features discussed in the previous section. Figure 16-2 shows a UML class diagram with three classes, Interface, DerivedClassOne, and DerivedClassTwo arranged in a three-level inheritance hierarchy.

click to expand
Figure 16-2: Class Diagram Showing Three-Level Inheritance Hierarchy

Class Interface is an abstract base class because it contains three pure virtual functions: function_a(), function_b(), and function_c(). It also contains a virtual destructor as well as a constructor. The code for the class Interface declaration is given in example 16.1.

Listing 16.1: interface.h

start example
  1  #ifndef INTERFACE_H  2  #define INTERFACE_H  3  #include <iostream>  4  using namespace std;  5  6  7  class Interface {  8    public:  9       Interface(){cout<<"Interface constructor called!"<<endl;} 10       virtual ~Interface(){cout<<"Interface destructor called!"<<endl;} 11       virtual void function_a() = 0; 12       virtual void function_b() = 0; 13       virtual void function_c() = 0; 14  }; 15  #endif
end example

In this example both the constructor and destructor are inline functions that contain execution trace messages. I did this to minimize the number of files required for this example. Notice that the destructor is declared virtual and that function_a(), function_b(), and function_c() are declared to be pure virtual functions by being assigned the value of zero.

Example 16.2 gives the code for class DerivedClassOne.

Listing 16.2: derived_class_one.h

start example
  1  #ifndef DERIVED_CLASS_ONE_H  2  #define DERIVED_CLASS_ONE_H  3  #include "interface.h"  4  #include <iostream>  5   using namespace std;  6  7   class DerivedClassOne : public Interface {  8     public:  9        DerivedClassOne(){cout<<"DerivedClassOne contructor called!"<<endl;} 10        virtual ~DerivedClassOne(){cout<<"DerivedClassOne destructor called!"<<endl;} 11        void function_a(){cout<<"DerivedClassOne function_a() called!"<<endl;} 12        void function_b(){cout<<"DerivedClassOne function_b() called!"<<endl;} 13        void function_c(){cout<<"DerivedClassOne function_c() called!"<<endl;} 14   }; 15  #endif
end example

DerivedClassOne publicly inherits the behavior of class Interface. In then provides its own destructor and overrides function_a(), function_b(), and function_c(). Notice there is no need to explicitly redeclare these three functions as virtual. This is because functions declared as virtual in a base class remain virtual functions throughout the inheritance hierarchy.

Example 16.3 gives the code for DerivedClassTwo.

Listing 16.3: derived_class_two.h

start example
  1  #ifndef DERIVED_CLASS_TWO_H  2  #define DERIVED_CLASS_TWO_H  3  #include "derived_class_one.h"  4  #include <iostream>  5  using namespace std;  6  7  class DerivedClassTwo : public DerivedClassOne {  8   public:  9       DerivedClassTwo(){cout<<"DerivedClassTwo constructor called!"<<endl;} 10       virtual ~DerivedClassTwo(){cout<<"DerivedClassTwo destructor called!"<<endl;} 11       void function_c(){cout<<"DerivedClassTwo function_c() called!"<<endl;} 12  }; 13  #endif
end example

DerivedClassTwo publicly inherits the behavior of DerivedClassOne. It provides a virtual destructor and overrides function_c().

Example 16.4 gives the code for a main() function used to test these three classes.

Listing 16.4: main.cpp

start example
  1  #include <iostream>  2  using namespace std;    3  #include "interface.h"  4  #include "derived_class_one.h"  5  #include "derived_class_two.h"  6  7  int main(){  8  9    Interface* i_ptr = new DerivedClassOne(); 10    i_ptr->function_a(); 11    i_ptr->function_b(); 12    i_ptr->function_c(); 13    delete i_ptr; 14     15    i_ptr = new DerivedClassTwo(); 16    i_ptr->function_a(); 17    i_ptr->function_b(); 18    i_ptr->function_c(); 19    delete i_ptr; 20 21    while(true){   22    cout<<"Enter 1 for DerivedClassOne object; 2 for DerivedClassTwo object; anything else to exit: "; 23    char c; 24    cin>>c; 25    switch(c){ 26     case '1': i_ptr = new DerivedClassOne(); 27               i_ptr->function_a(); 28               i_ptr->function_b(); 29               i_ptr->function_c(); 30               delete i_ptr; 31               break; 32     case '2': i_ptr = new DerivedClassTwo(); 33               i_ptr->function_a(); 34               i_ptr->function_b(); 35               i_ptr->function_c(); 36               delete i_ptr; 37               break; 38     default: exit(0); 39     }//end switch 40    }//end while 41   }//end main()
end example

On line 9 an Interface base class pointer is declared and initialized to point to a DerivedClassOne object. Lines 10 through 12 call each of the functions function_a(), function_b(), and function_c() via the base class pointer. The pointer is deleted on line 13 to free up memory.

Lines 16 through 19 repeat essentially the same steps but this time around a DerivedClassTwo object is instantiated and its address is assigned to the base class pointer.

Line 21 begins a while loop that demonstrates how derived class objects can be created 'on the fly' during program execution and accessed via a base class pointer.

Figure 16-3 shows the results of running example 16.4.

click to expand
Figure 16-3: Results of Running Example 16.4

Follow the trace messages for the constructors and destructors of each class along with function_a(), function_b(), and function_c() and note their corresponding location in the source code.

Quick Review

An abstract base class provides one or more pure virtual functions that must be implemented somewhere in the inheritance hierarchy. A derived class can provide an implementation for a pure virtual function declared in its base class or it might not, in which case the derived class becomes an abstract base class. This will be demonstrated in the next section. Virtual destructors are necessary to ensure the proper destruction of objects in the inheritance hierarchy. A compiler-supplied destructor is not a virtual destructor.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

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