c++设计类的问题解答


#include"iostream"
using namespace std;
class person{
protected:
 int age;
 int height;
 double weight;
 static int num;
public:
 person(int a,int h,double w):age(a),height(h),weight(w){ num++; }
 void Show(){
  cout << "age: " << "height:  " << "weight:   "  << endl ;
  cout << age << "   " << height << "       " << weight << endl ;
 }
 static int number(){
  return num;
 }
};
 
int person::num=0;
 
int main()
{
 person a(10,30,140),b(20,60,170);
 a.Show();
 b.Show();
 cout << "person:" << endl ;
 cout << person::number() << endl ;
}

请问怎么把这个代码改成输入格式为“按姓名、年龄、身高和体重依次输入每个人的信息,以exit结束”
这是原本的题目“设计一个People 类,该类的数据成员有姓名、年龄、身高、体重和人数,其中人数为静态数据成员,成员函数有构造函数、显示和显示人数。其中构造函数由参数姓名、年龄、身高和体重来构造对象;显示函数用于显示人的姓名、年龄、身高和体重;显示人数函数为静态成员函数,用于显示总的人数。”
非常感谢!

如有帮助望采纳

#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Person
{
    public:
        string name;
        int age;
        int height;
        double weight;
        static int num;
    public:
        Person()
        {
            num++;
        }
        Person(string name,int age,int height,double weight)
        {
            this->name = name;
            this->age = age;
            this->height = height;
            this->weight = weight;
            num++;
        }
        void show()
        {
            cout <<"name:" <<name<< "\tage:" << age<<"\theight:"<<height<<"\tweight:"<<weight<<endl;
        }
        static int number()
        {
            return num;
        }
};

int Person::num = 0;

int main()
{
    cout<<"按姓名、年龄、身高和体重依次输入每个人的信息,以exit结束"<<endl;
    vector<Person> ps;
    while(true)
    {
        Person* p = new Person();
        cin>>p->name;
        if(p->name == "exit")
        {
            break;
        }
        cin>>p->age>>p->height>>p->weight;
        ps.push_back(*p);
    }
    for(int i = 0; i < ps.size(); ++i)
    {
        ps[i].show();
    }
    cout << "the number of person: " ;
    cout << Person::number() - 1<< endl ;
    return 0;
}

img

如有帮助,点个采纳吧~


#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

class person {
protected:
    int age;
    int height;
    double weight;
    static int num;
public:
    person(int a, int h, double w) : age(a), height(h), weight(w) {
        this->age = a;
        this->height = h;
        this->weight = weight;
        num++;
    }

    void Show() {
        cout << "age: " << "height:  " << "weight:   " << endl;
        cout << age << "   " << height << "       " << weight << endl;
    }

    static int number() {
        return num;
    }
};

int person::num = 0;


vector<string> split(const string& str, const string& delim) {
    vector<string> res;
    if ("" == str) return res;
    //先将要切割的字符串从string类型转换为char*类型
    char *strs = new char[str.length() + 1]; //不要忘了
    strcpy(strs, str.c_str());

    char *d = new char[delim.length() + 1];
    strcpy(d, delim.c_str());

    char *p = strtok(strs, d);
    while (p) {
        string s = p; //分割得到的字符串转换为string类型
        res.push_back(s); //存入结果数组
        p = strtok(NULL, d);
    }
    return res;
}

int main() {
    vector<person> ps;
    vector<string> ss;
    string s;
    do{
        if (!s.empty()) {
            ss = split(s, " "); // 分割字符串
            ps.emplace_back(atoi(ss[0].c_str()), atoi(ss[1].c_str()), atof(ss[2].c_str())); // 字符串转换为int、double
        }
        getline(cin, s); // 读取一行数据

    } while (s != "exit"); // 用户输入exit,结束读取


    for (auto iter : ps) // 遍历vector
    {
        iter.Show();
    }

    cout << "person:" << endl;
    cout << person::number() << endl;
}