用 C++ 编写原型构造函数
我采用二次表达式,其中 y=ax^2 + bx + c
和 a
,b
,c
是常量,x
是变量。这是我的类:
class quadratic {
public:
double evaluate(const double x);
void getCoefficients (double &A, double &B, double &C);
void setCoefficients (const double A, const double B, const double C);
private:
double a;
double b;
double c;
};
我要为该类创建两个构造函数,以便以下内容合法。
quadratic y1 = quadratic(1.0, -5.0, 7.0);
quadratic y2 = quadratic(-3.0, -2.0, 10.0);
quadratic y3;
默认构造函数应设置为零,而第二个构造函数中的参数指示系数的初始值。
我认为我应该这样做:
quadratic() //default values
{
double A, double B, double C = 0.0;
}
quadratic(double A, double B, double C) //initial values
{
double A = double a;
double B = double b;
double C = double c;
}
但是我不完全理解如何设置它,并且希望任何专家帮助解决这个问题。
I am taking a quadratic expression, where y=ax^2 + bx + c
with a
,b
,c
are constants and x
is a variable. Here is my class:
class quadratic {
public:
double evaluate(const double x);
void getCoefficients (double &A, double &B, double &C);
void setCoefficients (const double A, const double B, const double C);
private:
double a;
double b;
double c;
};
I am to create TWO constructors for the class so that the following is legal
quadratic y1 = quadratic(1.0, -5.0, 7.0);
quadratic y2 = quadratic(-3.0, -2.0, 10.0);
quadratic y3;
The default constructor should be set to zero while the parameters in the second constructor indicate initial values for the coefficients.
Here is how I believe I should do that:
quadratic() //default values
{
double A, double B, double C = 0.0;
}
quadratic(double A, double B, double C) //initial values
{
double A = double a;
double B = double b;
double C = double c;
}
However I'm not fully understanding how to set this up and would appreciate any gurus help in figuring this out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能应该使用构造函数的初始值设定项列表:
上面使用 C++ 语言的一部分来初始化称为初始值设定项列表的成员变量。
您对带参数的构造函数所做的操作:
首先不会编译,但即使您简化了操作:
那么它仍然不会编译,因为 A、B 和 C 已经定义。当您像上面一样在类型名称后面加上变量名称时,它将尝试创建一个新变量。
因此,如果我们再次简化为:
那么这仍然是错误的,但至少它会编译。这是错误的,因为您将参数设置为未初始化的类变量的值。
相反,您想要:
您对不带参数的构造函数所做的操作:
这不会编译。在一行中声明多个变量的正确语法如下:
但这仍然不正确,它只会创建 3 个新变量 A、B 和 C 并将它们初始化为 0。您真正想要的是设置您的成员变量 a、b 和 c。
所以你想要:
a = b = c = 0;
You should probably use the constructors' initializer list instead:
The above uses a part of the C++ language to initialize member variables called an initializer list.
What you did for the constructor with parameters:
First of all won't compile, but even if you simplify what you did to:
Then it is still won't compile because A, B, and C are already defined. When you put a type name followed by a variable name like the above, it will try to create a new variable.
So if we simplify again to:
Then this is still wrong again but at least it will compile. It's wrong because you are setting the parameters to the value of the uninitialized class variables.
Instead you want:
What you did for the constructor without parameters:
This won't compile. The right syntax to declare many variables in a single line is as follows:
But this is still not correct, it will simply create 3 new variables A, B, and C and initialize them to 0. What you really want is to set your member variables a,b, and c.
So you want:
a = b = c = 0;
直接而简单的方法是:
声明第二个不带参数的构造函数并将所有值分配为 0 也是可以接受的,但需要使用更多代码,对我来说,构造函数中的默认值更可取。
这是替代解决方案,除了在构造函数中仅使用 1 或 2 个参数不再有效之外,其他解决方案完全相同:
The direct and simple way of doing this would be:
Declaring a second constructor with no parameters and assigning all values to 0 is also acceptable but uses more code, for me the default values in the constructor are preferable.
This is the alternate solution identical except for the fact that using only 1 or 2 parameters in the constructor is no longer valid:
使用成员初始值设定项列表:
Use the member initializer list:
使用初始化列表来初始化类成员变量。你写的就是作业。
此外,OP 中的代码分配给构造函数中定义的局部变量,而不是类成员变量。我猜其目的是初始化类的成员变量
Use initialization lists to initialize class member variables. What you have written is assignment.
Also the code in OP assigns to local variables defined within the constructor and not the class member variables. I guess the intention was to initialize the member variables of the class