home *** CD-ROM | disk | FTP | other *** search
/ Master Visual C++ 1.5 / MASTERVC15.ISO / vcprog / original / ch03 / rect4.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-07  |  3.2 KB  |  177 lines

  1. ///////////////////////////
  2. // Program Name: Rect4.CPP
  3. ///////////////////////////
  4.  
  5.  
  6. ////////////
  7. // #include
  8. ////////////
  9. #include <iostream.h>
  10.  
  11.  
  12. /////////////////////////////////////////////////
  13. // The CRectangle class declaration (base class)
  14. /////////////////////////////////////////////////
  15. class CRectangle
  16. {
  17. public:
  18.      CRectangle(int w, int h);  // Constructor
  19.  
  20.      void DisplayArea (void);
  21.  
  22.      ~CRectangle(); // Destructor
  23.     
  24.  
  25.      int m_Width;
  26.      int m_Height;
  27.  
  28. };
  29.  
  30.  
  31. /////////////////////////////////////////////////////
  32. // The declaration of the derived class CNewRectangle
  33. /////////////////////////////////////////////////////
  34. class CNewRectangle : public CRectangle
  35. {
  36.  
  37. public:
  38.     CNewRectangle(int w, int h);  // Constructor
  39.     
  40.     void SetWidth (int w);
  41.     void SetHeight (int h);
  42.  
  43.     void DisplayArea();
  44.  
  45.     ~CNewRectangle(); // Destructor
  46.     
  47. };
  48.  
  49.  
  50.  
  51.  
  52. //////////////////////////////////////////////////
  53. // The constructor function of CRectangle (base)
  54. //////////////////////////////////////////////////
  55. CRectangle::CRectangle( int w, int h)
  56. {
  57.  
  58. cout << "In the constructor of the base class" << "\n";
  59.  
  60. m_Width = w;
  61. m_Height = h;
  62.  
  63. }
  64.  
  65.  
  66. //////////////////////////////////////////////////
  67. // The destructor function of CRectangle (base)
  68. /////////////////////////////////////////////////
  69.  
  70. CRectangle::~CRectangle()
  71. {
  72.  
  73. cout << "In the destructor of the base class" << "\n";
  74.  
  75. }
  76.  
  77.  
  78. ////////////////////////////////////////
  79. // Function Name: DisplayArea() (base)
  80. ////////////////////////////////////////
  81. void CRectangle::DisplayArea(void)
  82. {
  83.  
  84. int iArea;
  85.  
  86. iArea = m_Width * m_Height;
  87.  
  88. cout << "The area is: " << iArea << "\n";
  89.  
  90. }
  91.  
  92.  
  93.  
  94. /////////////////////////////////////////////////////////
  95. // The constructor function of CNewRectangle (derived)
  96. /////////////////////////////////////////////////////////
  97. CNewRectangle::CNewRectangle( int w, int h):CRectangle( w, h)
  98. {
  99.  
  100. cout << "In the constructor of the derived class" << "\n";
  101.  
  102. }
  103.  
  104.  
  105.  
  106. ////////////////////////////////////////////////////////
  107. // The destructor function of CNewRectangle (derived)
  108. ////////////////////////////////////////////////////////
  109. CNewRectangle::~CNewRectangle()
  110.  
  111. {
  112.  
  113. cout << "In the destructor of the derived class" << "\n";
  114.  
  115. }
  116.  
  117.  
  118. /////////////////////////////////////////
  119. // Function Name: SetWidth() (derived)
  120. /////////////////////////////////////////
  121. void CNewRectangle::SetWidth(int w)
  122. {
  123.  
  124. m_Width = w;
  125.  
  126. }
  127.  
  128.  
  129. //////////////////////////////////////////
  130. // Function Name: SetHeight() (derived)
  131. //////////////////////////////////////////
  132. void CNewRectangle::SetHeight(int h)
  133. {
  134.  
  135. m_Height = h;
  136.  
  137. }
  138.  
  139.  
  140. //////////////////////////////////////////
  141. // Function Name: DisplayArea() (derived)
  142. //////////////////////////////////////////
  143. void CNewRectangle::DisplayArea(void)
  144. {
  145.  
  146. int iArea;
  147.  
  148. iArea = m_Width * m_Height;
  149.  
  150. cout << "================= \n";
  151. cout << "The area is: " << iArea << "\n";
  152. cout << "================= \n";
  153.  
  154. }
  155.  
  156.  
  157.  
  158. void main(void)
  159. {
  160.  
  161. CNewRectangle MyRectangle (10, 5);
  162.  
  163. CNewRectangle * pMyRectangle = &MyRectangle;
  164.  
  165. pMyRectangle->DisplayArea();
  166.  
  167.  
  168. pMyRectangle->SetWidth (100);
  169. pMyRectangle->SetHeight (20);
  170.  
  171.  
  172. pMyRectangle->DisplayArea();
  173.  
  174.  
  175. }
  176.  
  177.