写一个程序,定义抽象基类Shape,由它派生出5个派生类:Circle(圆形)Square(正方形)Rectangle(矩形)Trapezoid(梯形)Triangle(三角形)。用指针指向来表示面积

这个程序实现了基于面向对象编程的概念,定义了五种几何形状(圆形、正方形、矩形、梯形和三角形)的类,并分别计算它们的面积。每个形状类都继承自基类`Shape`,并重写了纯虚函数`PrintArea()`来返回各自的面积。程序通过实例化这些类并调用`PrintArea()`方法,依次输出了各个形状的面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>

#include<cmath>

using namespace std;

double const pai=3.1415926;

const int N=5;

 

 

class Shape

{

public:

Shape(){};

virtual double PrintArea() const =0;

protected:

};

 

 

class Triangle:public Shape

{

public:

Triangle(double a,double b,double c):length1(a),length2(b),length3(c){}

virtual double PrintArea() const;

protected:

double length1,length2,length3;

};

double Triangle::PrintArea() const

{

    if(length1+length2<=length3||abs(length1-length2)>=length3||length1<=0||length2<=0||length3<=0)

    {

        cout<<"所输入的数不能构成三角形"<<endl;

        system("pause");

        return -1;

    }

    else

    {

        double p=(length1+length2+length3)/2;

        return sqrt(p*(p-length1)*(p-length2)*(p-length3));

    }

 

}

 

 

class Circle :public Shape

{

public:

    Circle(double c):radius(c){}

    virtual double PrintArea() const;

protected:

    double radius;

};

double Circle::PrintArea() const

{

    return pai*radius*radius;

}

 

 

class Rectangle:public Shape

{

public:

    Rectangle(double a, double b):length(a),width(b){}

    virtual double PrintArea() const;

protected:

    double length,width;

};

double Rectangle::PrintArea() const

{

    return length*width;

}

 

class Square:public Shape

{

public:

    Square(double a):length(a){}

    virtual double PrintArea() const;

protected:

    double length;

};

double Square::PrintArea()const

{

    return length*length;

}

 

class Trapezoid:public Shape

{

public:

    Trapezoid(double a,double b,double c):lengthup(a),lengthdown(b),height(c){}

    virtual double PrintArea() const;

protected:

    double lengthup,lengthdown,height;

};

double Trapezoid::PrintArea() const

{

    return (lengthup+lengthdown)/2*height;

}

 

 

 

int main()

{   

    Circle a(10);

    Square b(8);

    Rectangle c(5,2.5);

    Trapezoid d(2,4,6);

    Triangle e(3,4,5);

    Shape *p[N]={&a,&b,&c,&d,&e};

    cout<<"以下分别是园,正方形,矩形,梯形,三角形"<<endl;

    for(int i=0;i<N;i++)

    {

        cout<<p[i]->PrintArea()<<endl;

    }

    system("pause");

    return 0;

 

}

