-
inheritance
/* Here's my question, how would I go about making a method that when called up would tell me what breed the dog object is*/
//Listing 12.2 Using a derived object
#include <iostream>
enum BREED { GOLDEN, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB };
class Mammal
{
public:
// constructors
Mammal():itsAge(2), itsWeight(5){}
~Mammal(){}
//accessors
int GetAge() const { return itsAge; }
void SetAge(int age) { itsAge = age; }
int GetWeight() const { return itsWeight; }
void SetWeight(int weight) { itsWeight = weight; }
//Other methods
void Speak()const { cout << "Mammal sound!\n"; }
void Sleep()const { cout << "shhh. I'm sleeping.\n"; }
protected:
int itsAge;
int itsWeight;
};
class Dog : public Mammal
{
public:
// Constructors
Dog():itsBreed(GOLDEN){}
~Dog(){}
// Accessors
BREED GetBreed() const { return itsBreed; }
void SetBreed(BREED breed) { itsBreed = breed; }
// Other methods
void WagTail() const { cout << "Tail wagging...\n"; }
void BegForFood() const { cout << "Begging for food...\n"; }
private:
BREED itsBreed;
};
int main()
{
Dog fido;
fido.Speak();
fido.WagTail();
cout << "Fido is " << fido.GetAge() << " years old\n";
int x;
cin>>x;
return 0;
}
-
Return itsBreed. It will be an BREED, rather than a string as you probably want, however. You can write a function taking a BREED and returning a string, and use that for console output if you wish.
-
Oh, if I want to return the name of the breed I would have to do something with strings? Ok, that should be covered later on in the book, I guess I was getting a little bit ahead of myself, thanks.
-
Let me guess, Teach Yourself C++ in 21 Days :D
-
Exactly, how did you guess?