设计Dog类 代码参考

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 class Dog
 7 {
 8     private:
 9         string name;
10         int age;
11         char sex;
12         double weight;
13     public:
14         void GetName(){cin>>name;return;}
15         void GetAge(){cin>>age;return;}
16         void GetSex(){cin>>sex;return;}
17         void GetWeight(){cin>>weight;return;}
18         void ShowMyDog(){
19             cout<<"It is my dog."<<endl;
20             cout<<"Its name is "<<name<<'.'<<endl;
21             cout<<"It is "<<age<<" years old."<<endl;
22             if(sex=='m')    cout<<"It is male."<<endl;
23             else    cout<<"It is female."<<endl;
24             cout<<"It is "<<weight<<" kg."<<endl;
25             return;
26         }
27 };
28 
29 int main()
30 {
31     Dog one;
32     one.GetName();
33     one.GetAge();
34     one.GetSex();
35     one.GetWeight();
36     one.ShowMyDog();
37     return 0;
38 }

 

posted @ 2020-03-28 17:00  Conan-jine  阅读(334)  评论(0编辑  收藏  举报