Also the correct answer for this equation should be "4 and -1". |
Ah OK, I called the setvalues function and then the area functions. Since D hadn't been assigned a valid value yet, the result wasn't correct. d and D are only set in the friend function.
I might make the find_d function a void function, passing in a Qeq object by reference, so the function updates d and D without having to create a new QEq object within the function.
1 2 3 4 5
|
void find_d (QEq & obj)
{
obj.d=(obj.b*obj.b)-(4*obj.a*obj.c);
obj.D=sqrt(ob.d);
}
|
Constructors are unique functions in that they have no return type at all, not even void. A default constructor takes no parameters but can be used to initialize data members to a default value - in this case I would just make sure that
a
does not get set to 0 (at any time, not just the constructor), to avoid divide by zero errors.
1 2 3 4
|
Qeq() {
//default constructor
//assign default values to the class data members
}
|
So when you create an object in main like this, the default constructor is called.
QEq ob;
You can also have a constructor that takes parameters.
1 2 3 4
|
Qeq(float A, float B, float C) {
//constructor
//assign passed in values to the class data members
}
|
So when you create an object in main like this, this constructor that takes three float parameters is called.
QEq ob(2.3, 4.5, 6.6);
A copy constructor would allow you to do something like this.
1 2
|
QEq ob1;
QEq ob2(ob1);
|
It can also be useful to overload the assignment operator so that you can do something like this:
1 2 3
|
QEq ob1(2.3, 4.5, 6.6);
QEq ob2;
ob2 = ob1;
|
There's more reading on those here :)
http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/