有关c++的一些实验问题

设计、实现一个平面几何体类,成员变量至少包括:名称,面积,周长等 基本信息。成员函数至少包括:构造、析构函数、面积计算、周长计算、信息 显示等。 设计、实现一个矩形类,其基类为平面几何体类,成员变量至少包括:两 个边长(长宽)。成员函数至少包括:输入长、宽,面积计算,周长计算、信 息显示。 设计、实现一个正方形类,其基类为矩形类,成员变量至少包括:边长。 成员函数至少包括:输入边长,面积计算,周长计算、信息显示。 设计、实现一个圆形类,其基类为平面几何体类,成员变量至少包括:半 径参数。成员函数至少包括:输入半径,面积计算,周长计算、信息显示。 用户可选择输入矩形、正方形或圆形,根据用户输入的类型创建对象,并 完成参数的输入、面积和周长的计算。可输入任意数量的图形对象。用户终止 输入后,把所有对象的信息以列表方式显示出来。

基于new bing的编写:

img

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

// 定义平面几何体类
class PlaneGeometry {
protected:
    string name; // 几何体名称
    double area; // 面积
    double perimeter; // 周长
public:
    PlaneGeometry() { // 构造函数
        name = "";
        area = 0.0;
        perimeter = 0.0;
    }
    virtual ~PlaneGeometry() {} // 虚析构函数
    virtual void input_params() = 0; // 输入参数的纯虚函数
    virtual void calc_area() = 0; // 计算面积的纯虚函数
    virtual void calc_perimeter() = 0; // 计算周长的纯虚函数
    void display() { // 显示几何体信息
        cout << "名称:" << name << endl;
        cout << "面积:" << area << endl;
        cout << "周长:" << perimeter << endl;
    }
};

// 定义矩形类,继承自平面几何体类
class Rectangle: public PlaneGeometry {
private:
    double length; // 长
    double width; // 宽
public:
    Rectangle() { // 构造函数
        name = "矩形";
        length = 0.0;
        width = 0.0;
    }
    void input_params() { // 输入参数
        cout << "请输入矩形的长和宽:" << endl;
        cin >> length >> width;
    }
    void calc_area() { // 计算面积
        area = length * width;
    }
    void calc_perimeter() { // 计算周长
        perimeter = 2 * (length + width);
    }
};

// 定义正方形类,继承自矩形类
class Square: public Rectangle {
private:
    double side; // 边长
public:
    Square() { // 构造函数
        name = "正方形";
        side = 0.0;
    }
    void input_params() { // 输入参数
        cout << "请输入正方形的边长:" << endl;
        cin >> side;
    }
    void calc_area() { // 计算面积
        area = side * side;
    }
    void calc_perimeter() { // 计算周长
        perimeter = 4 * side;
    }
};

// 定义圆形类,继承自平面几何体类
class Circle: public PlaneGeometry {
private:
    double radius; // 半径
public:
    Circle() { // 构造函数
        name = "圆形";
        radius = 0.0;
    }
    void input_params() { // 输入参数
        cout << "请输入圆形的半径:" << endl;
        cin >> radius;
    }
    void calc_area() { // 计算面积
        area = radius * radius * 3.14159;
    }
    void calc_perimeter() { // 计算周长
        perimeter = 2 * radius * 3.14159;
    }
};

int main() {
    int choice;
    bool exit_flag = false;
    PlaneGeometry *p_shape = NULL;
    while (!exit_flag) {
        cout << endl;
        cout << "请选择要输入的几何体类型:" << endl;
        cout << "1. 矩形" << endl;
        cout << "2. 正方形" << endl;
        cout << "3. 圆形" << endl;
        cout << "0. 退出" << endl;
        cin >> choice;
        switch (choice) {
            case 1: // 输入矩形
                p_shape = new Rectangle();
                break;
            case 2: // 输入正方形
                p_shape = new Square();
                break;
            case 3: // 输入圆形
                p_shape = new Circle();
                break;
            case 0: // 退出程序
                exit_flag = true;
                break;
            default: // 输入无效
                cout << "输入无效,请重新选择!" << endl;
                continue;
        }
        if (!exit_flag) {
            p_shape->input_params(); // 输入参数
            p_shape->calc_area(); // 计算面积
            p_shape->calc_perimeter(); // 计算周长
            p_shape->display(); // 显示信息
            delete p_shape; // 释放内存
            p_shape = NULL;
        }
    }
    return 0;
}


