home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / tutorial / cpptutor / source / appfram2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-15  |  1.3 KB  |  47 lines

  1.                              // Chapter 11 - Program 10 - APPFRAM2.CPP
  2. #include "iostream.h"
  3.  
  4. // This is a very simple class
  5. class CForm {
  6. public:
  7.    void display_form(void);
  8.    virtual void header(void) { cout << "This is a header\n"; }
  9.    virtual void body(void) = 0;    // Pure virtual function
  10.    virtual void footer(void) { cout << "This is a footer\n\n"; }
  11. };
  12.  
  13. void CForm::display_form(void)
  14. {
  15.    header();
  16.    for (int index = 0 ; index < 3 ; index++)
  17.       body();
  18.    footer();
  19. }
  20.  
  21. // This class overrides two of the virtual methods of the base class
  22. class CMyForm : public CForm {
  23.    void body(void) { cout << " This is the new body text\n"; }
  24.    void footer(void) { cout << "This is the new footer\n"; }
  25. };
  26.  
  27. void main()
  28. {                            
  29. // The next three lines of code are now illegal since CForm is now an
  30. //  abstract type and an object cannot be created of that type.
  31. // CForm *first_form = new CForm; 
  32. //    first_form->display_form();   // A call to the base class
  33. //    delete first_form;
  34.  
  35. CForm *first_form = new CMyForm;   // An object of the derived class
  36.    first_form->display_form();     // A call to the derived class
  37. }
  38.  
  39.  
  40. // Result of execution
  41. //
  42. // This is a header
  43. //  This is the new body text
  44. //  This is the new body text
  45. //  This is the new body text
  46. // This is the new footer
  47.