need help regarding assignment operator overloading

trignometery.h
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
#ifndef TRIGNO_H
#define TRIGNO_H

class trig
	{
   protected:
   const float PI;
   float* angle;
   public:
   trig() : PI(3.14159265)
   	{
      angle = new float;
  	 	*angle = 0.0F;
      }
   trig(float a) : PI(3.14159265)
   	{
      angle = new float;
      *angle = a;
      }
   trig(const trig& t) : PI(3.14159265)
   	{
      angle = t.angle;
      }
   trig& operator=(const trig& t)
   	{
      delete angle;
      angle = t.angle;
      return *this;
      }
   void get_trig();
   void display_trig() const;
   };
#endif 

trignometery.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include "trignometry.h"

void trig :: get_trig()
	{
   cout<<"Enter Angle Value : ";
   cin>>*angle;
   }

void trig :: display_trig() const
	{
   cout<<*angle<<endl;
   }

number.h
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
#ifndef NUMBER_H
#define NUMBER_H

class number:
	{
   protected:
   	int *num;
   public:
   	void vertfunc()
      	{ }
   	number()
      	{
      	num = new int;
      	*num = 0;
      	}
      number(int n)
      	{
         num = new int;
         *num = n;
         }
   	void get();
      number(const number& n)
      	{
         num = n.num;
         }
      number& operator=(const number& n)
      	{
         delete num;
         num = n.num;
         return *this;
         }
   	void print() const;
      int* getnum()
      	{return num;}
      ~number()	{
      delete num;
      }
   };

#endif 

number.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include "number.h"
void number :: get()
	{
	num = new int;
	cout<<"Please, Enter the number : ";
	cin>>*num;
	}

void number :: print() const
	{
   cout<<*num<<endl;
   }

calculator.h
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
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include "number.h"
#include "trignometry.h"

class calculator : public number,public trig
	{
   public:
      calculator() : number(),trig()	{}
      calculator(float a) : trig(a)	{}
      calculator(int n,float a) : trig(n),number(n) {}

      calculator(const calculator& c1) : number(c1),trig(c1)
      	{}
      /*also tried this way
       using number :: operator=;
       using number :: operator=;
      */
      calculator& operator=(const calculator& c1)
      	{
         number :: operator=(c1);
         trig :: operator=(c1);
         return *this;
         }


      calculator operator+(const calculator&);
      calculator operator-(const calculator&);
      calculator operator*(const calculator&);
      calculator operator/(const calculator&);
      void sinTheta() const;
      void cosTheta() const;
      void tanTheta() const;
      void sinInverse() const;
      void cosInverse() const;
      void tanInverse() const;
      void vertfunc(){}
      int factorial();
      float square() const;
      float cube() const;
      float sqroot();
      float power(float* x, float* y);
      float logarithm();
		float natural_log();

      ~calculator()	{
      number :: ~number();
      }
   };
#endif 

calculator.cpp
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
#include <iostream>
#include <math.h>
#include "calculator.h"


calculator calculator :: operator+(const calculator& c1)
	{
   return calculator((*num) + (*c1.num));
   }
calculator calculator :: operator-(const calculator& c1)
	{
   return calculator((*num) - (*c1.num));
   }
calculator calculator :: operator*(const calculator& c1)
	{
   return calculator((*num) *  (*c1.num) );
   }
calculator calculator :: operator/(const calculator& c1)
	{
   return calculator((*num) /  (*c1.num));
   }
void calculator :: sinTheta() const
	{
	float deg = *angle * 180.0F/PI;
	float ans = sin(deg);
   cout<<"sin("<<deg<<") : "<<ans<<endl;
   }
void calculator :: cosTheta() const
	{
	float deg = *angle * 180.0F/PI;
	float ans = cos(deg);
   cout<<"cos("<<deg<<") : "<<ans<<endl;
   }
void calculator :: tanTheta() const
	{
	float deg = *angle * 180.0/PI;
	float ans = tan(deg);
   cout<<"tan("<<deg<<") : "<<ans<<endl;
   }
void calculator :: sinInverse() const
	{
   float ans = asin(*angle) * 180.0 * PI;
   cout<<"sin Inverse("<<*angle<<") : "<<ans<<endl;
   }
