我有一个任务要用数字制作一个类,并且我必须使用构造函数来实现它。问题是我的写功能只能正确地写入给定电路的值,而不能写入极点或电路。而不是写“ nan”。
我添加了“调试提示”,以检查该功能是否正确计数了极点和电路。
#include <iostream>
#include <cmath>
class Figura
{
protected:
double pole;
double circuit;
public:
virtual double funkcja_pole(double A) = 0;
virtual double funkcja_circuit(double B) = 0;
virtual void write() = 0;
Figura(double, double);
};
class Circle: public Figura
{
private:
double radius;
public:
Circle(double);
void write()
{
std::cout << "Radius = " << radius << std::endl;
std::cout << "Pole = " << pole << std::endl;
std::cout << "Circuit = " << circuit << std::endl;
}
double funkcja_pole(double A)
{
pole = 3.14 * A*A;
std::cout << "pole:" << pole << std::endl;
}
double funkcja_circuit(double B)
{
circuit = 3.14 * 2*B;
std::cout << "circuit:" << circuit << std::endl;
}
};
Figura::Figura(double POLE, double CIRCUIT):
pole(POLE), circuit(CIRCUIT) {}
Circle::Circle(double RADIUS):
radius(RADIUS), Figura(funkcja_pole(RADIUS),funkcja_circuit(RADIUS)) {}
int main()
{
Circle my_circle(2);
Figura *wsk;
wsk = &my_circle;
wsk->write();
}
结果:
最佳答案
您不返回计算值:
double funkcja_pole(double A)
{
double pole = 3.14 * A*A;
std::cout << "pole:" << pole << std::endl;
return pole;
}
您应该重构代码,以使所有内容都更有条理:
class Circle: public Figura
{
private:
double radius;
public:
Circle(double);
void write()
{
std::cout << "Radius = " << radius << std::endl;
std::cout << "Pole = " << pole << std::endl;
std::cout << "Circuit = " << circuit << std::endl;
}
private:
double funkcja_pole(double A)
{
double pole = 3.14 * A*A;
std::cout << "pole:" << pole << std::endl;
return pole;
}
double funkcja_circuit(double B)
{
double circuit = 3.14 * 2*B;
std::cout << "circuit:" << circuit << std::endl;
return circuit;
}
};
Figura::Figura(double POLE, double CIRCUIT):
pole(POLE), circuit(CIRCUIT) {}
Circle::Circle(double RADIUS):
Figura(funkcja_pole(RADIUS),funkcja_circuit(RADIUS)), radius(RADIUS) {}
关于c++ - C++ write_function写入“Nan”而不是适当的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53416426/