Was given this assignment in class, but professor is not really good at explaini
ID: 3707192 • Letter: W
Question
Was given this assignment in class, but professor is not really good at explaining, and no one else in class seems to understand it, Help anyone! This is a C++ Program.
Overload the following operators for point objects: +, ==, ! =, <<, >>. Write a suitable driver program to test your overloaded operators.
//Overload + as a member function of the point class:
point operator +(const point& p);
// Postcondition: The sum of p1 and p2 is returned.
//Overload ==, ! = and << (stream insertion-output)operators as non-member functions:
// bool operator ==(const point& p1, const point& p2);
// Postcondition: The return value is true if p1 and p2 are identical.//
// bool operator !=(const point& p1, const point& p2)
// Postcondition: The return value is true if p1 and p2 are not identical.//
// ostream& operator <<(ostream& outs, const point& source)
// Postcondition: The x and y coordinates of source have been
// written to outs. The return value is the ostream outs.
//Overload >> (stream extraction -input) as a friend function of the point class
// friend istream& operator >>(istream& ins, point& target);
// Postcondition: The x and y coordinates of target have been
// read from ins. The return value is the istream ins.
Explanation / Answer
In this example, we shall overload the '+' operator in the Point class to support addition of two Point objects. In other words, we can write p3 = p1+p2, where p1, p2 and p3 are Point objects, similar to the usual arithmetic operation. We shall construct a new Point instance p3 for the sum, without changing the p1 and p2 instances.
Point.h
Program Notes:
Point.cpp
Program Notes:
TestPoint.cpp
Overloading << and >> Operators of Point class using non-member friend Functions
In this example, we shall overload << and >> operators to support stream insertion and extraction of Point objects, i.e., cout << aPoint, and cin >> aPoint. Since the left operand is not a Point object (cout is an ostream object and cin is an istream object), we cannot use member function, but need to use non-member function for operator overloading. We shall make these functions friends of the Point class, to allow them to access the private data members directly for enhanced performance.
Point.h
Program Notes:
Point.cpp
Program Notes:
TestPoint.cpp
The overloaded >> and << can also be used for file input/output, as the file IO stream ifstream/ofstream (in fstream header) is a subclass of istream/ostream. For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* The Point class Header file (Point.h) */ #ifndef POINT_H #define POINT_H class Point { private: int x, y; // Private data members public: Point(int x = 0, int y = 0); // Constructor int getX() const; // Getters int getY() const; void setX(int x); // Setters void setY(int y); void print() const; const Point operator+(const Point & rhs) const; // Overload '+' operator as member function of the class }; #endif Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.