Adding Constructors using operators overloaded

SO i want to add the default constructor and the generic constructor or at least keep changing the values as i either add or subtract the values.

it prints the default, generic and then the negative values 3 times when it should print only once. can you please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <iostream>
#include <cmath>
using namespace std;

class Quadratic
{
private:
    double *cf, a , b, c;
public:
    Quadratic();
    Quadratic(int, int, int);
	Quadratic operator !(); 
	Quadratic operator +(Quadratic &); 
	Quadratic operator -(Quadratic &); 
	Quadratic operator +=(Quadratic &); 
	Quadratic operator ~(); 
	int geta(); 
	int getb();
	int getc();
    void print();
};
Quadratic::Quadratic()	//Default values
{
    a = 0;
    b = 0;
    c = 0;
	cf = new double[3];
    cf[0] = 0;
    cf[1] = 10;
    cf[2] = 20;
}
Quadratic::Quadratic(int a1, int b1, int c1)	//initializing array with new values
{
	a = a1;
    b = b1;
    c = c1;
    cf = new double[3];
    cf[0] = a;
    cf[1] = b;
    cf[2] = c;
}
Quadratic Quadratic::operator !() //changes values to negatives
{
	cf[0] *= -1;
	cf[1] *= -1;
	cf[2] *= -1;
	return *this;
}
Quadratic Quadratic::operator+(Quadratic &p)
//i want to add the currect values of the array with the new values
{
    cf[0] += p.geta();
    cf[1] += p.getb();
    cf[2] += p.getc();
	return *this;
}
Quadratic Quadratic::operator- (Quadratic &p)//substract the added values previously
{
    cf[0] -= p.geta();
    cf[1] -= p.getb();
    cf[2] -= p.getc();
	return *this;
}
Quadratic Quadratic::operator+= (Quadratic &p)//not sure how to use this operator
{
    cf[0] += p.geta();
    cf[1] += p.getb();
    cf[2] += p.getc();
	return *this;
}
Quadratic Quadratic::operator~ ()//calles the destructor
{
    delete [] cf;
    cout << "Destructor called.\n";
}
// since a b and c are private i use geta,b,c
int Quadratic::geta()
{
	return a;
}
int Quadratic::getb()
{
	return b;
}
int Quadratic::getc()
{
	return c;
}
void Quadratic::print()
{
    cout << "y = " << cf[0] << "x^2 " << cf[1] << "x " << cf[2]<< endl;;
}

//heres my main. I think the problem might be here
int main ()
{
    Quadratic Q1;
    Quadratic Q2(11, 22, 33);
    
	Q1.print();
	Q2.print();
	
	!Q2;
	Q2.print();

	Q2 + Q1;
	Q2.print();

	Q1 - Q2;
	Q2.print();

	Q2 += Q1;
	Q2.print();
	
	~Q2;
    
    return  0;
}
Last edited on
you didnt initialize a, b, c in your default constructor, so when Q1 was constructed a, b, c was given a garbage value
I tried both ways but it still won't add anything tho. I tried initializing a,b,c =0 or 10 still didn't work
You need to change line 9 to:
double cf[3], a, b, c;
to avoid memory management problems. Come to think of it, why do you have a cf array AND values a, b, and c? You should have one or the other.

You have declared the destructor wrong. Line 17 declares a ~ operator. Line 17 should be :
~Quatratic();, but really, if you make the change above, then there's no need to define a destructor at all because there will be no need to free up memory.

Look at lines 107 and 108:
1
2
	Q2 + Q1;
	Q2.print();

If you needed to add integers, you wouldn't write:
1
2
3
int i1=1, i2=2;
i1+i2;
cout << i1;

because i1+i2 doesn't change i1, it just evaluates to a new integer. In particular, you don't want cout << i1+i2 to modify i1 as a side effect, and you really don't want cout << i1+i2+i3 to make changes to the variables along the way.

Your operators should work the same way. Q1+Q2 shouldn't change either Q1 or Q2. So your main program should be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main ()
{
    Quadratic Q1;
    Quadratic Q2(11, 22, 33);
    Quadratic Q3;
    
	Q1.print();
	Q2.print();
	
	Q3 = !Q2;
	Q3.print();

	Q3 = Q2 + Q1;
	Q3.print();

	Q3 = Q1 - Q2;
	Q3.print();

	Q2 += Q1;
	Q2.print();
    return  0;
}

Note that this applies to everything except the += operator. That one really DOES change the argument.
¿why do you have both `cf' and `a, b, c'?

> (+=) not sure how to use this operator
look at how that same operator is used on other types, by instance, int and std::string.
a += b; should be equivalent to a = a+b;, but the operator `+' should not modify its arguments.

> cf = new double[3];
unnecessary dynamic memory allocation, use an array.

> operator~ ()//calles the destructor
that isn't the destructor.
Topic archived. No new replies allowed.