c++类与对象题目,老是弄不出来,,求解答啊啊啊啊

2025-04-16 08:40:15
推荐回答(2个)
回答1:

#include #include using namespace std; class Student{ private: string name, street, city; int age, zip; public: string getName() {return name;} string getStreet() {return street;} string getCity() {return city;} int getAge() {return age;} int getZip() {return zip;} void setName(string x) {name = x;} void setStreet(string x) {street = x;} void setCity(string x) {city = x;} void setAge(int x) {age = x;} void setZip(int x) {zip = x;} void print(){ cout <<'\t' << getName() <<'\t' << getAge() <<'\t' << getStreet() <<'\t' << getCity() <<'\t' << getZip() << endl; } } #define N 4 int main(){ Student stu[N]; int i, t; string x; cout <<"\t姓名\t年龄\t\t街道\t\t城市\t邮编" << endl; for (i = N; i--; stu[i].print()){ stu[i] = new Student(); cin >> x; stu[i].setName(x); cin >> t; stu[i].setAge(t); cin >> x; stu[i].setStreet(x); cin >> x; stu[i].setCity(x); cin >> t; stu[i].setZip(x); } return 0; }

回答2:

Student stu[10]; for(i=0;i<10;i++) {cin>>stu[i].name>>stu[i].age>>stu[i].street>>stu[i].city>>stu[i].zip>>....等等你要的属性;}//这是输入 输出就找样子打印好了