home *** CD-ROM | disk | FTP | other *** search
/ Master Visual C++ 1.5 / MASTERVC15.ISO / vcprog / original / ch03 / rect2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-11  |  2.9 KB  |  152 lines

  1. ///////////////////////////
  2. // Program Name: Rect2.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.  
  44.     ~CNewRectangle();            // Destructor
  45.  
  46. };
  47.  
  48.  
  49. //////////////////////////////////////////////////
  50. // The constructor function of CRectangle (base)
  51. //////////////////////////////////////////////////
  52. CRectangle::CRectangle( int w, int h)
  53. {
  54.  
  55. cout << "In the constructor of the base class" << "\n";
  56.  
  57. m_Width = w;
  58. m_Height = h;
  59.  
  60. }
  61.  
  62.  
  63. //////////////////////////////////////////////////
  64. // The destructor function of CRectangle (base)
  65. /////////////////////////////////////////////////
  66.  
  67. CRectangle::~CRectangle()
  68. {
  69.  
  70. cout << "In the destructor of the base class" << "\n";
  71.  
  72. }
  73.  
  74.  
  75. ////////////////////////////////////////
  76. // Function Name: DisplayArea() (base)
  77. ////////////////////////////////////////
  78. void CRectangle::DisplayArea(void)
  79. {
  80.  
  81. int iArea;
  82.  
  83. iArea = m_Width * m_Height;
  84.  
  85. cout << "The area is: " << iArea << "\n";
  86.  
  87. }
  88.  
  89.  
  90. /////////////////////////////////////////////////////////
  91. // The constructor function of CNewRectangle (derived)
  92. /////////////////////////////////////////////////////////
  93. CNewRectangle::CNewRectangle( int w,
  94.                               int h):CRectangle( w, h)
  95. {
  96.  
  97. cout << "In the constructor of the derived class" << "\n";
  98.  
  99. }
  100.  
  101.  
  102. ////////////////////////////////////////////////////////
  103. // The destructor function of CNewRectangle (derived)
  104. ////////////////////////////////////////////////////////
  105. CNewRectangle::~CNewRectangle()
  106.  
  107. {
  108.  
  109. cout << "In the destructor of the derived class" << "\n";
  110.  
  111. }
  112.  
  113.  
  114. /////////////////////////////////////////
  115. // Function Name: SetWidth() (derived)
  116. /////////////////////////////////////////
  117. void CNewRectangle::SetWidth(int w)
  118. {
  119.  
  120. m_Width = w;
  121.  
  122. }
  123.  
  124.  
  125. //////////////////////////////////////////
  126. // Function Name: SetHeight() (derived)
  127. //////////////////////////////////////////
  128. void CNewRectangle::SetHeight(int h)
  129. {
  130.  
  131. m_Height = h;
  132.  
  133. }
  134.  
  135.  
  136. void main(void)
  137. {
  138.  
  139. CNewRectangle MyRectangle (10, 5);
  140.  
  141. MyRectangle.DisplayArea();
  142.  
  143.  
  144. MyRectangle.SetWidth (100);
  145. MyRectangle.SetHeight (20);
  146.  
  147.  
  148. MyRectangle.DisplayArea();
  149.  
  150.  
  151. }
  152.