Okay, is this a much more proper use of const correctness? Btw, thank you guys for all your help. I really am trying to learn this stuff.
Code:
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
class animal {
protected :
// Does this need to be const? Idt so...
string aniName;
public :
// This is everything cow is allowed to inherit and modify, right?
animal(string name) : aniName(name) { }
//getName is not allowed to change any member of the class now, correct?
string getName(void) const {
return aniName;
}
// We return a const char* which seems to be logically equivalent to
// a string and speak() is no longer allowed to change any members
// as well.
virtual const char* speak(void) const = 0;
};
class cow : public animal {
public :
cow(string name) : animal(name) { }
// This version of speak must match the base version, right? As such, it too
// is no longer allowed to change any members of the class.
virtual const char* speak(void) const {
return "\'Cow says 'moo', mother........er!\'";
}
};
int main(void) {
// Much like irl, my Taurus is constant in my life
const cow taurus("Sadie");
// And I am a constant beast as well.
const animal & beast = taurus;
cout << beast.getName() << " says, " << beast.speak() << endl;
return 0;
}