欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

设计带构造函数的Dog类 代码参考

程序员文章站 2022-03-25 17:02:02
1 #include 2 #include 3 4 using namespace std; 5 6 class Dog 7 { 8 private: 9 string name; 10 int age; 11 char sex; 12 double weig ......
 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 }