程序中定义了一个抽象类 PlaneGeometry,所有几何图形类都继承自该类。PlaneGeometry 类具有三个数据成员:几何体名称 name、面积 area 和周长 perimeter,以及三个纯虚函数:input_params()、calc_area() 和calc_perimeter()。这三个纯虚函数用于输入几何体所需的参数、计算几何体的面积和周长,并且在具体的几何体类中被重写。

接下来,程序定义一个 Rectange 类、一个 Square 类和一个 Circle 类,这些类继承 PlaneGeometry 类,并分别实现 input_params()、calc_area() 和calc_perimeter() 函数。这些类可按照用户的选择进行创建:

  • 当用户选择创建矩形时,程序将分配一个 Rectangle 类对象;

  • 当用户选择创建正方形时,程序将分配一个 Square 类对象;

  • 当用户选择创建圆形时,程序将分配一个 Circle 类对象。

主程序中使用了 switch 语句来按顺序处理用户的输入。对于每个输入类型,程序将使用 new 运算符动态分配对象内存。然后,程序使用动态绑定(多态)调用 input_params()、calc_area() 和 calc_perimeter() 函数,并使用 display() 函数输出图形对象的信息。最后,程序使用 delete 运算符释放对象内存。这种方法可以确保进行动态内存分配和释放,并对程序的可扩展性进行了优化。

这个程序使用了面向对象的技术,利用继承和多态特性来解决图形计算的问题,增加程序的可维护性和可扩展性。

#include <iostream>
#include <string>
#include <vector>

#define PI 3.1415927
using namespace std;

class Geometry{
public:
    string name;  // 名称
    double area;  // 面积
    double perimeter;  // 周长
    Geometry(){};  // 无参构造函数
    Geometry(string n):name(n){};
    ~Geometry(){};  // 析构函数
    virtual void input(){}  // 输入信息
    virtual void calcArea(){};  // 面积计算虚函数
    virtual void calcPerimeter(){};  // 周长计算虚函数
    virtual void printInfo(){};  // 信息显示虚函数
};
class Rectangle: public Geometry{
public:
    double length;double width;
    Rectangle(){name="rectangle";};  // 无参构造函数
    //Rectangle(double l,double w):Geometry(string("Rectangle")),length(l),width(w){};
    //Rectangle(string n,double l,double w):Geometry(n),length(l),width(w){};
    ~Rectangle(){};  // 析构函数
    virtual void input(){  // 输入长、宽
        cout << "请输入矩形的长和宽,空格隔开:";
        cin >> length >> width;
    };
    virtual void calcArea(){area = length*width;};  // 面积计算
    virtual void calcPerimeter(){perimeter = 2*(length+width);};  // 周长计算
    virtual void printInfo(){  // 信息显示
        printf("%-10s长=%-7.2f宽=%-7.2f%-7.2f%-7.2f\n",name.c_str(),length,width,area,perimeter);
    };
};
class Square: public Rectangle{
public:
    Square(){name = "square";};
    //Square(double l){name = "square";length = l;};
    //Square(string n, double l){name = n;length = l;};
    void input(){  // 输入边长
        cout << "请输入正方形的边长:";
        cin >> length;
    };
    void calcArea(){area = length*length;};  // 面积计算
    void calcPerimeter(){perimeter = 4*length;};  // 周长计算
    void printInfo(){  // 信息显示
        printf("%-10s边长=%-15.2f%-7.2f%-7.2f\n",name.c_str(),length,area,perimeter);
    };
};
class Circle: public Geometry{
public:
    double radius;
    Circle(){name = "circle";};
    //Circle(double r):Geometry(string("circle")),radius(r){};
    //Circle(string n,double r):Geometry(n),radius(r){};
    void input(){  // 输入半径
        cout << "请输入圆形的半径:";
        cin >> radius;
    };
    void calcArea(){area = PI*radius*radius;};  // 面积计算
    void calcPerimeter(){perimeter = 2*PI*radius;};  // 周长计算
    void printInfo(){  // 信息显示
        printf("%-10s半径=%-15.2f%-7.2f%-7.2f\n",name.c_str(),radius,area,perimeter);
    };
};

