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

📄 upcast.cpp

📁 适合初学者学习以及程序员回顾
💻 CPP
字号:
// Upcast.cpp

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::cerr;

//---- 宣告类别 Shape --------
class Shape
{ int i;
public:
  Shape(): i(7){}
  ~Shape(){}
  void Draw()  {cout<<"画个图 \n";}
  void Erase() {cout<<"将图清除\n";}
};

//---- 宣告类别 Circle--------
class Circle : public Shape
{ int r;
public:
  Circle(): r(5)      {}
  Circle(int N): r(N) {}
  ~Circle()           {}
  void Draw()  {cout<<"画一个圆形 \n";}
  void Erase() {cout<<"把圆形清除\n";}
};
void Make(Shape &S1) {S1.Draw();}
void Remove(Shape *pS) {pS->Erase();}
//void Remove(Shape *pS) {(*pS).Erase();}

// ----主程式-------------------------------
main()
{
  Circle C1;
  Circle *pC1 = &C1;
  cout << "执行 “Make(C1)”  之后: " << endl;
  Make(C1);
  cout << "执行 “Remove(pC1)”  之后: " << endl;
  Remove(pC1);
  cout << "C1 有 "
       << sizeof(C1)/sizeof(int)
       << " 个 int." << endl;
}

⌨️ 快捷键说明

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