Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a) Which line in the class definition contains the constructor? b) Give the prot

ID: 3842118 • Letter: A

Question

a) Which line in the class definition contains the constructor?

b) Give the prototype and defintion for the overloaded + operator, that will add two points.  You can define the addition of two points as follows: (x1,y1) + (x2,y2) = (x1+y1,x2+y2)

prototype:_____________________________________________

definition:

c) Assume the class defintion above, plus the added overloaded addition from question b).  What is printed by the following code?

int main()

{

        // Some points.

           Point a(5.2, -4.8);

           Point b(1.0, 9.0);

        Point d;  //look at default values

           a.print();

        b.print();

        d.print();

        cout << endl;

        b.move(2,-5);

        b.print();

       

        (a + b).print();

        cout << endl << endl;

       

        b.print();

        cout << " is " << b.dist(d) << " from ";

        d.print();

        cout << endl;

      

        }

d ) Suppose that we want users to input a point in the following format (note that the numbers here are just examples; the user can input any doubles):   (2,3.5)  Give the prototype and definition for the overloaded >> operator.

prototype:____________________________________________________________

definition:

2) Here is a class definition for a Point class. Objects in the class represent points on the Cartesian Graph. 1 class Point 2 public Uses default arguments to allow calling with zero, one or two values. Point (double x 0.0, double y 0.0) Computes distance to another point using Pythagorean theorem. double dist (Point other) Move the existing point. void move (double a double b) //print the point nicely 10 void print 11 12 private: 13 double xval double val; 15 Point:: Point (double x, double y) xval yval void Point move (double a double b) xval val b; double Point dist (Point other) double xd J xval other. xval; double ya yval other yval return sart (xd*xd yd yd); void Point print cout

Explanation / Answer

Ans: a) 5th line contain the constructer,

Ans: b)

To overload operator+ friend function: place the declaration of the operator function in the class prefixed by the keyword friend.

Prototype:

Let's show the code for the array1 class and the overloaded "+":

class array1

{

private:

                int val[MAXARRAY];

public:

                void setval(int index, int newval){

                                val[index]=newval;}

                int getval(int index){

                                return val[index];}

                friend array1 operator+(array1 x1, array1 x2);

};

array1 operator+(array1 x1, array1 x2)

{

                array1 temparray;

                for (int i=0; i<MAXARRAY; i++)

                                temparray.val[i]=x1.val[i] + x2.val[i];

                return temparray;

}

Ans: c) OUTPUT

(a+b)= (5.2,-4.8)+(2,-5)

=(7.2,-9.8)

d= -5

Ans: d )

the overloaded operator must be added as a member of the left operand. In this case, the left operand is an object of type std::ostream. std::ostream is fixed as part of the standard library.

class temp1

{

private:

    double a_x, a_y;

public:

    temp1(double x=0.0, double y=0.0): a_x(x), a_y(y)

    {

    }

    friend std::ostream& operator<< (std::ostream &out, const temp1&t1);

};

std::ostream& operator<< (std::ostream &out, const temp1&t1)

{

    out << " temp1(" << t1.a_x << ", " << t1.a_y << ")";

    return out;

}

int main()

{

    temp1 t1(2, 3.5);

    std::cout << t1;

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote