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

📄 prog16_03.cpp

📁 c++最经典的入门书籍
💻 CPP
字号:
// Program 16.3 Access specifiers and virtual functionsFile prog16_03.cpp
#include <iostream>
#include "Box.h"                                    // For the Box class
#include "ToughPack.h"                              // For the ToughPack class
#include "Carton.h"	                                 // For the Carton class
using std::cout;
using std::endl;

int main() {
  Box myBox(20.0, 30.0, 40.0);                      // Declare a base box
  ToughPack hardcase(20.0, 30.0, 40.0);             // Declare derived box - same size
  Carton aCarton(20.0, 30.0, 40.0);                 // A different kind of derived box

  cout << endl;
  myBox.showVolume();                               // Display volume of base box
  hardcase.showVolume();                            // Display volume of derived box
  aCarton.showVolume();                             // Display volume of derived box
  cout << endl;

// Uncomment the following statement for an error
//  cout << "hardcase volume is " << hardcase.volume() << endl; 

  // Now try using a base pointer for the Box object
  Box* pBox = &myBox;                               // Points to type Box
  cout << "myBox volume through pBox is " << pBox->volume() << endl;
  pBox->showVolume();
  cout << endl;

  // Now try using a base pointer for the ToughPack object
  pBox = &hardcase;                                 // Points to type ToughPack
  cout << "hardcase volume through pBox is " << pBox->volume() << endl;
  pBox->showVolume();
  cout << endl;

  // Now try using a base pointer for the Carton object
  pBox = &aCarton;                                  // Points to type Carton
  cout << "aCarton volume through pBox is " << pBox->volume() << endl;
  pBox->showVolume();

  return 0;
}

⌨️ 快捷键说明

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