⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex10_09.cpp

📁 Beginning Visual C++ 6源码。Wrox。
💻 CPP
字号:
// EX10_09.CPP
// Using a reference to call a virtual function
#include <iostream>
using namespace std;

class CBox;          // Incomplete class definition required for prototype

void Output(const CBox& aBox);           // Prototype of function


class CBox                               // Base class
{
   public:

      // Function to show the volume of an object
      void ShowVolume() const
      {
         cout << endl
              << "CBox usable volume is " << Volume(); 
      }

      // Function to calculate the volume of a CBox object
      virtual double Volume() const
      { return m_Length*m_Breadth*m_Height; }

      // Constructor
      CBox(double lv = 1.0, double bv = 1.0, double hv = 1.0)
                            :m_Length(lv), m_Breadth(bv), m_Height(hv) {}

   protected:
      double m_Length;
      double m_Breadth;
      double m_Height;
};

class CGlassBox: public CBox             // Derived class
{
   public:

      // Function to calculate volume of a CGlassBox
      // allowing 15% for packing
      virtual double Volume() const
      { return 0.85*m_Length*m_Breadth*m_Height; }

      // Constructor
      CGlassBox(double lv, double bv, double hv): CBox(lv, bv, hv){}
};

int main()
{
   CBox myBox(2.0, 3.0, 4.0);            // Declare a base box
   CGlassBox myGlassBox(2.0, 3.0, 4.0);  // Declare derived box of same size

   Output(myBox);                        // Output volume of base class object
   Output(myGlassBox);                   // Output volume of derived class object

   cout << endl;
   return 0;
}

// Function to output a volume via a virtual function call using a reference
void Output(const CBox& aBox)
{
   aBox.ShowVolume();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -