[C++] c++ 实现纯虚函数 →→→→→进入此内容的聊天室

来自 , 2020-07-10, 写在 C++, 查看 113 次.
URL http://www.code666.cn/view/6ae07dcb
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Shape {
  5. public:
  6.         Shape() {
  7.         }
  8.         virtual ~Shape() {
  9.         }
  10.         virtual double GetArea() = 0;
  11.         virtual double GetPerim()= 0;
  12.         virtual void Draw() = 0;
  13. private:
  14. };
  15.  
  16. void Shape::Draw() {
  17.         cout << "Abstract drawing mechanism!\n";
  18. }
  19.  
  20. class Circle: public Shape {
  21. public:
  22.         Circle(int radius) :
  23.                         itsRadius(radius) {
  24.         }
  25.         virtual ~Circle() {
  26.         }
  27.         double GetArea() {
  28.                 return 3.14 * itsRadius * itsRadius;
  29.         }
  30.         double GetPerim() {
  31.                 return 2 * 3.14 * itsRadius;
  32.         }
  33.         void Draw();
  34. private:
  35.         int itsRadius;
  36.         int itsCircumference;
  37. };
  38.  
  39. void Circle::Draw() {
  40.         cout << "Circle drawing routine here!\n";
  41.         Shape::Draw();
  42. }
  43.  
  44. class Rectangle: public Shape {
  45. public:
  46.         Rectangle(int len, int width) :
  47.                         itsLength(len), itsWidth(width) {
  48.         }
  49.         virtual ~Rectangle() {
  50.         }
  51.         double GetArea() {
  52.                 return itsLength * itsWidth;
  53.         }
  54.         double GetPerim() {
  55.                 return 2 * itsLength + 2 * itsWidth;
  56.         }
  57.         virtual int GetLength() {
  58.                 return itsLength;
  59.         }
  60.         virtual int GetWidth() {
  61.                 return itsWidth;
  62.         }
  63.         void Draw();
  64. private:
  65.         int itsWidth;
  66.         int itsLength;
  67. };
  68.  
  69. void Rectangle::Draw() {
  70.         for (int i = 0; i < itsLength; i++) {
  71.                 for (int j = 0; j < itsWidth; j++)
  72.                         cout << "x ";
  73.  
  74.                 cout << "\n";
  75.         }
  76.         Shape::Draw();
  77. }
  78.  
  79. class Square: public Rectangle {
  80. public:
  81.         Square(int len);
  82.         Square(int len, int width);
  83.         virtual ~Square() {
  84.         }
  85.         double GetPerim() {
  86.                 return 4 * GetLength();
  87.         }
  88. };
  89.  
  90. Square::Square(int len) :
  91.                 Rectangle(len, len) {
  92. }
  93.  
  94. Square::Square(int len, int width) :
  95.                 Rectangle(len, width)
  96.  
  97. {
  98.         if (GetLength() != GetWidth())
  99.                 cout << "Error, not a square... a Rectangle??\n";
  100. }
  101.  
  102. int main() {
  103.         int choice;
  104.         bool fQuit = false;
  105.         Shape * sp;
  106.  
  107.         while (fQuit == false) {
  108.                 cout << "(1)Circle (2)Rectangle (3)Square (0)Quit: ";
  109.                 cin >> choice;
  110.  
  111.                 switch (choice) {
  112.                 case 1:
  113.                         sp = new Circle(5);
  114.                         break;
  115.                 case 2:
  116.                         sp = new Rectangle(4, 6);
  117.                         break;
  118.                 case 3:
  119.                         sp = new Square(5);
  120.                         break;
  121.                 default:
  122.                         fQuit = true;
  123.                         break;
  124.                 }
  125.                 if (fQuit == false) {
  126.                         sp->Draw();
  127.                         delete sp;
  128.                         cout << endl;
  129.                 }
  130.         }
  131.         return 0;
  132. }
  133.  

回复 "c++ 实现纯虚函数"

这儿你可以回复上面这条便签

captcha