void calculator :: cosInverse() const
	{
   float ans = acos(*angle) * 180.0 * PI;
   cout<<"sin Inverse("<<*angle<<") : "<<ans<<endl;
   }
void calculator :: tanInverse() const
	{
   float ans = atan(*angle) * 180.0 * PI;
   cout<<"sin Inverse("<<*angle<<") : "<<ans<<endl;
   }
int calculator :: factorial()
	{
   for(int i=*num-1; i>0; i--)
   	*num *= i;
   return *num;
   }
float calculator :: square() const
	{
   return (*angle) * (*angle);
   }
float calculator :: cube() const
	{
   return (*angle) * (*angle) * (*angle);
   }
float calculator :: sqroot()
	{
   float sq = sqrt(*angle);
   return sq;
   }

main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <conio>
#include "number.h"
#include "trignometry.h"
#include "calculator.h"

int main()
	{
   calculator c1,c2,c3;
   c1.get();
   c2.get();
   c3 = c1 + c2; 
   c3.print();
   getch();
   }


this is just a simple practice program. everything is going well accept the assignment operator and copy constructor, in the calculator class, which is multiply inherited. I am totally stuck and have been trying to sort this out for last 6 hours. Please help me.

please, enter value : 10
please, enter value : 20
19234458
Don't call ~number() explicitly. It will be called automatically after ~calculator() so as it is now it will be called twice.

Your copy assignment operators and copy constructors are incorrect. The problem is that they make two objects have pointers to the same value.
1
2
3
4
5
number(const number& n)
{
	num = n.num;
	// num and n.num will now point to the same object
}

This is a problem if one of the objects try to manipulate num it will be changed for the other object as well because they both share it. Even worse is when one of the objects delete num (in the constructor or elsewhere). This will leave the other object with a dangling pointer and when that object try to use or delete num you will have undefined behaviour.
Last edited on

This is a problem if one of the objects try to manipulate num it will be changed for the other object as well because they both share it.

then what should I do, I can't change the value of n.num as it is const. I cannot delete it. what's left?
I did this,

1
2
3
4
5
number(const number& n)
{
	num =  new int(*n.num);
	
}


but it still won't help, the output is zero now.
Why are you using a pointer in the first place?
I corrected the code. Thought it would be a good experience to go through someone else's code. You had quite a few problems with shallow pointers. Also, what cire said. Why are you using pointers and complicated classes instead of just storing a float and an int in calculator itself?
Code:
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <iostream>
#include <math.h>
using namespace std;
#ifndef TRIGNO_H
#define TRIGNO_H

class trig
	{
   protected:
   const float PI;
   float* angle;
   public:
   trig() : PI(3.14159265)
   	{
      angle = new float;
  	 	*angle = 0.0F;
      }
   trig(float a) : PI(3.14159265)
   	{
      angle = new float;
      *angle = a;
      }
   trig(const trig& t) : PI(3.14159265)
   	{
      angle = new float(*t.angle);
      }
   trig& operator=(const trig& t)
   	{
      *angle = *t.angle;
      return *this;
      }
   void get_trig();
   void display_trig() const;
   };
#endif 

void trig :: get_trig()
	{
   cout<<"Enter Angle Value : ";
   cin>>*angle;
   }

void trig :: display_trig() const
	{
   cout<<*angle<<endl;
   }

#ifndef NUMBER_H
#define NUMBER_H

class number
	{
   protected:
   	int *num;
   public:
   	void vertfunc()
      	{ }
   	number()
      	{
      	num = new int;
      	*num = 0;
      	}
      number(int n)
      	{
         num = new int;
         *num = n;
         }
   	void get();
      number(const number& n)
      	{
		num = new int(*n.num);
         }
      number& operator=(const number& n)
      	{
         *num = *n.num;
         return *this;
         }
   	void print() const;
      int* getnum()
      	{return num;}
      ~number()	{
      delete num;
      }
   };

#endif 

void number :: get()
	{
	cout<<"Please, Enter the number : ";
	cin>>*num;
	}

void number :: print() const
	{
   cout<<*num<<endl;
   }

#ifndef CALCULATOR_H
#define CALCULATOR_H


