Designing a C++ class with overloading operators - help!

Hi all,

I have a C++ exam on monday and I would appreciate some help. I'm currently going through some past papers and I need help on this question:

Design a C++ class to represent an object that consists of two members, a and b. Provide methods to overload the assignment (=), addition +, subtraction - , and division / operators, as given below. Provide a constructor that can initialise the object with specified values for a and b.

Operations on the object consisting of the members a and b with another object with members c and d.

Addition: (a,b) + (c,d) = (a+c, b+d)
Subtraction (a,b) - (c,d) = (a-c, b-d)
Multiplication: (a,b)(c,d) = (ac-bd,bc+ad)
Division: (a,b)/(c,d) = ((ac+bd)/(c^2 + d^2), (bc-ad)/ (c^2 + d^2))

(You may recognise this as the behaviour of complex numbers.)


I am absolutely hopeless with C++ :(. The only help I can find relevant appears to be the tutorial on classes (quarter way down) - http://www.cplusplus.com/doc/tutorial/classes2/.

Based on that I have made a hopeless attempt below:

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
//vectors: overloading operators
#include <iostream>
using namespace std;
 
class CVector {
public:
int x,y;
CVector () {};
CVector (int,int);
CVector operator + (CVector);
CVector operator - (CVector);
CVector operator * (CVector);
CVector operator / (CVector);
};
 
CVector::CVector (int a, int b){
x = a;
y = b;
}
 
CVector CVector::operator+ (CVector param) {
CVector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}

CVector CVector::operator- (CVector param) {
CVector temp;
temp.x = x - param.x;
temp.y = y - param.y;
return (temp);
}

CVector CVector::operator* (CVector param) {
CVector temp;
temp.x = (x*param.x) + (y*param.y);
temp.y = (x*param.y) + (y*param.x);
return (temp);
}
 
CVector CVector::operator/ (CVector param) {
CVector temp;
temp.x = (x*param.x) + (y*param.y)/((param.x)*(param.x) + (param.y)*(param.y));
temp.y = (y*param.x) + (x*param.y)/((param.x)*(param.x) + (param.y)*(param.y));
return (temp);
}


I have no idea if this is right and I have no idea how to complete it. I'm unsure in the example I listed to include a b c d instead of actual numbers in the main function and if that would actually work.

Appreciate any help on how to start/complete this :)

Last edited on
A few notes:

- Don't bother with defining default constructor unless you plan on setting the X and y member variables to 0 on initialization (which you may want to do).

- When you do define a default constructor like in this case the compiler will no longer fill in the copy constructor or the destructor for you, this is commonly referred to as the "Rule of Three". The destructor isn't so important in your case since there aren't any dynamically allocated variables to clean up, but the copy constructor should be made.

- The braces around 'temp' when you return it are not necessary.

- You should avoid 'using' namespaces inside of header files, this is more about good practice then functionality. Also you don't use anything from the std namespace in this header file anyway.

EDIT: Delete that semicolon at the end of Line 8.
Last edited on
When you do define a default constructor like in this case the compiler will no longer fill in the copy constructor or the destructor for you, this is commonly referred to as the "Rule of Three".

No if you define any constructor, the default constructor will not be generated, however unless you specifically define the copy constructor and the destructor they will still be generated.

The rule of three deals with the 1. copy constructor, 2. the assignment operator and 3. the destructor.
Topic archived. No new replies allowed.