Parenthesis in Declaration Question

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

inline ostream& operator<<(ostream& out, const Point& c)
{
return (out << c.x << " " << c.y << " " << c.z << " ");
}
Last edited on
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".
So then are those 2 constructors just a short way of writing:

Point::Point(){
x = 0;
y = 0;
z = 0;
}

and

Point::Point(int x_, int y_, int z_){
x = x_;
y= y_;
z = z_;
}

given that x, y, and z were declared ints in public? Thank you for the help.
Almost, but not the same.
Suppose a constant attribute or one that haven't got a default constructor. You will need to use the initializer list then.
1
2
3
int x = 0; //initializer list
int x; //assign in the constructor
x = 0;
Last edited on
Topic archived. No new replies allowed.