### 回答1: 题意:编一个程序定义抽象基类shape,由它派生5派生类circle圆形)、square正方形)、rectangle矩形)、trapezoid梯形)、triangle三角形),用虚函数。 回答:这道题是要求根据题目描述,编一个程序定义抽象基类shape,利用抽象基类实现派生类circlesquarerectangletrapezoidtriangle,并使用虚函数。 ### 回答2: 编这样一个程序的目的在于帮助大家更深入地理解抽象基类派生类的概念,以及如何使用虚函数实现多态性。 首先,抽象基类shape一个纯虚函数类,它的作用在于定义所有形状所具有的基本属性和行为。在C++中,纯虚函数可以通过在函数声明后加“= 0”来表示,例如: ``` class shape{ public: virtual double area() const = 0; virtual double perimeter() const = 0; }; ``` 这里定义了两个纯虚函数:area()和perimeter(),分别用于计算形状面积和周长。这样一来,任何形状都必须实现这两个函数才能被正确处理。 接下来,我们通过五个派生类来具体实现各种形状: ``` class circle : public shape{ private: double radius; public: circle(double r): radius(r){} double area() const override{ return PI * radius * radius; } double perimeter() const override{ return 2 * PI * radius; } }; class square : public shape{ private: double length; public: square(double l): length(l){} double area() const override{ return length * length; } double perimeter() const override{ return 4 * length; } }; class rectangle : public shape{ private: double length, width; public: rectangle(double l, double w): length(l), width(w){} double area() const override{ return length * width; } double perimeter() const override{ return 2 * (length + width); } }; class trapezoid : public shape{ private: double top, bottom, height, left, right; public: trapezoid(double t, double b, double h, double l, double r): top(t), bottom(b), height(h), left(l), right(r){} double area() const override{ return (top + bottom) * height / 2; } double perimeter() const override{ return top + bottom + left + right; } }; class triangle : public shape{ private: double side1, side2, side3; public: triangle(double s1, double s2, double s3): side1(s1), side2(s2), side3(s3){} double area() const override{ double p = (side1 + side2 + side3) / 2; return sqrt(p * (p - side1) * (p - side2) * (p - side3)); } double perimeter() const override{ return side1 + side2 + side3; } }; ``` 以上是五个形状的具体实现代码,各自实现了area()和perimeter()函数,通过具体的计算公式来返回面积和周长的值。 最后,我们可以在主函数中创建这些形状的对象,并通过基类shape来调用它们的虚函数: ``` int main(){ shape* s1 = new circle(2); shape* s2 = new square(3); shape* s3 = new rectangle(2, 3); shape* s4 = new trapezoid(2, 5, 4, 3, 4); shape* s5 = new triangle(3, 4, 5); cout << "Circle area: " << s1->area() << endl; cout << "Circle perimeter: " << s1->perimeter() << endl; cout << "Square area: " << s2->area() << endl; cout << "Square perimeter: " << s2->perimeter() << endl; cout << "Rectangle area: " << s3->area() << endl; cout << "Rectangle perimeter: " << s3->perimeter() << endl; cout << "Trapezoid area: " << s4->area() << endl; cout << "Trapezoid perimeter: " << s4->perimeter() << endl; cout << "Triangle area: " << s5->area() << endl; cout << "Triangle perimeter: " << s5->perimeter() << endl; return 0; } ``` 这里使用了基类指针指向派生类对象,并通过调用各自的虚函数来获取具体的面积和周长值。需要注意的是,这里调用虚函数时使用了override关键字,表示覆盖了基类中的同名函数。 综上所述,通过编这样一个程序,我们可以更好地理解C++中的面向对象编程基础,包括抽象基类派生类和虚函数,有助于我们提高代码设计的能力和面向对象思维的水平。 ### 回答3: 首先,重点是要理解抽象类和虚函数的概念。抽象类指的是不能被实例化的类,它的目的是为了定义接口,由它派生来的具体类才能被实例化。虚函数指的是在基类中声明为virtual的函数,派生类中可以重该函数以实现自己的行为。 在本题中,我们需要定义一个抽象基类Shape,它包含一个纯虚函数draw()表示这个形状需要能够绘制来。派生类CircleSquareRectangleTrapezoidTriangle都是从Shape派生而来的。它们需要实现Shape定义的虚函数draw(),以自己的方式来绘制对应的形状。 下面是一个示例代码: ```c++ // shape.h #ifndef SHAPE_H_ #define SHAPE_H_ class Shape { public: virtual void draw() = 0; // 纯虚函数,需要在派生类中实现 }; #endif // circle.h #ifndef CIRCLE_H_ #define CIRCLE_H_ #include "shape.h" class Circle : public Shape { public: void draw() override; // 派生类中重载虚函数 }; #endif // circle.cpp #include "circle.h" void Circle::draw() { // 绘制圆形的代码 } // square.h #ifndef SQUARE_H_ #define SQUARE_H_ #include "shape.h" class Square : public Shape { public: void draw() override; }; #endif // square.cpp #include "square.h" void Square::draw() { // 绘制正方形的代码 } // rectangle.h #ifndef RECTANGLE_H_ #define RECTANGLE_H_ #include "shape.h" class Rectangle : public Shape { public: void draw() override; }; #endif // rectangle.cpp #include "rectangle.h" void Rectangle::draw() { // 绘制矩形的代码 } // trapezoid.h #ifndef TRAPEZOID_H_ #define TRAPEZOID_H_ #include "shape.h" class Trapezoid : public Shape { public: void draw() override; }; #endif // trapezoid.cpp #include "trapezoid.h" void Trapezoid::draw() { // 绘制梯形的代码 } // triangle.h #ifndef TRIANGLE_H_ #define TRIANGLE_H_ #include "shape.h" class Triangle : public Shape { public: void draw() override; }; #endif // triangle.cpp #include "triangle.h" void Triangle::draw() { // 绘制三角形的代码 } ``` 这个示例中,我们把抽象基类Shape定义一个单独的头文件中,把每个具体的形状定义在对应的头文件和源文件中。注意到在具体形状的类定义中,重载虚函数时需要加上override关键字,以确保正确地覆盖了基类中的虚函数。 这样,我们就可以使用Shape指针指向任意一个派生类对象,通过虚函数调用实现多态性。例如: ```c++ #include "shape.h" #include "circle.h" #include "square.h" #include "rectangle.h" #include "trapezoid.h" #include "triangle.h" int main() { Shape *shape; Circle circle; Square square; Rectangle rectangle; Trapezoid trapezoid; Triangle triangle; shape = &circle; shape->draw(); // 调用Circle类中覆盖的draw函数 shape = &square; shape->draw(); // 调用Square类中覆盖的draw函数 shape = &rectangle; shape->draw(); // 调用Rectangle类中覆盖的draw函数 shape = &trapezoid; shape->draw(); // 调用Trapezoid类中覆盖的draw函数 shape = &triangle; shape->draw(); // 调用Triangle类中覆盖的draw函数 return 0; } ``` 这个例子中,我们定义5派生类并实例化了它们,然后使用Shape指针分别指向这些对象并调用它们的draw函数,得到了不同的绘制结果。这就是虚函数带来的多态性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值