【阿锤】 2021-09-13 15:25 采纳率: 100%
浏览 33
已结题

程序编译后,编译结果前两条会出现半径是负数,面积是科学计数法的结果,为什么会出现这种结果,该怎样解决?

//#pragma once
//Shape.h头文件
#include//
using namespace std;
//建立形状类
class Shape
{
public:
Shape(int a = 0, int b = 0);
void draw();
void set(int a,int b);//设置x和y
int getx();//获取x
int gety();//获取y/
private:
int x;//横坐标
int y;//纵坐标
};
//

  • //Circle.h头文件
    #include//
    #include"Shape.h"
    using namespace std;

class Circle :public Shape
{
int r;//圆的半径
public:
Circle(int x,int y,int c);
void Setr(int c);//设置半径r
double Area();//面积
void Move(int x_offset, int y_offset);//修改坐标
void drawCircle();
};
//
//Shape.cpp
#include//
#include"Shape.h"//
using namespace std;
const double Pi = 3.14159;
//初始化参数列表
Shape::Shape(int a,int b)
{
x = a;
y = b;
}
void Shape::draw()
{
cout << "draw in Shape:( "<< x << ',' << y<< ")"<< endl;
}
//设置xy的值
void Shape::set(int a, int b)
{
x = a;
y = b;
}
//获取x值
int Shape::getx()
{
return x;
}
//获取y值
int Shape::gety()
{
return y;
}
//
//Circle.cpp
#include//
#include"Circle.h"
using namespace std;
const double Pi = 3.14159;
//初始化成员列表
Circle::Circle(int x, int y, int c):Shape(x,y)
{
int r = c;
}
//设置半径
void Circle::Setr(int c=0)
{
r = c;
}
//面积
double Circle::Area()
{
return Pi * r*r ;
}
//修改坐标
void Circle::Move(int x_offset, int y_offset)
{
int x1 = getx();
int y1 = gety();
x1 += x_offset, y1 += y_offset;
set(x1, y1);
}
//打印
void Circle::drawCircle()
{
draw();
cout << "draw in circle:" << " ";
cout << "半径:" << r << " ";
cout << "面积:" << Area() << endl;
}
//
//main函数
#include//
#include"Circle.h"
using namespace std;
int main()
{
Circle s(3,3,3);
s.drawCircle();
s.Move(1, 2);
s.drawCircle();
s.set(4, 5);
s.Setr(3);
s.drawCircle();
s.Setr(5);
s.drawCircle();
system("pause");
return 0;

}

  • 写回答

4条回答 默认 最新

  • CSDN专家-link 2021-09-13 15:52
    关注

    编译结果前两条会出现半径是负数---编译只会产生可执行文件,不会运行可执行文件,所以不会产生什么“结果”。要明白编译和运行的区别
    Circle::Circle(int x, int y, int c):Shape(x,y)
    {
    int r = c;
    }
    这是不对的,构造函数你又写了一个 int r,这是定义临时变量,不是使用你类变量,把int去掉

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 9月21日
  • 已采纳回答 9月13日
  • 创建了问题 9月13日

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行