设计带构造函数的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         Dog(string,int,char,double);
15         void GetName(){cin>>name;return;}
16         void GetAge(){cin>>age;return;}
17         void GetSex(){cin>>sex;return;}
18         void GetWeigth(){cin>>weight;return;}
19         void speak(){cout<<"Arf!Arf!"<<endl;return;}
20         void show()
21         {
22             cout<<name<<endl;
23             cout<<age<<endl;
24             cout<<sex<<endl;
25             cout<<weight<<endl;
26             return;
27         }
28 };
29 
30 Dog::Dog(string name, int age, char sex, double weight)
31 {
32     this->name=name;
33     this->age=age;
34     this->sex=sex;
35     this->weight=weight;
36 }
37 
38 int main()
39 {
40     string name;
41     int age;
42     char sex;
43     double weight;
44     cin>>name>>age>>sex>>weight;
45     Dog one(name,age,sex,weight);
46     one.show();
47     one.speak();
48     return 0;
49 }

 

posted @ 2020-03-29 00:01  Conan-jine  阅读(412)  评论(0编辑  收藏  举报