class calculator : public number,public trig
	{
   public:
      calculator() : number(),trig()	{}
      calculator(float a) : number(),trig(a)	{}
      calculator(int n,float a) : trig(a),number(n) {}

      calculator(const calculator& c1) : number(*c1.num),trig(*c1.angle)
      	{}
 
      calculator& operator=(const calculator& c1)
      	{
         *num = *c1.num;
         *angle = *c1.angle;
         return *this;
         }


      calculator operator+(const calculator&);
      calculator operator-(const calculator&);
      calculator operator*(const calculator&);
      calculator operator/(const calculator&);
      void sinTheta() const;
      void cosTheta() const;
      void tanTheta() const;
      void sinInverse() const;
      void cosInverse() const;
      void tanInverse() const;
      void vertfunc(){}
      int factorial();
      float square() const;
      float cube() const;
      float sqroot();
      float power(float* x, float* y);
      float logarithm();
		float natural_log();

      ~calculator()	{
      }
   };
#endif 

calculator calculator :: operator+(const calculator& c1)
	{
   return calculator((*num) + (*c1.num),*angle);
   }
calculator calculator :: operator-(const calculator& c1)
	{
   return calculator(*angle,(*num) - (*c1.num));
   }
calculator calculator :: operator*(const calculator& c1)
	{
   return calculator(*angle,(*num) *  (*c1.num) );
   }
calculator calculator :: operator/(const calculator& c1)
	{
   return calculator(*angle,(*num) /  (*c1.num));
   }
void calculator :: sinTheta() const
	{
	float deg = *angle * 180.0F/PI;
	float ans = sin(deg);
   cout<<"sin("<<deg<<") : "<<ans<<endl;
   }
void calculator :: cosTheta() const
	{
	float deg = *angle * 180.0F/PI;
	float ans = cos(deg);
   cout<<"cos("<<deg<<") : "<<ans<<endl;
   }
void calculator :: tanTheta() const
	{
	float deg = *angle * 180.0/PI;
	float ans = tan(deg);
   cout<<"tan("<<deg<<") : "<<ans<<endl;
   }
void calculator :: sinInverse() const
	{
   float ans = asin(*angle) * 180.0 * PI;
   cout<<"sin Inverse("<<*angle<<") : "<<ans<<endl;
   }
void calculator :: cosInverse() const
	{
   float ans = acos(*angle) * 180.0 * PI;
   cout<<"sin Inverse("<<*angle<<") : "<<ans<<endl;
   }
void calculator :: tanInverse() const
	{
   float ans = atan(*angle) * 180.0 * PI;
   cout<<"sin Inverse("<<*angle<<") : "<<ans<<endl;
   }
int calculator :: factorial()
	{
   for(int i=*num-1; i>0; i--)
   	*num *= i;
   return *num;
   }
float calculator :: square() const
	{
   return (*angle) * (*angle);
   }
float calculator :: cube() const
	{
   return (*angle) * (*angle) * (*angle);
   }
float calculator :: sqroot()
	{
   float sq = sqrt(*angle);
   return sq;
   }

int main()
	{
   calculator c1,c2,c3;
   c1.get();
   c2.get();
   c3 = c1 + c2; 
   c3.print();
   std::getchar();
   std::getchar();
   }

Oh yeah, I was lazy, so it's all one file.
Last edited on

Why are you using pointers and complicated classes instead of just storing a float and an int in calculator itself?


thankyou very much... I am using pointers and inheritance for the sake of practice. I know it can be done without pointers. Again thankyou, you helped me alot in clearing this concept although i still need to study it indepth.
one thing I would ask
1
2
3
4
5
6
 calculator& operator=(const calculator& c1)
      	{
         *num = *c1.num;
         *angle = *c1.angle;
         return *this;
         }


you use this in assignment operator. but in the books they show the other way to do it and that works well in simple classes without inheritance. Is it better? should I always stick to it?

and please tell me of tutorials on pointer concepts, or any book please.
Last edited on
Haha, I guess I'd go with what your book shows you! Both ways work, and I'd be lying if I told you I knew one was better.
Have you looked at the tutorial on this site? You can find it under "documentation".
Topic archived. No new replies allowed.