标题:新手学习C++,请指导一下程序
取消只看楼主
CImage
Rank: 1
等 级:新手上路
帖 子:18
专家分:6
注 册:2010-8-24
结帖率:100%
 问题点数:0 回复次数:0 
新手学习C++,请指导一下程序
程序代码:
#include <iostream>

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

class Mammal
{
public:
    Mammal():itsAge(2),itsWeight(5){}//initialize 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 Speek()const{cout<<"Mammal sound!\n";}
    void Sleep()const{cout<<"shhh. I'm sleeping.\n";}

protected:
    int itsAge;
    int itsWeight;
};

class Dog:public Mammal
{
public:
    Dog():itsBreed(GOLDEN){}//what it means?
    ~Dog(){}

    BREED GetBreed()const{return itsBreed;}
    void SetBreed(BREED breed){itsBreed = breed;}

    void WagTail()const{cout<<"Tail wagging...\n";}
    void BegForFood()const{cout<<"Begging for food...\n";}

protected:
    BREED itsBreed;
};

int main()
{
    Dog Fido;
    Fido.Speek();
    Fido.WagTail();
    cout<<"Fido is "<<Fido.GetAge()<<" years old."<<endl;
    return 0;
}

-------------------------------------------------------
帮忙说一下Dog():itsBreed(GOLDEN){}到底有什么用啊。我就知道这是构造函数,用来初始化类对象的。itsBreed(GOLDEN)就是初始化,相当于itsBreed = GOLDEN;但是没发觉这个在程序中的用处啊。。。
搜索更多相关主题的帖子: 指导 学习 
2010-09-20 21:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-321011-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.065070 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved