Copy Constructor in a class

Hey guys totally new to c++ but have a question:)
I need to create a quadratic equation solver with set coefficients.. so far i have created a class. Im having trouble setting the values of a b and c to a given number. I then need to make a copy constructer for this.

I know how to set the coefficients if it was not in a class e.g
1
2
3
4
5
 
double a, b, c;
a = 4;
b = 6 ;
c = 1;


but if i want it in the format of class const(a,b,c) how would i do it and use a copy constructor?

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <math.h>

using namespace std;

class quad
{

public:
	quad constant (double a, double b, double c);

	
};

int main ()
{
	return 0;
}


Much appreciated if someone can help me or guide me in the right direction
Just this?
1
2
3
4
5
6
7
8
class quad
{
public:
	quad(double A, double B, double C): a(A), b(B), c(C) {}// constructor
	quad( const quad& q): a(q.a), b(q.b), c(q.c) {}// copy ctor. The default version should be fine though as it does the same
private:
	double a, b, c;
};
Thanks! if i wanted to print the values for a b and c would it be

1
2
3
4
int main()
{
cout << "values are: " << get.quad << endl;
}
Unfortunately, it tends to get much uglier than get.quad. (Nice try.)

http://www.parashift.com/c++-faq/output-operator.html

You basically "teach" cout how to display your quad objects.
It's either that, or write an internal (and parasitic) quad::print() function.
Thanks thats helped alot! spent a few days trying to figure out how to write this code! now im a quarter way there lol
i have been trying to compute the roots, everything compiles perfectly, but the result it returns in the terminal is "-nan"

any idea what this means? the values for a b and c are a=3 b=2 c=0
so its should return root1 = 0 and root 2 = -0.67 if i am correct

minor coding error on my part on that!

my last and final question...

if i have

1
2
3
4
void first_root()
{
	Root1=(-b+sqrt(d))/(2*a);
}


and i want to get the value for root one and print it in another void how is this possible?

would it be

1
2
3
4
void root1solution()
{
	cout << "Root 1:"<< first_root()<< endl;
}
but the result it returns in the terminal is "-nan"

NaN usually stands for Not a Number. It can represent infinity, or whatever else isn't a valid number.

my last and final question...

Ask as few as possible, but as many as needed.

and i want to get the value for root one and print it in another void how is this possible?

I don't know what language you're coming from, but you'll have to spend some time learning the basics of C++. The link below explains functions.
http://cplusplus.com/doc/tutorial/functions/

To answer your question, it'd be closer to:
1
2
3
4
5
double first_root()
{
    // where do you get b, d and a from?
    return (-b + sqrt(d)) / (2*a);
}


When called, the function first_root() evaluates to what it returns. Which is what cout then prints.
Topic archived. No new replies allowed.