开发者

Writing a Prototype Constructor in 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.


You should probably use the constructors' initializer list instead:

quadratic() : a(0), b(0), c(0)
{
}

quadratic(double A, double B, double C) : a(A), b(B), c(C)
{
}

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:

double A = double a;
double B = double b;
double C = double c;

First of all won't compile, but even if you simplify what you did to:

double A = a;
double B = b;
double C = c;

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:

A = a;
B = b;
C = c;

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:

a = A;
b = B;
c = C;

What you did for the constructor without parameters:

 double A, double B, double C = 0.0;

This won't compile. The right syntax to declare many variables in a single line is as follows:

 double A = 0, B = 0, C = 0;

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;


The direct and simple way of doing this would be:

class quadratic {
 public:
  explicit quadratic(double A=0,double B=0,double C=0): a(A),b(B),c(C) { }
  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;
};

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:

class quadratic {
 public:
  quadratic(double A,double B,double C): a(A),b(B),c(C) { }
  quadratic(): a(0), b(0), c(0) { }
  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;
};


Use the member initializer list:

quadratic():
    a(0),b(0),c(0)
{}

quadratic(double a_,double b_,double c_):
    a(a_),b(b_),c(c_)
{}


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

quadratic() : a(0.0), b(0.0), c(0.0)
{
} 

quadratic(double A, double B, double C)  : a(A), b(B), c(C)
{ 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