views:

192

answers:

4

homework help time!

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.

+1  A: 

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)
{ 
}
Chubsdad
+18  A: 

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;

Brian R. Bondy
oh wow this makes a lot more sense then what i had, and is cleaner and smaller code. greatly appreciate this.
HollerTrain
+1 excellent help and sorting out the askers confusion wrt to declaration.
Elemental
@HollerTrain: I made a small edit btw so if you copied out the code re-copy it out.
Brian R. Bondy
np i just saw that. now i'm trying to figure out how to implement it... ;)
HollerTrain
if he uses `const` for `a`, `b` and `c` he wont be able to use the setCoefficients method.
João Portela
@João Portela: Ah ya good point, I didn't look at the rest of his interface. I removed that part of my answer.
Brian R. Bondy
Nice answer. Only thing I'm missing is a mention of why the initializer list is preferable over just setting the variables in the ctor body. But +1 from me in any case :)
jalf
Why not just use a default constructor taking 3 `double`s?
John Dibling
+2  A: 

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_)
{}
Anthony Williams
ah perfect sense. what is best way to implement this in the main function?
HollerTrain
+3  A: 

The direct and simple way of doing this would be:

class quadratic {
 public:
  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;
};
Elemental
+1 Although correct it is not what is spec'ed out "am to create TWO constructors for the class so that the following is legal", yours gives one constructor and allows it to be called in 4 diff ways.
Brian R. Bondy
In light of the fact that as you say this can be called in 4 ways rather than the two I modded the code to include the explicit default constructor
Elemental
You might want to add the `explicit` keyword to the ctor-declaration in order to prevent it from being used in a double-to-quadratic-conversion.
Dirk D
@Drik - indeed the explicit keyword would improve the first example - good idea
Elemental