Overloading + , - and * operators (long)

Question we got was : Define a class Pairs with two integer data members, f and s, where f represents
the first value in the ordered pair and s represents the second value in an ordered
pair. Objects of type Pairs can be used in any situation where ordered pairs are
needed. This class has:
The class should contain a default constructor that initializes both data members to
0, and two overloaded constructors, one with one int parameter and the other with
two int parameters. The one-parameter constructor should initialise the first
member of the pair; the second member of the pair is to be 0. The two-parameter
constructor should initialise both members of the ordered pair.
The class should also contain a destructor that outputs the message "Goodbye!"
Include accessor functions that return the values stored in each of the member
variables of an object of class Pairs (i.e. get functions), as well as mutator
functions to update each of the member variables of an object of class Pairs
respectively (i.e. set functions with a parameter to set each member variable to a
value specified by the parameter). The class should also contain a void member
function called reset() that resets the member variables of a Pairs to values
specified by parameters.
Overload the stream extraction operator >> and the stream insertion operator << as
friend functions so that objects of class Pairs are to be input and output in the
form (5,6) (5,-4) (-5,4) or (-5,-6).
Overload binary operator + as a friend function to add pairs according to the rule
(a,b) + (c,d) = (a + c, b + d)
Overload operator – as a friend function in the same way, i.e. according to the rule
(a,b) - (c,d) = (a - c, b - d)
Overload operator * as a friend function on Pairs and int according to the rule
(a,b) * c = (a * c, b * c)

my 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
#include <iostream>

using namespace std;

class Pairs
{
    private:
        int f;
        int s;

    public:
        Pairs()
        {
            f = 0;
            s = 0;
        }
        Pairs(int first)
        {
            f = first;
            s = 0;
        }
        Pairs(int first, int second)
        {
            f = first;
            s = second;
        }
        ~Pairs()
        {
            cout << "Goodbye!" << endl;
        }
        int getFirst() const
        {
            return f;
        }
        int getSecond() const
        {
            return s;
        }
        void setFirst(int first)
        {
            f = first;
        }
        void setSecond(int second)
        {
            s = second;
        }
        void reset(int first, int second)
        {
            setFirst(first);
            setSecond(second);
        }
        friend std::istream& operator >> (std::istream &in, Pairs&);
        friend std::ostream& operator << (std::ostream& out, const Pairs&);
        friend Pairs operator + (Pairs const &p1, Pairs const &p2);
        friend Pairs operator - (Pairs const &p1, Pairs const &p2);
        friend Pairs operator * (Pairs const &p1, int c);
};

std::istream& operator >> (std::istream &in, Pairs &p)

{
cout << "Enter first number: ";
    int f;
    in >> f;
    p.setFirst(f);
cout << "Enter second number: ";
    int s;
    in >> s;
    p.setSecond(s);
    return in;
}

std::ostream& operator<<(std::ostream& out, const Pairs &p)

{
        out << "(" << p.f << "," << p.s << ")";
        return out;
}
Pairs operator + (Pairs const &p1, Pairs const &p2)
{
    Pairs p;
    p.setFirst(p1.getFirst() + p2.getFirst());
    p.setSecond(p1.getSecond() + p2.getSecond());
    return p;
}

Pairs operator - (Pairs const &p1, Pairs const &p2)
{
    Pairs p;
    p.setFirst(p1.getFirst() - p2.getFirst());
    p.setSecond(p1.getSecond() - p2.getSecond());
    return p;
}

Pairs operator * (Pairs const &p1, int c)
{
    Pairs p;
    p.setFirst(p1.getFirst() * c);
    p.setSecond(p1.getSecond() * c);
    return p;
}
int main()
{
    Pairs p1 = Pairs();
cout << "P1: " << p1 << endl;
    Pairs p2 = Pairs(5);
cout << "P2: " << p2 << endl;
    Pairs p3 = Pairs(10, 20);
cout << "P3: " << p3 << endl;
cout << "Enter values for pair 1: " << endl;
cin >> p1;
cout <<  "P1: " << p1 << endl;
cout << "Reset P2 " << endl;
    p2.reset(5, 10);
cout << "P2: " << p2 << endl;
cout << p1 << " + " << p2 << " = " << (p1 + p2) << endl;
cout << p2 << " - " << p3 << " = " << (p2 - p3) << endl;
cout << p3 << " * 10  = " << (p3 * 10) << endl;
return 0;
}



now the next question is what i need help with :
Overload the +, - and * operators for objects of class Pairs, as
member functions. Also define the class Pairs as an ADT that uses separate files
for the interface and the implementation. Use separate compilation and the same
program as above to test these member functions
Overload binary operator + as a friend function to add Pairs
Overload the operators + for objects of class Pairs, as member functions.

I can see the educational track, but there is a canonical way to write operator+ that is neither member nor a friend:
1
2
3
4
5
Pairs operator+ ( Pairs lhs, Pairs const &rsh )
{
  lhs += rhs;
  return lhs;
}

