c++,类、对象练习题之狗类

 
#include <iostream>

using namespace std;
class Dog
{
private:
    string sex;
    int age;
    string kind;
public:
    //显式调用,构造初始化列表
    explicit Dog()
        :sex("male"),age(5),kind("中华田园犬"){}
    void show()
    {
        cout<<"性别:"<<sex<<" 年龄:"<<age<<" 品种:"<<kind<<endl;
    }
    //写函数
    void set_sex(string s)
    {
        sex=s;
    }
    void set_age(int a)
    {
        age=a;
    }
    void set_kind(string k)
    {
        kind=k;
    }
    //读函数
    int get_age()
    {
        return age;
    }
    string get_sex()
    {
        return sex;
    }
    string get_kind()
    {
        return kind;
    }

};
inline Dog *birth( Dog d)
{
    if(d.get_sex()=="female")

    {

        if(d.get_age()>=2 &&d.get_age()<=5)
        {
            Dog *dog=new Dog;
            return dog;
        }
    }
    else
        return NULL;

}

int main()
{
    string sex;
    string kind;
    int age;
    Dog *dog1=new Dog;
    dog1->show();
    cout<<endl;
    Dog *dog2=new Dog;
    cout<<"请输入第二只狗的性别,年龄,品种"<<endl;
    getline(cin,sex);
    cin>>age;
    getchar();
    getline(cin,kind);

    dog2->set_sex(sex);

    dog2->set_age(age);

    dog2->set_kind(kind);
    cout<<"第二只狗的性别、年龄、品种分别为"<<endl;
    dog2->show();
    cout<<endl;

    Dog *dog3 =birth(*dog2);
    if(dog3==NULL)
        cout<<"没有小狗"<<endl;
    else
    {
        cout<<"输入小狗的性别"<<endl;
        getline(cin,sex);
        dog3->set_sex(sex);
        dog3->set_age(1);
        if(dog2->get_kind()==dog1->get_kind())
            dog3->set_kind(dog1->get_kind());
        else
            dog3->set_kind(dog1->get_kind()+dog2->get_kind());
        cout<<"小狗的信息为"<<endl;
        dog3->show();

        delete dog3;
    }
    delete dog2;
    delete dog1;
    return 0;
}



  
 
 

                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值