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;
}

Recommended Answers

All 2 Replies

Most likely, these are just the values that happen to be in those memory addresses when the prgoram starts.

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); }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.