That obviously requires operator+=, which is usually a member of the class. (Furthermroe, this version requires that Pairs is copy-constructible.)


Operators are syntactic sugar. They make code look "simpler".
For example, p1 + p2 is same as writing operator+( p1, p2 )
Likewise, if the class of p1 has member op+, then p1 + p2 is shorthand of p1.operator+( p2 )
Can you guess the declaration of the member function from that?


Separate files, please read: https://www.cplusplus.com/articles/Gw6AC542/
Last edited on
The operator = * - don't need to be friend functions. They can be just orinary functions:

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
#include <iostream>

class Pairs {
private:
	int f {};
	int s {};

public:
	Pairs() {}
	Pairs(int first) : f(first) {}
	Pairs(int first, int second) : f(first), s(second) {}
	~Pairs() { std::cout << "Goodbye!\n"; }
	int getFirst() const { return f; }
	int getSecond() const { return s; }
	void setFirst(int first) { f = first; }
	void setSecond(int second) { s = second; }
	void reset(int first, int second) { setFirst(first); setSecond(second); }

	friend std::istream& operator >> (std::istream& in, Pairs&);
	friend std::ostream& operator << (std::ostream& out, const Pairs&);
};

std::istream& operator >> (std::istream& in, Pairs& p)
{
	// Format is (a,b)
	// Without error checking
	char tmp {};

	return in >> tmp >> p.f >> tmp >> p.s >> tmp;
}

std::ostream& operator<<(std::ostream& out, const Pairs& p)
{
	return out << '(' << p.f << ", " << p.s << ')';
}

Pairs operator+(Pairs const& p1, Pairs const& p2)
{
	return {p1.getFirst() + p2.getFirst(), p1.getSecond() + p2.getSecond()};
}

Pairs operator-(Pairs const& p1, Pairs const& p2)
{
	return {p1.getFirst() - p2.getFirst(), p1.getSecond() - p2.getSecond()};
}

Pairs operator*(Pairs const& p1, int c)
{
	return {p1.getFirst() * c, p1.getSecond() * c};
}

int main()
{
	Pairs p1;
	std::cout << "P1: " << p1 << '\n';

	Pairs p2 {5};
	std::cout << "P2: " << p2 << '\n';

	Pairs p3 {10, 20};
	std::cout << "P3: " << p3 << '\n';

	std::cout << "Enter values for pair 1 [format is (a,b)]: ";
	std::cin >> p1;

	std::cout << "P1: " << p1 << '\n';

	std::cout << "Reset P2\n";
	p2.reset(5, 10);
	std::cout << "P2: " << p2 << '\n';

	std::cout << p1 << " + " << p2 << " = " << (p1 + p2) << '\n';
	std::cout << p2 << " - " << p3 << " = " << (p2 - p3) << '\n';
	std::cout << p3 << " * 10  = " << (p3 * 10) << '\n';
}


To have them as member functions:

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
#include <iostream>

class Pairs {
private:
	int f {};
	int s {};

public:
	Pairs() {}
	Pairs(int first) : f(first) {}
	Pairs(int first, int second) : f(first), s(second) {}
	~Pairs() { std::cout << "Goodbye!\n"; }
	int getFirst() const { return f; }
	int getSecond() const { return s; }
	void setFirst(int first) { f = first; }
	void setSecond(int second) { s = second; }
	void reset(int first, int second) { setFirst(first); setSecond(second); }

	Pairs operator+(const Pairs& rhs) { return {f + rhs.f, s + rhs.s}; }
	Pairs operator-(const Pairs& rhs) { return {f - rhs.f, s - rhs.s}; }
	Pairs operator*(int c) { return {f * c, s * c}; }

	friend std::istream& operator >> (std::istream& in, Pairs&);
	friend std::ostream& operator << (std::ostream& out, const Pairs&);
};

std::istream& operator >> (std::istream& in, Pairs& p)
{
	// Format is (a,b)
	// Without error checking
	char tmp {};

	return in >> tmp >> p.f >> tmp >> p.s >> tmp;
}

std::ostream& operator<<(std::ostream& out, const Pairs& p)
{
	return out << '(' << p.f << ", " << p.s << ')';
}

int main()
{
	Pairs p1;
	std::cout << "P1: " << p1 << '\n';

	Pairs p2 {5};
	std::cout << "P2: " << p2 << '\n';

	Pairs p3 {10, 20};
	std::cout << "P3: " << p3 << '\n';

	std::cout << "Enter values for pair 1 [format is (a,b)]: ";
	std::cin >> p1;

	std::cout << "P1: " << p1 << '\n';

	std::cout << "Reset P2\n";
	p2.reset(5, 10);
	std::cout << "P2: " << p2 << '\n';

	std::cout << p1 << " + " << p2 << " = " << (p1 + p2) << '\n';
	std::cout << p2 << " - " << p3 << " = " << (p2 - p3) << '\n';
	std::cout << p3 << " * 10  = " << (p3 * 10) << '\n';
}

Topic archived. No new replies allowed.