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

  1.                              // Chapter 11 - Program 9 - APPFRAM1.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)   { cout << " This is body text\n"; }
  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 header(void) { cout << "This is the new header\n"; }
  24.    void footer(void) { cout << "This is the new footer\n"; }
  25. };
  26.  
  27. void main()
  28. {
  29. CForm *first_form = new CForm; 
  30.    first_form->display_form();   // A call to the base class
  31.  
  32.    delete first_form;
  33.    first_form = new CMyForm;
  34.    first_form->display_form();   // A call to the derived class
  35. }
  36.  
  37.  
  38. // Result of execution
  39. //
  40. // This is a header
  41. //  This is body text
  42. //  This is body text
  43. //  This is body text
  44. // This is a footer
  45. //
  46. // This is the new header
  47. //  This is body text
  48. //  This is body text
  49. //  This is body text
  50. // This is the new footer
  51.