struct继承例子

#include<iostream>
using namespace std;

enum BREED {GOLDEN, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB};

struct Mammal
{
public:
Mammal(): itsAge(2), itsWeight(5){}
~Mammal(){}

int GetAge() const
{
return itsAge;
}
void SetAge(int age)
{
itsAge = age;
}
int GetWeight() const
{
return itsWeight;
}
void SetWeight(int weight)
{
itsWeight = weight;
}

void Speak() const
{
cout << "\n Mammal sound!";
}
void Sleep() const
{
cout << "\n Shhh. I'm sleeping.";
}
protected:
int itsAge;
int itsWeight;
};

struct Dog : public Mammal
{
public:
Dog():itsBreed(GOLDEN){}
~Dog(){}
BREED GetBreed() const
{
return itsBreed;
}
void SetBreed(BREED breed)
{
itsBreed = breed;
}
void WagTai() const
{
cout << "Tall wagging ...\n";
}
void BegForFood() const
{
cout << "Begging for food...\n";
}

private:
BREED itsBreed;
};

int main()
{
Dog fido;
fido.Speak();
fido.WagTai();

cout << "Fido is " << fido.GetAge() << "years old \n";

return 0;
}

你可能感兴趣的:(struct)