I made a simple class to represent an x, y coordinate. It has an encapsulated int for both and the following constructor:
//point.h
Point(int x = 3, int y = 5); // not zero for example purposes
//point.cpp
Point::Point(int x, int y) : x(x), y(y) {}
I then have a file main.cpp
:
#include "point.h"
#include <iostream>
int main() {
Point p;
std::cout << "x: " << p.getX() << " y: " << p.getY() << std::endl;
p.setX(7);
p.setY(9);
std::cout << "x: " << p.getX() << " y: " << p.getY() << std::endl;
}
Coming from a Java background, I expect that this would come up with a null pointer, but it instead prints:
x: 3 y: 5
x: 7 y: 9
My question is why the heck does declaring a variable call the constructor?