int main(){
    vector<Geometry*> v;  // 储存平面几何体
    // int num = 0;  // 记录平面几何体的个数
    char inp = ' ';  // 记录用户的选择
    Geometry * g=nullptr;
    while(inp != 'q'){
        cout << "添加平面几何体:0-矩形 1-正方形 2-圆形 q-结束输入 : ";
        cin >> inp;
        if(inp == 'q') break;
        if(inp == '0'){
            g = new Rectangle();
            g->input();
            g->calcArea();g->calcPerimeter();
            v.push_back((Geometry*)g);
        } else if(inp == '1') {
            g = new Square();
            g->input();
            g->calcArea();g->calcPerimeter();
            v.push_back((Geometry*)g);
        } else if(inp == '2'){
            g = new Circle();
            g->input();
            g->calcArea();g->calcPerimeter();
            v.push_back((Geometry*)g);
        } else {
            cout << "输入有误,请重新输入" << endl;
        }
    }
    printf("%-10s%-20s%-7s%-7s\n","name", "info", "area", "perimeter");
    for(Geometry * & ge: v){
        ge->printInfo();
        delete ge;
        ge = nullptr;
    }
    return 0;
}

img


他说要任意数量图形,我用了vector。用cout进行格式化输出没学好,改用了printf

前言

创作不易,如有帮助给个采纳

效果截图

img

源码

#include <iostream>
#include <string>
#include <cmath>
#include <vector>

using namespace std;

// 平面几何体类
class Shape {
protected:
    string name;  // 名称
    double area;  // 面积
    double perimeter;  // 周长

public:
    Shape(const string& n = ""): name(n) {}
    virtual ~Shape() {}

    virtual void input() = 0;  // 输入数据
    virtual void calcArea() = 0;  // 计算面积
    virtual void calcPerimeter() = 0;  // 计算周长
    virtual void display() const {  // 显示信息
        cout << "名称:" << name << endl;
        cout << "面积:" << area << endl;
        cout << "周长:" << perimeter << endl;
    }
};

// 矩形类
class Rectangle : public Shape {
private:
    double length;  // 长
    double width;  // 宽

public:
    Rectangle(): Shape("矩形"), length(0), width(0) {}
    ~Rectangle() {}

    void input() override {
        cout << "请输入矩形的长和宽:";
        cin >> length >> width;
    }

    void calcArea() override {
        area = length * width;
    }

    void calcPerimeter() override {
        perimeter = 2 * (length + width);
    }

    void display() const override {
        Shape::display();
        cout << "长:" << length << endl;
        cout << "宽:" << width << endl;
    }

    void setLength(double l) {
        length = l;
    }

    void setWidth(double w) {
        width = w;
    }
};

// 正方形类
class Square : public Rectangle {
public:
    Square(): Rectangle() {
        name = "正方形";
    }
    ~Square() {}

    void input() override {
        cout << "请输入正方形的边长:";
        double len;
        cin >> len;
        setLength(len);
        setWidth(len);
    }
};

// 圆形类
class Circle : public Shape {
private:
    double radius;  // 半径

public:
    Circle(): Shape("圆形"), radius(0) {}
    ~Circle() {}

    void input() override {
        cout << "请输入圆形的半径:";
        cin >> radius;
    }

    void calcArea() override {
        area = M_PI * pow(radius, 2);
    }

    void calcPerimeter() override {
        perimeter = 2 * M_PI * radius;
    }

    void display() const override {
        Shape::display();
        cout << "半径:" << radius << endl;
    }
};

int main() {
    vector<Shape*> shapes;

    char choice;
    do {
        cout << "请选择要创建的图形类型(R-矩形,S-正方形,C-圆形,Q-退出):";
        cin >> choice;
        switch (toupper(choice)) {
            case 'R':
                shapes.push_back(new Rectangle());
                break;
            case 'S':
                shapes.push_back(new Square());
                break;
            case 'C':
                shapes.push_back(new Circle());
                break;
            default:
                break;
        }
        if (toupper(choice) != 'Q') {
            shapes.back()->input();  // 输入数据
            shapes.back()->calcArea();  // 计算面积
            shapes.back()->calcPerimeter();  // 计算周长
        }
    } while (toupper(choice) != 'Q');

    // 显示信息
    cout << "所有图形的信息如下:" << endl;
    for (auto p = shapes.begin(); p != shapes.end(); ++p) {
        (*p)->display();
        cout << endl;
    }

    // 释放内存
    for (auto p = shapes.begin(); p != shapes.end(); ++p) {
        delete *p;
    }

    return 0;
}


寄语

需要代码分析 再告诉我

可以借鉴下



#include <iostream>
#include <string>
#include <math.h>
using namespace std;
class Shape
{
public:
    virtual double area() = 0;
    virtual double Ctrcumference() = 0;

};

