home *** CD-ROM | disk | FTP | other *** search
- ///////////////////////////
- // Program Name: Rect5.CPP
- ///////////////////////////
-
-
- ////////////
- // #include
- ////////////
- #include <iostream.h>
-
-
- /////////////////////////////////////////////////
- // The CRectangle class declaration (base class)
- /////////////////////////////////////////////////
- class CRectangle
- {
- public:
- CRectangle(int w, int h); // Constructor
-
- void DisplayArea (void);
-
- ~CRectangle(); // Destructor
-
-
- int m_Width;
- int m_Height;
-
- };
-
-
- /////////////////////////////////////////////////////
- // The declaration of the derived class CNewRectangle
- /////////////////////////////////////////////////////
- class CNewRectangle : public CRectangle
- {
-
- public:
- CNewRectangle(int w, int h); // Constructor
-
- void SetWidth (int w);
- void SetHeight (int h);
-
- void DisplayArea();
-
- ~CNewRectangle(); // Destructor
-
- };
-
-
-
-
- //////////////////////////////////////////////////
- // The constructor function of CRectangle (base)
- //////////////////////////////////////////////////
- CRectangle::CRectangle( int w, int h)
- {
-
- cout << "In the constructor of the base class" << "\n";
-
- m_Width = w;
- m_Height = h;
-
- }
-
-
- //////////////////////////////////////////////////
- // The destructor function of CRectangle (base)
- /////////////////////////////////////////////////
-
- CRectangle::~CRectangle()
- {
-
- cout << "In the destructor of the base class" << "\n";
-
- }
-
-
- ////////////////////////////////////////
- // Function Name: DisplayArea() (base)
- ////////////////////////////////////////
- void CRectangle::DisplayArea(void)
- {
-
- int iArea;
-
- iArea = m_Width * m_Height;
-
- cout << "The area is: " << iArea << "\n";
-
- }
-
-
-
- /////////////////////////////////////////////////////////
- // The constructor function of CNewRectangle (derived)
- /////////////////////////////////////////////////////////
- CNewRectangle::CNewRectangle( int w, int h):CRectangle( w, h)
- {
-
- cout << "In the constructor of the derived class" << "\n";
-
- }
-
-
-
- ////////////////////////////////////////////////////////
- // The destructor function of CNewRectangle (derived)
- ////////////////////////////////////////////////////////
- CNewRectangle::~CNewRectangle()
-
- {
-
- cout << "In the destructor of the derived class" << "\n";
-
- }
-
-
- /////////////////////////////////////////
- // Function Name: SetWidth() (derived)
- /////////////////////////////////////////
- void CNewRectangle::SetWidth(int w)
- {
-
- m_Width = w;
-
- }
-
-
- //////////////////////////////////////////
- // Function Name: SetHeight() (derived)
- //////////////////////////////////////////
- void CNewRectangle::SetHeight(int h)
- {
-
- m_Height = h;
-
- }
-
-
- //////////////////////////////////////////
- // Function Name: DisplayArea() (derived)
- //////////////////////////////////////////
- void CNewRectangle::DisplayArea(void)
- {
-
- int iArea;
-
- iArea = m_Width * m_Height;
-
- cout << "================= \n";
- cout << "The area is: " << iArea << "\n";
- cout << "================= \n";
-
- }
-
-
-
- void main(void)
- {
-
-
- CNewRectangle* pMyRectangle;
- pMyRectangle = new CNewRectangle(10,5);
-
-
- // Above two statements can be combuned as follows:
- // CNewRectangle* pMyRectangle = new CNewRectangle(10,5);
-
- pMyRectangle->DisplayArea();
-
-
- pMyRectangle->SetWidth (100);
- pMyRectangle->SetHeight (20);
-
-
- pMyRectangle->DisplayArea();
-
- delete pMyRectangle;
- }
-
-