I have made class which represents Line Linia (aX+bY=c) and I overloaded + operator so now it now returns new Linia object which has c = c + b/argument. But the problem is that when I use this operator all the fields of the given Line object become 0
#include <iostream>
using namespace std;
struct Q{
public:
double x,y;
Q(double x, double y){
this->x = x;
this->y = y;
}
friend ostream& operator<< (ostream &wyjscie, Q const& ex){
wyjscie<<"("<<ex.x<<","<<ex.y<<")";
return wyjscie;
}
};
class Linia{
public:
double a,b,c;
Linia (double a, double b, double c){
this->a = a;
this->b = b;
this->c = c;
}
Linia operator+ (double i){
return Linia(a, b, c + i/b);
}
Linia operator- (double i){
return Linia(a, b, c - i/b);
}
Q operator* (const Linia& i){
double w = a*i.b - b*i.a;
double wx = -c*i.b + i.c*b;
double wy = a*(-i.c) + i.c*c;
double x = wx/w, y = wy/w;
cout<<*this<<endl;
cout<<i<<endl;
return Q(x,y);
}
friend ostream& operator<< (ostream &wyjscie, Linia const& ex){
wyjscie<<ex.a<<"x + "<<ex.b<<"y = "<<ex.c;
return wyjscie;
}
};//podwyzszenie przez ile/B
int main()
{
Linia* pionowa = new Linia(0,1,0);
Linia* l = new Linia(1,1,3);
// Q q = (*l) * (*pionowa);
cout<<"linia przed podniesieniem "<<*l<<endl;
// cout<<"punkt przeciecia przed podniesieniem: "<<q<<endl;
l = l+3;
cout<<"Line highered"<<*l<<endl;
l = l-3;
cout<<"Line Lowered "<<*l<<endl;
// q = (*l) * (*pionowa);
// cout<<"punkt przeciecia po podniesieniu: "<<q<<endl;
cout << "Hello world!" << endl;
return 0;
}
You are doing pointer arithmetic here. That means that l ends up pointing to some address where is shouldn't:
Linia* l = new Linia(1,1,3);
l = l+3; // l is a pointer!!!
If you stop using new and raw pointers everywhere it might just work.
int main()
{
Linia pionowa(0,1,0);
Linia l(1,1,3);
l = l+3;
cout<<"Line highered"<< l <<endl;
l = l-3;
cout<<"Line Lowered "<< l <<endl;
}
Related
I was trying to make a Complex class which would represent the Complex numbers in c++ but I have encountered an error. The copy constructor seems to be the problem but I am not sure as to what is wrong or why it is wrong. The / operator works well as I return a reference to an object of Complex class. but the * operator does not works when I return an object of Complex class. Please explain this behaviour,
edit: I have used the new operator and a custom copy constructor as it was a requirement of my school to dynamically allocate memory.
here is my code.
#include <iostream>
using namespace std;
class Complex
{
public:
double r, i;
Complex() : r(0), i(0) {}
Complex(int r, int i)
{
this->r = double(r);
this->i = double(i);
}
Complex(double r, int i)
{
this->r = r;
this->i = double(i);
}
Complex(int r, double i)
{
this->r = double(r);
this->i = i;
}
Complex(double r, double i)
{
this->r = r;
this->i = i;
}
Complex(Complex &c)
{
cout<<"cpy constructor called"<<endl;
r = c.r;
i = c.i;
}
void operator()(float r, float i)
{
this->r = r;
this->i = i;
}
void operator()(int r, int i)
{
this->r = r;
this->i = i;
}
Complex operator+(Complex &c)
{
Complex *c1 = new Complex(r + c.r, i + c.i);
return *c1;
}
Complex operator-(Complex &c)
{
Complex *c1 = new Complex(r - c.r, i - c.i);
return *c1;
}
Complex operator*(Complex &c)
{
Complex *c1 = new Complex(
r * c.r + (-1) * (i * c.i),
r * c.i + i * c.r);
return *c1;
}
Complex& operator/(Complex &c)
{
float dem;
Complex num;
num = (*this) * * (new Complex(c.r,-c.i));
dem = c.r*c.r + (c.i*c.i);
Complex *c1 = new Complex(num.r/dem,num.i/dem);
return *c1;
}
operator string()
{
string temp, s;
temp = to_string(r);
while (true)
{
if (temp[temp.length() - 1] == '0')
{
temp.pop_back();
}
else
break;
}
if (temp[temp.length() - 1] == '.')
temp.pop_back();
s += temp;
if (i >= 0)
{
s += '+';
}
temp = to_string(i);
while (true)
{
if (temp[temp.length() - 1] == '0')
{
temp.pop_back();
}
else
break;
}
if (temp[temp.length() - 1] == '.')
temp.pop_back();
s += temp;
s += 'i';
return s;
}
friend ostream &operator<<(ostream &out, Complex c);
};
ostream& operator<<(ostream &out, Complex c)
{
out << string(c);
return out;
}
int main()
{
Complex c(5, 5);
Complex d(5,5);
cout << c/d <<endl;
cout<< c*d <<endl;
}
and this is the error I encounter after this
test.cpp: In function 'int main()':
test.cpp:123:13: error: invalid initialization of non-const reference of type 'Complex&' from an rvalue of type 'Complex'
cout<< c*d <<endl;
~^~
test.cpp:31:5: note: initializing argument 1 of 'Complex::Complex(Complex&)'
Complex(Complex &c)
^~~~~~~
test.cpp:111:10: note: initializing argument 2 of 'std::ostream& operator<<(std::ostream&, Complex)'
ostream& operator<<(ostream &out, Complex c)
This is the image to the error
Repeat the pattern of the code below for your other operators. Note the operator takes the parameter by const ref and returns the result by value:
Complex operator+(Complex const &c)
{
return Complex(r + c.r, i + c.i);
}
The copy constructor should also be changed to take it's parameter by const ref ie
Complex(Complex const &c)
{
r = c.r;
i = c.i;
}
The minimal fix isComplex(Complex &c) -> Complex(const Complex &c)
But then there will remain leaks due to the inappropriate usage of new there, after some cleanup:
#include <iostream>
using namespace std;
class Complex
{
public:
double r, i;
Complex(double r, double i) : r(r), i(i) {}
Complex operator+(Complex c)
{
return Complex(r + c.r, i + c.i);
}
Complex operator-(Complex c)
{
return Complex(r - c.r, i - c.i);
}
Complex operator*(Complex c)
{
return Complex(
r * c.r + (-1) * (i * c.i),
r * c.i + i * c.r);
}
Complex operator/(Complex c)
{
auto num = Complex (*this) * Complex(c.r,-c.i);
auto dem = c.r*c.r + (c.i*c.i);
return Complex(num.r/dem,num.i/dem);
}
operator string()
{
string temp, s;
temp = to_string(r);
while (!temp.empty() && temp.back() == '0')
temp.pop_back();
if (!temp.empty() && temp.back() == '.')
temp.pop_back();
s += temp;
if (i >= 0)
{
s += '+';
}
temp = to_string(i);
while (!temp.empty() && temp.back() == '0')
temp.pop_back();
if (!temp.empty() && temp.back() == '.')
temp.pop_back();
s += temp;
s += 'i';
return s;
}
friend ostream &operator<<(ostream &out, Complex c);
};
ostream& operator<<(ostream &out, Complex c)
{
out << string(c);
return out;
}
int main()
{
Complex c(5, 5);
Complex d(5,5);
cout << c/d <<endl;
cout<< c*d <<endl;
}
The big problem is lack of const on your references and members. The second problem is not using =default. The third problem is too many overloads. Forth, your new use is wrong. operstor() are all wrong. The last problem is your operator strategy. And don't use namespace std;.
struct Complex
Complex is both default public and basically an aggregate. I'd use struct
{
double r=0
double i=0;
you can do default initialization in C++ now.
Complex()=default;
now this ctor is auto-written, because we initialized the members at declaration. DRY is "don't repeat yourself"; when easy, don't repeat member names.
Complex(double r_in, double i_in):r(r_in),i(i_in){}
ctors should construct, not assign, members.
Drop the other 2 arg ctors. Callers can convert.
Complex(Complex const&c)=default;
const here, and let the compiler write a member-wise copy with =default; DRY.
Complex(Complex &&c)=default;
Complex& operator=(Complex const&c)=default;
Complex& operator=(Complex &&c)=default;
also assignment and moves
Now other operators...
Complex& operator+=(Complex const& o)&{
implement a+=b before +.
r+=o.r;
i+=o.i;
return *this;
}
here we repeat member names; it isn't easy to avoid.
Do the same for -= and *=
Next,
friend Complex operator+(Complex lhs, Complex const& rhs){
lhs+=rhs;
return lhs;
}
this makes a non-member +. It makes a copy of its left hand argument by taking it by value, and a const reference to its right hand argument.
We then increment the left hand copy by the right, and return it.
This pattern chans wonderfully in 99% of situations;
Complex x=a+b+c
becomes
Complex x=a;
x+=b;
x+=c;
after accounting for moves and elision.
And you get a+=b; and a+b by writing only one member-wise operation.
Just don't use your operator(), they are nonsense.
I've been having trouble trying to implement objects from class Cart_Vector in class Cart_Point. My compiler has been listing the following errors and I can't seem to fix them:
friend Cart_Point operator+(const Cart_Point&p1,const Cart_Vector&v1);
'Cart_Vector' does not name a type
Cart_Point operator+(const Cart_Point&p1, const Cart_Vector&v1)
'Cart_Vector' does not have a type
x = p1.x + v1.x; request for member 'x' in 'v1', which is of non-class
type 'const int'
y = p1.y + v1.y; request for member 'y' in 'v1', which is of non-class
type 'const int'
return Cart_Vector(x,y); 'Cart_Vector' was not declared in this scope
#include <iostream>
#include <math.h>
using namespace std;
class Cart_Point
{
public:
double x;
double y;
friend class Cart_Vector;
Cart_Point (double inputx, double inputy);
friend Cart_Point operator<<(const Cart_Point&p1, const Cart_Point&p2);
friend Cart_Point operator+(const Cart_Point&p1,const Cart_Vector&v1);
friend Cart_Point operator-(const Cart_Point&p1,const Cart_Point&p2);
double Cart_distance(Cart_Point, Cart_Point);
};
Cart_Point::Cart_Point(double inputx, double inputy)
{
x = inputx;
y = inputy;
}
double Cart_Point::Cart_distance(Cart_Point p1, Cart_Point p2)
{
double distance = (sqrt( pow(p1.x - p2.x,2) + pow(p1.y - p2.y,2) ));
return distance;
//returns distance between p1 (point 1) and p2 (point 2)
}
Cart_Point operator<<(const Cart_Point&p1, const Cart_Point&p2)
{
cout << "p1:(" << p1.x << ", " << p1.y << ")" << endl;
cout << "p2:(" << p2.x << ", " << p2.y << ")" << endl;
return p1,p2;
//this function should just print each point
}
Cart_Point operator+(const Cart_Point&p1, const Cart_Vector&v1)
{
double x,y;
x = p1.x + v1.x;
y = p1.y + v1.y;
return Cart_Point(x,y);
//this function should make a new Cart_Point
}
Cart_Point operator-(const Cart_Point&p1, const Cart_Point&p2)
{
double x,y;
x = p1.x- p2.x;
y = p1.y - p2.y;
return Cart_Vector(x,y);
//this function should make a new Cart_Vector
}
class Cart_Vector
{
public:
double x; //x displacement of vector
double y; //y displacement of vector
Cart_Vector(double inputx, double inputy);
friend Cart_Vector operator*(const Cart_Vector&v1, double d);
friend Cart_Vector operator/(const Cart_Vector&v1, double d);
Cart_Vector operator<<(const Cart_Vector&v1);
friend class Cart_Point;
};
Cart_Vector::Cart_Vector(double inputx, double inputy)
{
x = inputx;
y = inputy;
}
Cart_Vector operator*(const Cart_Vector&v1, double d)
{
double x,y;
x = v1.x*d;
y = v1.y*d;
return Cart_Vector(x,y);
//this function should make a new Cart_Vector
}
Cart_Vector operator/(const Cart_Vector&v1, double d)
{
double x,y;
if (d == 0)
{
x = v1.x;
y = v1.y;
}
x = v1.x/d;
y = v1.y/d;
return Cart_Vector(x,y);
//this function should make a new Cart_Vector and dividing by zero creates v1
}
Cart_Vector Cart_Vector::operator<<(const Cart_Vector&v1)
{
cout <<"v1: <" << v1.x << ", " << ">" << endl;
return v1;
//this function should just print v1
}
//TestCheckpoint1.cpp file below
int main()
{
//I haven't finished the main function to test all the functions yet
return 0;
}
split your code in different files but your implementation of operator << is wrong , consider following code , its just a hint to help
#include <iostream>
#include <math.h>
using namespace std;
class Cart_Vector
{
public:
double x; //x displacement of vector
double y; //y displacement of vector
Cart_Vector(double inputx, double inputy);
friend Cart_Vector operator*(const Cart_Vector&v1, double d);
friend Cart_Vector operator/(const Cart_Vector&v1, double d);
friend std::ostream& operator<<( std::ostream& out,const Cart_Vector&v1);
friend class Cart_Point;
};
Cart_Vector::Cart_Vector(double inputx, double inputy)
{
x = inputx;
y = inputy;
}
Cart_Vector operator*(const Cart_Vector&v1, double d)
{
double x,y;
x = v1.x*d;
y = v1.y*d;
return Cart_Vector(x,y);
//this function should make a new Cart_Vector
}
Cart_Vector operator/(const Cart_Vector&v1, double d)
{
double x,y;
if (d == 0)
{
x = v1.x;
y = v1.y;
}
x = v1.x/d;
y = v1.y/d;
return Cart_Vector(x,y);
//this function should make a new Cart_Vector and dividing by zero creates v1
}
std::ostream& operator<<(std::ostream &out, const Cart_Vector&v1)
{
out <<"v1: <" << v1.x << ", " << ">" << endl;
return out;
//this function should just print v1
}
class Cart_Point
{
public:
double x;
double y;
friend class Cart_Vector;
Cart_Point (double inputx, double inputy);
friend std::ostream& operator<<(std::ostream& out , const Cart_Point&p2);
friend Cart_Point operator+(const Cart_Point&p1,const Cart_Vector&v1);
friend Cart_Point operator-(const Cart_Point&p1,const Cart_Point&p2);
double Cart_distance(Cart_Point, Cart_Point);
};
Cart_Point::Cart_Point(double inputx, double inputy)
{
x = inputx;
y = inputy;
}
double Cart_Point::Cart_distance(Cart_Point p1, Cart_Point p2)
{
double distance = (sqrt( pow(p1.x - p2.x,2) + pow(p1.y - p2.y,2) ));
return distance;
//returns distance between p1 (point 1) and p2 (point 2)
}
std::ostream& operator<<(std::ostream &out, const Cart_Point&p1)
{
// Since operator<< is a friend of the Cart_Point class, we can access Point's members directly.
out << "p:(" << p1.x << ", " << p1.y << ")" << endl;
return out;
//this function should just print each point
}
Cart_Point operator+(const Cart_Point&p1, const Cart_Vector&v1)
{
double x,y;
x = p1.x + v1.x;
y = p1.y + v1.y;
return Cart_Point(x,y);
//this function should make a new Cart_Point
}
Cart_Point operator-(const Cart_Point&p1, const Cart_Point&p2)
{
double x,y;
x = p1.x- p2.x;
y = p1.y - p2.y;
return Cart_Point(x,y);
//this function should make a new Cart_Vector
}
//TestCheckpoint1.cpp file below
int main()
{
Cart_Point point1(2.0, 3.0);
std::cout << point1;
//I haven't finished the main function to test all the functions yet
return 0;
}
Wandbox :
Do yourself a favor and split your code in different files,at least one .h for Cart_Vector, one .h for Cart_Point, and one .cpp for the test script (main). You can correct this code to make it work (just swap the order of the two classes), if you correct other errors too, like in the overloading of the operator "-".
The point here is that if you start coding in this way, when you start programming complex projects you will find yourself in great difficulties. Coding one public (non-inner) class-per file is like applying Single Responsibility Principle for files too. I would say that also a file should have only one reason to change, and this has great benefits in readability and when will you perform version control.
Cart_Vector and Cart_point need each other. So you need to implement these classes in separate header files. Do not forget include them. Also in here ;
Cart_Point operator-(const Cart_Point&p1, const Cart_Point&p2)
{
double x,y;
x = p1.x- p2.x;
y = p1.y - p2.y;
//return Cart_Vector(x,y);
return Cart_Pointer(x,y);
//this function should make a new Cart_Vector
}
You can not access non-const elements of const object and you can not call non-const functions. You can pass these objects by value if you want it.
I am a beginner at C++ and I am trying to make a program that uses 2 points, adds them together to a line. Then adds a line together with a point, and gets a polygon.
I am trying, unsuccessfully, to overload the operator << so that I can print out my line:
#include <iostream>
using namespace std;
class Line {
private:
OnePoint onevalue;
OnePoint twovalue;
public:
Line(OnePoint a, OnePoint b) {
onevalue = a;
twovalue = b;
}
ostream& operator<<(ostream& print, Line& linje){ // Error right here.
print << linje.onevalue << ',' << linje.twovalue; // Too many
return print; // parameters for this type of function
}
};
class OnePoint {
private:
double xvalue;
double yvalue;
public:
OnePoint(double x = 0.0, double y = 0.0) {
xvalue = x;
yvalue = y;
}
friend ostream& operator<<(ostream& printh, OnePoint& cPoint) {
printh << "(" << cPoint.xvalue << ',' << cPoint.yvalue << ")";
return printh;
}
void Plus(OnePoint a) {
xvalue = xvalue + a.xvalue;
yvalue = yvalue + a.yvalue;
}
void Minus(OnePoint b) {
xvalue = xvalue + b.xvalue;
yvalue = yvalue + b.yvalue;
}
OnePoint Plustwo(OnePoint a) {
return (xvalue + a.xvalue, yvalue - a.yvalue);
}
void Change(double a, double b) {
xvalue += a;
yvalue += b;
}
void Print(OnePoint b) {
cout << xvalue << "," << yvalue << endl;
}
OnePoint operator+(OnePoint a) {
OnePoint temp;
temp.xvalue = xvalue + a.xvalue;
temp.yvalue = yvalue + a.yvalue;
return temp;
}
};
//--------------------------------------------------------------------
int main(){
OnePoint a(3.0, 3.0);
OnePoint b(1.0, 1.0);
OnePoint d(1.0, 4.0);
OnePoint c;
c = a + b + d;
c.Print(c);
}
Edit:
I can now cout my OnePoint, but I cannot cout Line.
As a member function, the operator gets an extra hidden parameter for the this pointer.
You can make it a free function by declaring it a friend of the class:
class Line {
// other members
public:
friend ostream& operator<<(ostream& print, Line& linje){
print << linje.onevalue << ',' << linje.twovalue;
return print;
}
};
I want to overload operator *, in this way:
#include <iostream>
using namespace std;
class A{
public:
double liczba;
A operator * (int a){
A b;
b.liczba = this->liczba * a;
return b;
}
};
int main(){
A a;
2*a;
return 0;
}
I get errors, I know that a*2 doesn't cause problems, but how to do 2*a?
You need to declare a friend operator:
class A{
public:
double liczba;
A operator * (int a){
A b;
b.liczba = this->liczba * a;
return b;
}
friend A operator*(int a, A & b);// friend operator
};
A operator*(int a, A & b)
{
return b * a;
}
Ideally, A & b would be const A & b, but you need to declare the operator you already defined to be const as well.
You should declare the operator as a non-member function. For example
A operator * ( const A &a, int x )
{
A b;
b.liczba = a.liczba * x;
return b;
}
A operator * ( int x, const A &a )
{
A b;
b.liczba = a.liczba * x;
return b;
}
Here is a demonstrative program
#include <iostream>
class A{
public:
double liczba;
};
A operator * ( const A &a, int x )
{
A b;
b.liczba = a.liczba * x;
return b;
}
A operator * ( int x, const A &a )
{
A b;
b.liczba = a.liczba * x;
return b;
}
int main()
{
A a = { 10 };
A b = 2 * a * 5;
std::cout << b.liczba << std::endl;
return 0;
}
The program output is
100
You can declare a non-member function:
A operator*( int i, const A & a ) {
A b;
b.liczba = a.liczba * i;
return b;
}
Here's a working example.
I can't seem to get this to work without the driver including the quadratic.cpp file. I have it set up as a project in Dev C++ but keep getting errors saying undefined reference to all of my functions called in the driver.
quadratic.h
#include <iostream>
using namespace std;
class Quadratic{
private:
float a,b,c;
public:
//Quadratic();
Quadratic();
Quadratic(float a,float b,float c);
float x;
float get_a();
float get_b();
float get_c();
float evaluate(float);
void set();
int numRoots(Quadratic &qTest);
float roots1(int numRoots, Quadratic &qRoots);
float roots2(int numRoots, Quadratic &qRoots);
};
Quadratic operator+(const Quadratic &q1,const Quadratic &q2);
Quadratic operator*(double r, const Quadratic& q);
quadratic.cpp
#include<iostream>
#include "quadratic.h"
#include <cmath> // std::sqrt(double)
using namespace std;
//Default Constructor
Quadratic::Quadratic():a(0),b(0),c(0){}
//Had to add this to get the overloaded + operator to work
//(mainly because of the way I had it return the new instance)
Quadratic::Quadratic(float aSum, float bSum, float cSum)
{
this->a = aSum;
this->b = bSum;
this->c = cSum;
}
float Quadratic::get_a()
{
return a;
}
float Quadratic::get_b()
{
return b;
}
float Quadratic::get_c()
{
return c;
}
float Quadratic::evaluate(const float x)
{
return ((a*(x*x))+(b*x)+c);
}
void Quadratic::set()
{
float aNew=0.0,bNew=0.0,cNew=0.0;
cout<<"Enter a new a: ";
cin>>aNew;
cout<<"Enter a new b: ";
cin>>bNew;
cout<<"Enter a new c: ";
cin>>cNew;
this->b = bNew;
this->c = cNew;
this->a = aNew;
}
int Quadratic::numRoots(Quadratic &qTest)
{
float a= qTest.get_a();
float b= qTest.get_b();
float c= qTest.get_c();
int numRoots=0;
if ((a==0)&&(b==0)&&(c==0))
{
//every value of x = real root, so infinity
numRoots=3;
}
else if((a==0)&&(b==0)&&(c!=0))
{
numRoots=0;
}
else if((a==0)&&(b!=0))
{
numRoots=1;
//root is x= -c/b
}
else if((a!=0)&&( (b*b)<(4*a*c) ))
{
numRoots=0;
}
else if((a!=0)&&( (b*b)==(4*a*c) ))
{
numRoots=1;
//root is x= -b/2a
}
else if((a!=0)&&( (b*b)>(4*a*c) ))
{
numRoots=2;
//root is x= +/- quadratic formula
}
return numRoots;
}
float Quadratic::roots1(int numRoots, Quadratic &qRoots)
{
float root1=0.0;
float rootPlus=0.0;
float rootMinus=0.0;
if (numRoots==1)
{
if((a==0)&&(b!=0))
{
root1 = (((0-c)/b));// + ((0-c)%b));
}
if((a!=0)&&( (b*b)==(4*a*c) ))
{
root1 = (((0-b)/(2*a)));// + ((0-b)%(2*a)));
}
}
else if (numRoots == 2)
{
rootMinus= (((0-b)-sqrt((b*b)-(4*a*c)))/(2*a));
rootPlus= (((0-b)+sqrt((b*b)-(4*a*c)))/(2*a));
if (rootMinus > rootPlus) root1=rootPlus;
else root1=rootMinus;
}
else if (numRoots == 3) root1=0;
return root1;
}
float Quadratic::roots2(int numRoots, Quadratic &qRoots)
{
float root2=0.0;
float rootPlus=0.0;
float rootMinus=0.0;
if (numRoots==1)
{
if((a==0)&&(b!=0))
{
root2 = (((0-c)/b));// + ((0-c)%b));
}
if((a!=0)&&( (b*b)==(4*a*c) ))
{
root2 = (((0-b)/(2*a)));// + ((0-b)%(2*a)));
}
}
else if (numRoots == 2)
{
rootMinus= (((0-b)-sqrt((b*b)-(4*a*c)))/(2*a));
rootPlus= (((0-b)+sqrt((b*b)-(4*a*c)))/(2*a));
if (rootMinus < rootPlus) root2=rootPlus;
else root2=rootMinus;
}
else if (numRoots == 3) root2=0;
return root2;
}
Quadratic operator+(Quadratic &q1,Quadratic &q2)
{
float tempa,tempb,tempc=0.0;
tempa= q1.get_a() + q2.get_a();
tempb= q1.get_b() + q2.get_b();
tempc= q1.get_c() + q2.get_c();
Quadratic temp(tempa, tempb,tempc);
return temp;
// return Quadratic( q1.get_a() + q2.get_a(),
// q1.get_b() + q2.get_b(),
// q1.get_c() + q2.get_c());
}
Quadratic operator*(double r, Quadratic& q)
{
return Quadratic( q.get_a() * r,
q.get_b() * r,
q.get_c() * r);
}
main.cpp
#include<iostream>
//#include "quadratic.cpp""
#include "quadratic.h"
using namespace std;
int main()
{
//Default constructors called, all 3 coefficients set to 0
Quadratic q1;
Quadratic q2;
cout<<"Default constructor called\n"
<<"q1 a = "<<q1.get_a()<<"\n"
<<"q1 b = "<<q1.get_b()<<"\n"
<<"q1 c = "<<q1.get_c()<<"\n"
<<"q2 a = "<<q2.get_a()<<"\n"
<<"q2 b = "<<q2.get_b()<<"\n"
<<"q2 c = "<<q2.get_c()<<"\n";
//Call the set function to set all 3 coefficents of existing
//quadratic expression to new values
q1.set();
q2.set();
cout<<"\nSet function called. New values:\n"
<<"q1 a = "<<q1.get_a()<<"\n"
<<"q1 b = "<<q1.get_b()<<"\n"
<<"q1 c = "<<q1.get_c()<<"\n"
<<"q2 a = "<<q2.get_a()<<"\n"
<<"q2 b = "<<q2.get_b()<<"\n"
<<"q2 c = "<<q2.get_c()<<"\n";
//calls function to evaluate the quadratic expression for a given calue of x
cout<<"\nEnter x: ";
float x=0.0;
cin>>x;
cout<<"for q1, ax^2 + bx +c = "<<q1.evaluate(x)
<<"\nfor q2, ax^2 + bx +c = "<<q2.evaluate(x);
//add q1+q2 with overloaded + operator
Quadratic quad_sum = q1 + q2;
cout<<"\n\nAdding q1+_q2\n"
<<"\nq1_a + q2_a= "<<quad_sum.get_a()
<<"\nq1_b + q2_b= "<<quad_sum.get_b()<<" "
<<"\nq1_c + q2_c= "<<quad_sum.get_c();
//multiply the coefficients by r using overloaded * operator
cout<<"\n\nEnter a number to multiply the coefficeints by: ";
double r=0.0;
cin>>r;
Quadratic quad_prod=r*quad_sum;
cout<<"\nMultiplying q1 by r results in "
<<"\nnew a = "<<quad_prod.get_a()
<<"\nnew b = "<<quad_prod.get_b()
<<"\nnew c = "<<quad_prod.get_c();
//calculate number of and display roots
int numroots=q1.numRoots(q1);
cout<<"\n\nNumber of Roots for q1 = "<<numroots;
if (numroots != 0) cout<<"\nThey are: "<<q1.roots1(numroots, q1)<<" and "<<q1.roots2(numroots, q1);
}
Added
#ifndef QUAD_H_
#define QUAD_H_
and
#endif
to the header file, but still get "undefined reference" for every function call in the driver program.