这是我被赋予的任务
你的任务是创建一个名为 equation 的类,它具有数据成员 a、b 和 c,它们是二次方程的系数。该类将有另外两个数据成员,即 proot 和 nroot,它们代表方程的正根和负根。假设变量 a、b 和 c 是整数。其中 proroot 和 nroot 是浮点数。- 使用空构造函数构造类对象。- 然后设计一个友元函数来确定方程的根和nroot。- 创建另一个友元函数,它将显示 proot 和 nroot 的值。
我有一些问题
我试图将“a”声明为整数并取其平方根,它给出了一个错误,提示“多个 sqrt 实例与参数列表匹配”。当我将“a”声明为双精度并类型转换为整数时,同样的事情。为什么会这样?
输出应该是 -1 -1.5 但我的输出完全不同。我究竟做错了什么?
- 我的教授告诉我,要成为一个类的函数朋友,我们必须在类中编写它的原型。原型不包括“&”,但如果我不写它,程序就不能工作
在此处输入代码
#include <iostream>
#include <math.h>
using namespace std;
class Equation
{
friend void Roots (Equation & );
friend void Display (Equation &);
int a;
int b;
int c;
float proot;
float nroot;
public:
Equation ()
{
a=0;
b=0;
c=0;
proot=0;
nroot=0;
}
Equation (int _a, int _b, int _c)
{
a=_a;
b=_b;
c=_c;
}
};
void Roots (Equation &obj1)
{
double a;
int determinant;
a=(obj1.b^2)-(4*obj1.a * obj1.c);
if (a>-1)
determinant=int(sqrt(a));
else
{
cout<<"Determinant returns an imaginary number; solution not possible\n";
exit (0);
}
obj1.proot= (-obj1.b + determinant)/2*obj1.a;
obj1.nroot= (-obj1.b - determinant)/2*obj1.a;
}
void Display (Equation &obj1)
{
cout<<"Value of positive root : "<<obj1.proot<<endl;
cout<<"Value of negative root : "<<obj1.nroot<<endl;
}
void main ()
{
int a,b,c;
cout<<"Calculate Quadratic Equation"<<endl<<"Enter A : ";
cin>>a;
cout<<"Enter B : ";
cin>>b;
cout<<"Enter C ";
cin>>c;
Equation obj(a,b,c);
Display (obj);
Display (obj);
}