home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / m / m003_1 / sb_bc.ddi / BC / CH4 / PROG4-8.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  1.3 KB  |  91 lines

  1. //  PROG4-8.CPP
  2.  
  3. #include<stdio.h>
  4.  
  5. //////////  Base Class
  6. //////////
  7. class Base
  8. { protected:         // modified here
  9.     int base_int ;
  10.  
  11.   public:
  12.     Base(int i) ;
  13.     void print(void) ;
  14. } ;
  15.  
  16.  
  17. Base::Base(int i)
  18. {
  19.   base_int = i ;
  20. }
  21.  
  22. void Base::print(void)
  23. {
  24. }
  25.  
  26.  
  27. ///////////  Derived1 Class
  28. //////////
  29. class Derived1 : public Base
  30. { private :
  31.     char derived1 ;
  32.  
  33.   public :
  34.     Derived1(int i, char c) ;
  35.     void list(void) ;
  36. } ;
  37.  
  38.  
  39. Derived1::Derived1(int i, char c) : Base(i)
  40. { int test ;
  41.  
  42.   derived1 = c ;
  43.   test = base_int ;       // no more error
  44. }
  45.  
  46. void Derived1::list(void)
  47. {
  48.   print() ;               // correct
  49.   // other program segment
  50. }
  51.  
  52.  
  53. //////////  Derived2 Class
  54. //////////
  55. class Derived2 : private Base
  56. { private :
  57.     float derived2 ;
  58.  
  59.   public :
  60.     Derived2(int i, float f) ;
  61.     void list(void) ;
  62. } ;
  63.  
  64.  
  65. Derived2::Derived2(int i, float f) : Base(i)
  66. { int test ;
  67.  
  68.   derived2 = f ;
  69.   test = base_int ;     // no more error
  70. }
  71.  
  72. void Derived2::list(void)
  73. {
  74.   print() ;             // correct
  75.   // other program segment
  76. }
  77.  
  78.  
  79. int main()
  80. { Base a(1) ;
  81.   Derived1 b(2,'b') ;
  82.   Derived2 c(3,'c') ;
  83.  
  84.   a.print() ;           // correct
  85.   b.print() ;           // correct
  86.   c.print() ;           // error 3
  87.  
  88.   a.base_int = 2 ;      // error 4
  89. }
  90.  
  91.