0

In the following program, where is pt1 getting it's values (1, 0)? I get pt3's values (5, 10) because I can see them in the code.

#include <iostream>
using namespace std;

class Point
{
    private:
        int x, y;
    public:
        Point() {}
        Point(int new_x, int new_y) { set(new_x, new_y); }

        void set(int new_x, int new_y);
        int get_x();
        int get_y();
};

int main()
{
    Point pt1, pt2;
    Point pt3(5, 10);

    cout << "The value of pt1 is ";
    cout << pt1.get_x() << ", ";
    cout << pt1.get_y() << endl;

    cout << "The value of pt3 is ";
    cout << pt3.get_x() << ", ";
    cout << pt3.get_y() << endl;

    return 0;
}

void Point::set(int new_x, int new_y)
{
    if(new_x < 0)
        new_x *= -1;
    if(new_y < 0)
        new_y *= -1;
    x = new_x;
    y = new_y;
}

int Point::get_x()
{
    return x;
}

int Point::get_y()
{
    return  y;
}
3
Contributors
2
Replies
14
Views
3 Years
Discussion Span
Last Post by AndrisP
0

if you want to set default values to (0,0) then replace lines 9 and 10 to this

        Point(int new_x=0, int new_y=0) { set(new_x, new_y); }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.