當前位置:編程學習大全網 - 編程語言 - 寫壹個程序,定義抽象基類shape,派生5個派生類 虛函數

寫壹個程序,定義抽象基類shape,派生5個派生類 虛函數

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 #include <iostream>

using namespace std;

const float PI = (float)3.14159;

class Shape{

public:

virtual float CalCircumference()=0;

virtual float CalArea()=0;

};

//圓形

class Roundness: public Shape{

public:

Roundness(float r):radius(r)

{}

virtual float CalCircumference()

{

return 2 * PI * radius;

}

virtual float CalArea()

{

return PI * radius * radius;

}

private:

float radius;

};

//正方形

class Square: public Shape{

public:

Square(float w):width(w)

{}

virtual float CalCircumference()

{

return 4 * width;

}

virtual float CalArea()

{

return width * width;

}

private:

float width;

};

//長方形

class Rectangle: public Shape{

public:

Rectangle(float w, float h):width(w), hight(h)

{}

virtual float CalCircumference()

{

return 2 * (width + hight);

}

virtual float CalArea()

{

return hight * width;

}

private:

float width;

float hight;

};

void main()

{

Shape* roud = new Roundness(3);

Shape* squ = new Square(3);

Shape* rang = new Rectangle(3, 4);

cout<<"圓形的周長:"<<roud->CalCircumference()<<" 面積:"<<roud->CalArea()<<endl;

cout<<"正方形的周長:"<<squ->CalCircumference()<<" 面積:"<<squ->CalArea()<<endl;

cout<<"長方形的周長:"<<rang->CalCircumference()<<" 面積:"<<rang->CalArea()<<endl;

}

  • 上一篇:t is+adj+for sb+to do sth和it is+of sb +to do sth有什麽區別?
  • 下一篇:Pro handle宏編程
  • copyright 2024編程學習大全網