cc32a_demo-32dk2j_cpp_纯虚函数与抽象类-txwtech

 

//32dk2j_cpp_纯虚函数与抽象类cc32a_demo-txwtech
//纯虚函数是用来继承用的
//纯虚函数
//抽象类-抽象数据类型
//*任何包含一个或者多个纯虚函数的类都是抽象类
//*不要/不能创建这个类的对象,应该/只能继承它
//*务必覆盖从这个类继承的纯虚函数
//实现纯虚函数-----------可写可以不写
//C++接口
//就是只包含纯虚函数的抽象基类

 

  1 //32dk2j_cpp_纯虚函数与抽象类cc32a_demo-txwtech
  2 //纯虚函数是用来继承用的
  3 //纯虚函数
  4 //抽象类-抽象数据类型
  5 //*任何包含一个或者多个纯虚函数的类都是抽象类
  6 //*不要/不能创建这个类的对象,应该/只能继承它
  7 //*务必覆盖从这个类继承的纯虚函数
  8 //实现纯虚函数-----------可写可以不写
  9 //C++接口
 10 //就是只包含纯虚函数的抽象基类
 11 #include <iostream>
 12 using namespace std;
 13 
 14 class Shape//老子--//包含一个或者多个纯虚函数的类就是抽象类
 15 {
 16 public:
 17     Shape()
 18     {}
 19     virtual ~Shape() {}
 20     virtual double GetArea()=0;//纯虚函数
 21     virtual double GetPerim()=0;//纯虚函数
 22     //virtual void Draw() {}
 23     virtual void Draw() = 0;//=0就是纯虚函数
 24 };
 25 ////纯虚函数可以写,一般不写
 26 //void Shape::Draw()  //这个就是实现 这个纯虚函数
 27 //{
 28 //    cout << "...";
 29 //}
 30 class Circle :public Shape//儿子
 31 {
 32     //没有覆盖纯虚函数,还是抽象类
 33     //如下覆盖的操作
 34 public:
 35     Circle(int radius) :itsRadius(radius) {}//构造函数
 36     //析构函数-//因为GetArea() 与GetPerim()是继承来的,所以还是虚函数
 37     //只要类里面有一个虚函数,那么析构函数也需要做成虚的,不然要出错
 38     virtual ~Circle() {}
 39     //三个纯虚函数重写后,就不是虚的了。
 40     double GetArea() 
 41     {
 42         return 3.14*itsRadius*itsRadius;
 43     }
 44     double GetPerim()
 45     {
 46         return 2 * 3.14*itsRadius;
 47     }
 48     void Draw();
 49 private:
 50     int itsRadius;
 51 };
 52 void Circle::Draw()
 53 {
 54     cout << "circle drawing routine" << endl;
 55     Shape::Draw();//调用基类的纯虚函数
 56 }
 57 class Rectangle :public Shape//儿子
 58 {
 59 public:
 60     Rectangle(int len,int width):itsWidth(width),itsLength(len) {}
 61     virtual ~Rectangle() {}
 62     double GetArea() { return itsLength * itsWidth; }
 63     double GetPerim() { return 2 * itsWidth + 2 * itsLength; }
 64     virtual int GetLength() { return itsLength; }
 65     virtual int GetWidth() { return itsWidth; }
 66     void Draw();
 67 private:
 68     int itsWidth;
 69     int itsLength;
 70 
 71 };
 72 void Rectangle::Draw()
 73 {
 74     for (int i = 0; i < itsLength; i++)
 75     {
 76         for (int j = 0; j < itsWidth; j++)
 77         {
 78             cout << "x"; //<< endl;
 79         }
 80         cout << endl;
 81 
 82 
 83     }
 84     Shape::Draw();//调用基类的纯虚函数
 85 }
 86 class Square :public Rectangle //孙子
 87 {
 88 public:
 89     Square(int len);
 90     Square(int len,int width);
 91     virtual ~Square() {};
 92     double getPerim() { return 4 * GetLength(); }
 93 
 94 
 95 };
 96 Square::Square(int len) :Rectangle(len, len) {}
 97 Square::Square(int len, int width) : Rectangle(len, width)
 98 {
 99     if (GetLength() != GetWidth())
100         cout << "Error,not a square...a rectangle???" << endl;
101 }
102 
103 
104 int main()
105 {
106     Circle a(8);
107     a.Draw();
108     Rectangle b(5,10);
109     b.Draw();
110     Square c(8);
111     c.Draw();
112 
113     int choice;
114     bool fQuit = false;
115     Shape *sp=nullptr;//一个指向基类的指针可以指向它的派生类,指向它的子子孙孙
116     //C++的特性
117     //Shape *sp; //vs2017中必须初始化指针
118 
119     while (fQuit == false)
120     {
121         cout << "(1)circle (2)Rectangle (3)Square (0)Quit:";
122         cin >> choice;
123         switch (choice)
124         {
125         case 1:
126             sp = new Circle(5);//指针必须使用new创建对象
127             break;
128         case 2:
129             sp = new Rectangle(4, 6);
130             break;
131         case 3:
132             sp = new Square(5);
133             break;
134         case 0:
135             fQuit = true;
136             break;
137         
138         /*default:
139             break;*/
140         }
141         if (fQuit == false)
142         {
143             sp->Draw();
144             delete sp;
145             cout << endl;
146         }
147         
148     }
149 
150     getchar();
151     return 0;
152 }
posted @ 2020-01-16 18:02  txwtech  阅读(215)  评论(0编辑  收藏  举报