zoukankan      html  css  js  c++  java
  • 设计带构造函数的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 }
  • 相关阅读:
    喷水装置(一)
    下沙小面的(2)
    +-字符串
    非洲小孩
    寻找最大数(三)
    C
    寻找最大数
    阶乘之和
    背包问题
    python的内存管理机制(zz)
  • 原文地址:https://www.cnblogs.com/Conan-jine/p/12590346.html
Copyright © 2011-2022 走看看