派生与私有保护

#include <iostream>
using namespace std;
enum BREED{ black, red, white, blue};

class mam{
    public:
        mam(): age(4), weight(5){};
        ~mam(){}
        int get_age()const { return age; }
        void set_age(int age1) {age = age1; }
        int get_weight() const { return weight; }
        void set_weight(int x) { weight = x; }
        void speak() const { cout << "mam sound!\n"; }
        void sleep() const {cout << "I'am sleeping.\n"; }
    protected:
        int age;
        int weight;
};

class dog:public mam {
    public:
        dog():its_breed(red) {}
        ~dog(){}
        BREED get_breed()const { return its_breed; }
        void set_breed(BREED breed) { its_breed = breed; }
        void wag_tail() const { cout << "Tail wagging...\n";}
        void beg_for_food() const { cout << "begging for food...\n";}
    private:
        BREED its_breed;
};

int main()
{
    dog fido;
    fido.speak();
    fido.wag_tail();
    cout << "fido is " << fido.get_age() << "years old" << endl;
    cout << fido.get_breed() <<endl;
    fido.set_breed(white);
    cout << fido.get_breed() << endl;
    return 0;
}

你可能感兴趣的:(派生与私有保护)