class Circle : public Shape
{
public:
    Circle(double r) :radius(r) {}
    virtual double area() { return 3.14 * radius * radius; }
    virtual double Ctrcumference() { return 2 * 3.14 * radius; }

protected:
    double radius;
};

class Recentage : public Shape
{
public:
    Recentage(double l, double w) : longth(l), width(w) {}
    virtual double area() { return longth * width; }
    virtual double Ctrcumference() { return 2 * (longth * width); }

protected:
    double longth;
    double width;
};


class Triangle : public Shape
{
public:
    Triangle(double l, double w) : longth(l), width(w) {}
    virtual double area()   { return  0.5 * longth * width; }
    virtual double Ctrcumference() { return (longth + width + sqrt(longth * longth + width * width)); }

protected:
    double longth;
    double width;
};
class Ellipse : public Shape
{
public:
    Ellipse(double sa, double la) { short_as = sa; long_as = la; }
    virtual double area() { return 3.14 * short_as * long_as; }
    virtual double Ctrcumference() { return (2 * 3.14 * short_as + 4 * (long_as - short_as)); }
protected:
    
    double short_as;
    double long_as;


};



void printArea_Ctr(Shape& s)
{
    cout <<"面积:"<< s.area() <<","<< "周长:"<<s.Ctrcumference() << endl;
}


void Meum_Selcet()
{
    cout << "-----------欢迎使用几何图形运算-----------------" << endl;
    cout << "           number 0: circle(圆)" << endl;
    cout << "           number 1: Recentage(矩形)" << endl;
    cout << "           number 2: Triangle(三角形)" << endl;
    cout << "           number 3: ellipse(椭圆)" << endl;
    cout << "           number else: 四种几何图形的周长,面积总和 " << endl;

}

int main()
{
    Meum_Selcet();
loop:
    {
        double ToTal_Area = 0;
        double ToTal_Ctr = 0;

        cout << "Please choose the parterns of the Geometric Calculation:" << endl;
        int choose = 0;
        cin >> choose;
        switch (choose)
         {
            case 0:
            {
                double r = 0;
                cout << "Please enter the menbers of Circle:" << endl;
                cin >> r;
                Circle circle(r);
                cout << "the area and the ctrcumference of the circle:" << endl;
                printArea_Ctr(circle);

            }; break;
            case 1:
            {    cout << "Please enter the menbers of recentage:" << endl;
                double lo = 0; double  wi = 0;
                cin >> lo >> wi;
                Recentage recentage(lo, wi);
                cout << "the area and the ctrcumference of the recentage:" << endl;
                printArea_Ctr(recentage);
            }; break;
            case 2:
            {    cout << "Please enter the menbers of triangle:" << endl;
                double lt = 0; double wt = 0;
                cin >> lt >> wt;
                Triangle triangle(lt, wt);
                cout << "the area  and the ctrcumference of the triangle:" << endl;
                printArea_Ctr(triangle);
            }; break;
            case 3:
            {
                cout << "Please enter the menbers of ellipse:" << endl;
                double sl = 0; double ll = 0;
                cin >> sl >> ll;
                Ellipse ellipse(sl, ll);
                cout << "the area and the ctrcumference of ellipse:" << endl;
                printArea_Ctr(ellipse);
            }; break;
            default:
            {   cout << "Please enter the parameters of the four Geometry: " << endl;
                cout << "圆半径,矩形长宽,三角形底和高,椭圆长短半轴:" << endl;
                double r1, lo1, wi1, lt1, wt1, sl1, ll1;
                cin >> r1 >> lo1 >> wi1 >> lt1 >> wt1 >> sl1 >> ll1;
                Circle circle(r1);
                Recentage recentage(lo1, wi1);
                Triangle triangle(lt1, wt1);
                Ellipse ellipse(sl1, ll1);
                ToTal_Area = circle.area() + recentage.area() + triangle.area() + ellipse.area();
                ToTal_Ctr = circle.Ctrcumference() + recentage.Ctrcumference() + triangle.Ctrcumference() + ellipse.Ctrcumference();
                cout << "派生类对象的面积之和、周长之和:" << endl;
                cout << ToTal_Area << "," << ToTal_Ctr << endl;
            }; break;
        }
        cout << '\n' << "输入Left离开计算界面/输入Continue继续进行运算" << endl;
        string ch;
        cin >> ch;
        if (ch == "Left")
            cout << "————————感谢使用几何图形运算!——————————" << endl;

        else if (ch == "Continue")
            goto loop;
        else
            cout << "请重新选择(Left or Continue)!" << endl;

    }
    return 0;


}