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

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

问题描述:

//#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;

}

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

麻烦用代码块模式粘贴一下代码..

你断点跟啊,看走到哪一步半径就变成负数了
代码在自己手里,编辑器也在自己手里,自己调试一下不就行了



```c++
//Shape.h
#pragma once
#include<iostream>
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
#pragma once
#include<iostream>
#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<iostream>
#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<iostream>
#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.cpp
#include<iostream>
#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;
}

```