Well this is not as much as something that I dont know how to implement, but more something that I dont know why I would want to implement it.
When I have a simple class, say this one:
class Cube
{
public:
Cube(int x, int y, int z);
int return_area();
private:
int width;
int height;
int other;
};
The way that I would normaly expand my functions would be like this:
Cube::Cube(int x, int y, int z)
{
width = x;
height = y;
other = z;
}
int Cube::return_area()
{
return width * height * other;
}
But recently, in one of my many C++ learning books, the sample code in the book was more like this:
class Cube
{
public:
Cube(int x, int y, int z): width(x), height(y), other(z) {}
// all the other code
};
They both have the same effect of asigning a value to the Cube's varaibles but does this second way have something extra? Is there a reason I should rather use the first way than the second (vice versa)?