类与对象 设计一个Dog类

题目内容:

设计一个Dog类,包含name、age、sex和weight等属性以及对这些属性操作的方法。实现并测试这个类。
根据类的封装性要求,把name、age、sex和weight声明为私有的数据成员,编写公有成员函数setdata()对数据进行初始化,GetName()、GetAge()、GetSex()和GetWeight()获取相应属性。初始化数据由用户输入。

 

输入格式:

Dog类对象的初始化数据

 

输出格式:

根据Dog类对象的初始化数据输出一句话,请严格按照格式输出,句末有点号。

 

输入样例:

ahuang 3 m 2.4

 

输出样例:

It is my dog.

Its name is ahuang.

It is 3 years old.

It is male.

It is 2.4 kg.

点击查看学习笔记

这题深刻的告诉我,你永远不知道自己的编译器会报什么奇奇怪怪的错误

以及"."的重要性!!!

#include <bits/stdc++.h>
using namespace std;
class Dog{
private:
    string name;
    int age;
    char sex;
    double weight;
public:
    void setdata(){
        cin>>name>>age>>sex>>weight;
    }
    void GetName(){
        cout<<"Its name is "<<name<<"."<<'\n';
    }
    void GetAge(){
        cout<<"It is "<<age<<" years old."<<'\n';
    }
    void GetSex(){
        if(sex=='m')cout<<"It is male."<<'\n';
        else cout<<"It is female."<<'\n';
    }
    void GetWeight(){
        cout<<"It is "<<weight<<" kg."<<'\n';
    }
}dog;
int main(){
    dog.setdata();
    cout<<"It is my dog."<<'\n';
    dog.GetName();
    dog.GetAge();
    dog.GetSex();
    dog.GetWeight();
    return 0;
}

 

 写就是这么个写法,简单明了,but我永远不会忘了我在这上面卡了多久

以下为吐槽自己蠢的过程,看官可以退出了

 

 

 

我那可爱的编译器不知道为何把我中间的几行隐藏了==【脏话】

 

于是 微笑.jpg

 

还有哦

 

我忘了一个"."

 

微笑.jpg;

 

 

 

然后

 

 

今天也是被自己蠢哭的一天呢

微笑.jpg 

 

posted @ 2020-04-02 15:26  小靖快去敲代码  阅读(892)  评论(0编辑  收藏  举报