Athar gave you a good explanation, but I will elaborate on the pointers and delete
Don't use pointers in this case. You really, really don't need them; just using the primitive types themselves will suffice. By using pointers, the compiler-give constructor will just copy the pointers and not what they point to. What do you think will happen for cat = fish; ?
As for the delete, in C++ the comma operator evaluates each opearand and then returns the last:
int x = SomeFunc(), SomeOtherFunc, 0, 42;
SomeFunc() is called first, then SomeOtherFunc(), and x receives a value of 42.
For such a small class, you don't need to externally define the functions; you can define them inline like in Java and Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
class Animal
{
double x, y, speed;
public:
Animal() : x(0.0), y(0.0), speed(0.0) {}
Animal(double X, double Y, double Speed) : x(X), y(Y), speed(Speed) {}
void Print()
{
cout << "X=" << x << endl;
cout << "Y=" << y << endl;
cout << "Speed=" << speed << endl;
}
//etc...
};
|
Though for large classes, the class definition is usually in a header (*.h, *.hpp) and the implementation in a source file (*.cc, *.cpp, *.cxx). And even then, you don't have to explicitly say this->SomeVar, as SomeVar will already be in scope.