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