类的多态性

分别定义抽象类Shape,由它派生出5个派生类Circle(圆形)、Square(正方形)、Rectangle(矩形)、Trapezoid(梯形)、Triangle(三角形)。
① 要求使用基类指针数组,使它的每一个元素指向一个派生类对象。
② 用虚函数分别计算这些图形的周长和面积,并分别求出它们的和。
③ 实现指定的功能,按要求撰写实验报告。

代码仅供参考

#include <iostream>
#include <string>
using namespace std;

const double PI=3.14;

class Shape
{
 public:
 virtual float perimeter() const {return 0.0;}//周长 
 virtual float area() const {return 0.0;}//面积 
 virtual void shapeName() const = 0;//虚函数 
};

class Circle:public Shape
{
 public:
 Circle(float r=0);
 virtual float perimeter() const;
 virtual float area() const;
 virtual void shapeName() const {cout << "圆:";}
 protected:
 float radius;
};
//声明Circle类成员函数
Circle::Circle(float r):radius(r){}
float Circle::perimeter() const {return PI*radius*2;}
float Circle::area() const {return PI*radius*radius;} 

class Square:public Shape
{
 public:
 Square(float a=0);
 virtual float perimeter() const;
 virtual float area() const;
 virtual void shapeName() const {cout << "正方形:";}
 protected:
 float sideLength;
};
//声明Square类成员函数
Square::Square(float a):sideLength(a){} 
float Square::perimeter() const {return 4*sideLength;}
float Square::area() const {return sideLength*sideLength;} 

class Rectangle:public Shape
{
 public:
 Rectangle(float a=0, float b=0);
 virtual float perimeter() const;
 virtual float area() const;
 virtual void shapeName() const {cout << "矩形:";}
 protected:
 float sideLength1;
 float sideLength2;
};
//声明Rectangle类成员函数
Rectangle::Rectangle(float a, float b):sideLength1(a),sideLength2(b){} 
float Rectangle::perimeter() const {return 2*sideLength1+2*sideLength2;}
float Rectangle::area() const {return sideLength1*sideLength2;} 

class Trapezoid:public Shape
{
 public:
 Trapezoid(float a=0, float b=0,float c=0,float d=0,float e=0);
 virtual float perimeter() const;
 virtual float area() const;
 virtual void shapeName() const {cout << "梯形:";}
 protected:
 float upper_bottom;
 float under_bottom;
 float height;
 float length1;
 float length2;
};
//声明Trapezoid类成员函数
Trapezoid::Trapezoid(float a, float b, float c, float d, float e):
           upper_bottom(a),under_bottom(b),height(c),length1(d),length2(e){} 
float Trapezoid::perimeter() const {return length1+length2+upper_bottom+under_bottom;}
float Trapezoid::area() const {return (upper_bottom+under_bottom)*height/2;} 

class Triangle:public Shape
{
 public:
 Triangle(float a=0, float b=0, float c=0, float d=0);
 virtual float perimeter() const;
 virtual float area() const;
 virtual void shapeName() const {cout << "三角形:";}
 protected:
 float bottom;
 float height;
 float sideLength1;
 float sideLength2;
};
//声明Triangle类成员函数
Triangle::Triangle(float a, float b, float c, float d):
                   bottom(a),height(b),sideLength1(c),sideLength2(d){} 
float Triangle::perimeter() const {return bottom*height/2;}
float Triangle::area() const {return sideLength1+sideLength2+bottom;} 

int main()
{
 Circle circle(3);
 Square square(4);
 Rectangle rectangle(3,4);
 Trapezoid trapezoid(2,10,3,5,5);
 Triangle triangle(6,4,5,5);
 Shape *pt[5]={&circle,&square,&rectangle,&trapezoid,&triangle};
 int i,j;
 for(i=0;i<5;i++)
 {
 pt[i]->shapeName();
 cout<<"perimeter="<<pt[i]->perimeter()<<",area="<<pt[i]->area()<<endl;
 }
 float sum1=0,sum2=0;
 for(i=0;i<5;i++)
 {
 sum1+=pt[i]->perimeter();
 sum2+=pt[i]->area();
 }
 cout << "总周长为:" << sum1 << endl;
 cout << "总面积为:" << sum2 << endl;
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
多态性是面向对象程序设计中一个非常重要的概念,它允许同一型的函数或方法在不同情况下表现出不同的行为。在C++中,多态性有两种实现方式:虚函数和模板。 1. 虚函数 虚函数是一种特殊的成员函数,它可以在基中被声明为虚函数,在派生中进行重写。当使用基指针或引用调用虚函数时,实际调用的是派生中的重写函数,这就是多态性的体现。 例如: ```cpp class Shape { public: virtual void draw() { cout << "I am a shape." << endl; } }; class Circle : public Shape { public: void draw() { cout << "I am a circle." << endl; } }; class Square : public Shape { public: void draw() { cout << "I am a square." << endl; } }; int main() { Shape* s1 = new Circle(); Shape* s2 = new Square(); s1->draw(); // I am a circle. s2->draw(); // I am a square. return 0; } ``` 2. 模板 模板是一种通用的编程技术,它可以实现代码的重用和泛化。在C++中,我们可以使用模板函数和模板实现多态性。 例如: ```cpp template<typename T> void swap(T& a, T& b) { T temp = a; a = b; b = temp; } int main() { int a = 10, b = 20; swap(a, b); // a=20, b=10 double c = 1.23, d = 4.56; swap(c, d); // c=4.56, d=1.23 return 0; } ``` 在上面的例子中,我们定义了一个通用的swap函数,可以交换任意型的变量。当我们传入不同型的变量时,编译器会自动实例化出对应型的函数。这就是模板实现多态性的方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值