Hello, I was just wondering what specifically the () does in the first and second line after the "public:" declaration? When I make a point object without any inputs, I get x = 0, y = 0, and z = 0 (one calls the x,y,z coordinates with point.x point.y and point.z). When I make a point object with arguments say (1,2,3) x = 1, y = 2, and z = 3. So I guess my question is if "x(0)" is equivalent to "int x = 0"? If so, how would the parenthesis know what type to give the x, y, and z variables within the class Point?
#include <iostream>
using namespace::std;
class Point
{
public:
Point(): x(0), y(0), z(0) {}
Point(int x_, int y_, int z_): x(x_), y(y_), z(z_) {}
int x;
int y;
int z;
int normsq() const {return x * x + y * y + z * z;}
void set(int x_, int y_, int z_) {x = x_; y = y_; z = z_;}
int get() const
{
union
{
int val;
signed char bytes[4];
};
bytes[0] = x;
bytes[1] = y;
bytes[2] = z;
bytes[3] = 0;
return val;
}
Point operator-(const Point& c) const {return Point(x - c.x, y - c.y, z - c.z);}
Point operator+(const Point& c) const {return Point(x + c.x, y + c.y, z + c.z);}
bool operator==(const Point& c) const {return x == c.x && y == c.y && z == c.z;}
bool operator!=(const Point& c) const {return x != c.x || y != c.y || z == c.z;}
};
That's how you define a constructor, or any function that takes no parameters. The reason x, y, and z are set to 0 is because of the lines following the colon: the "initializer list".