C++ please write down each following question Complete the following paper/penci
ID: 3685760 • Letter: C
Question
C++ please write down each following question
Complete the following paper/pencil exercises. First, do these individually. Then you and your partner should share your answers and make any needed corrections, making sure you both understand the solutions. Then you both should discuss your answers with one of your TA. 1) Consider the following class declaration: class Point { public: Point(); Point(int xval, int yval); friend Point operator +(Point, Point); private: int xloc; int yloc;}; a. Write the function definition for the overloaded + operator that will produce the sum of the x values and the sum of the y values of two Point objects. b. Rewrite the Point class interface to make the overloaded + operator a public member function of the class. c. Provide the function definition for the overloaded + member operator of the Point class. d. Describe what the overloaded output operator (e.g., some_object >> some_other_object; e. In order to overload the output operator for a class we make it a non-member friend function of the class and not a member function. Explain why. f. Overload the output operator for the point class to output a point object as follows: x, y where x and y are the coordinate values for the point, separated by a comma.Explanation / Answer
a)
Point operator+(Point p1,Point p2)
{
Point p3;
p3.xloc=p1.xloc+p2.xloc;
p3.yloc=p1.yloc+p2.yloc;
return p3;
}
--------------------------------------------------------------------------------------------
b)
class Point
{
public:
Point();
Point(int xval, int yval);
Point operator+(Point,Point);
private:
int xloc;
int yloc;
};
--------------------------------------------------------------------------------------------------
c)
Point Point::operator+(Point p1,Point p2)
{
Point p3;
p3.xloc=p1.xloc+p2.xloc;
p3.yloc=p1.yloc+p2.yloc;
return p3;
}
--------------------------------------------------------------------------------------------------
d)
The return type must be ostream & for the overloaded output(<<) operator. Because
the overloaded function displays the Object details in a output stream and this should
be returned to the main function, where it is called.
--------------------------------------------------------------------------------------------------
e)
We make the operator overloading function a friend of the class because it can be called
without creating an object.
--------------------------------------------------------------------------------------------------
f)
friend ostream &operator<<( ostream &output, const Point &P )
{
output << P.xloc << " ," << P.yloc;
return output;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.