Question 4 5 points In class we discussed the importance of

Question 4 [5 points]

In class, we discussed the importance of a copy constructor. Take the example we saw from class:

class Point {

private:

    int x, y;

public:

    Point(int x1, int y1) {

        x = x1;

        y = y1;

    }

    Point(const Point& p2) {

        x = p2.x;

        y = p2.y;

    }

};

int main(void) {

    Point p1(10, 15);

    Point p2 = p1;      // copy constructor is invoked

}

What would happen if we removed the ampersand from the copy constructor and changed the call to Point(const Point p2)? The code will not compile, but please explain why this method invocation is fundamentally incorrect.

Solution

pointer means a variable whose value is the address of another variable. Like any variable or constant, you must declare a pointer before you can work with it.

if we donot place ampersand symbol adress will not located

error will shows like: invalid constructor

you probably meant \'Point (const Point&)\'

Point(const Point p2) {x = p2.x; y = p2.y; }

Question 4 [5 points] In class, we discussed the importance of a copy constructor. Take the example we saw from class: class Point { private: int x, y; public:

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site