程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 派生與私有保護

派生與私有保護

編輯:C++入門知識

[cpp] 
#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; 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved