日系奥利奥 2022-11-01 18:00 采纳率: 84.2%
浏览 20
已结题

请指正一下。问题有点多。

#include
using namespace std;
class triangle
{private:double x;
double y;
double z;
public:triangle(double a,double b,double c)
{x=a;y=b;z=c;}
triangle();
void input()
{cin>>x>>y>>z;}
double area1()
{
if(x+y>z||x+z>y||y+z>x)
{ double s,p;
p=x+y+z;
s=sqrt((p-x)(p-y)(p-z));//海伦公式求三角形面积
return s;
}
else return 0;}
};
class three:public triangle
{private: double h,a,b,c;
public:three(double x,double y,double z,double d):triangle(x,y,z)
{h=d;}
three();
void input(){cout<<h;}
double area2(){return area1()*h;}
};
void main()
{ triangle t1;
t1.input();
cout<<t1.area1();
three t2;
t2.input();
cout<<t2.area2();
}

img

  • 写回答

2条回答 默认 最新

  • JarodYv 人工智能领域新星创作者 2022-11-01 19:01
    关注

    你的代码存在2个问题:

    1. 面向对象编程语法不对;
    2. 海伦公式写的不对

    我都帮你改了,代码见下方:

    
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    class triangle
    {
    protected:
        double x;
        double y;
        double z;
    
    public:
        triangle();
        ~triangle();
    
        triangle(double a, double b, double c);
    
    public:
        void input();
    
        double area1();
    };
    
    triangle::triangle()
    {
    }
    
    triangle::~triangle()
    {
    }
    
    triangle::triangle(double a, double b, double c)
    {
        x = a;
        y = b;
        z = c;
    }
    
    void triangle::input()
    {
        cin >> x >> y >> z;
    }
    
    double triangle::area1()
    {
        if (x + y > z || x + z > y || y + z > x)
        {
            double s, p;
            p = (x + y + z) / 2.0;
            s = sqrt(p * (p - x) * (p - y) * (p - z)); //海伦公式求三角形面积
            return s;
        }
        else
            return 0;
    }
    
    class three : public triangle
    {
    private:
        double h;
    
    public:
        three();
        ~three();
    
        three(double x, double y, double z, double d);
    
    public:
        void input();
    
        double area2();
    };
    
    three::three()
    {
    }
    
    three::~three()
    {
    }
    
    three::three(double x, double y, double z, double d) : triangle(x, y, z)
    {
        h = d;
    }
    
    void three::input()
    {
        triangle::input();
        cin >> h;
    }
    
    double three::area2() { return area1() * h; }
    
    int main()
    {
        triangle t1;
        t1.input();
        cout << t1.area1() << endl;
    
        three t2;
        t2.input();
        cout << t2.area2() << endl;
    
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月10日
  • 已采纳回答 11月2日
  • 创建了问题 11月1日

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题