设计Dog类 代码参考

0
8
 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 }

<

发布回复

请输入评论!
请输入你的名字