I know that an object of another class can be initiated using the method below but this is a variable that was declared and not a function. I don't fully understand the code below.
Pet(string name) : name(name) {}
Full class:
1 2 3 4 5 6 7 8 9 10 11
class Pet {
protected: string name;
public: Pet(string name) : name(name) {}
virtualvoid MakeSound(void) { cout << name << " is silent :(" << endl; }
};
class Dog : public Pet {
public: Dog(string name) : Pet(name) {}
void MakeSound(void) { cout << name << " says: Woof!" << endl; }
};
with the const, it should not be there in the class declaration, but we do put it there in the function definition. This so someone reading the class declaration can see: "oh I can send it a string", but the const in the definition means it's value won't be changed inside the function.