fork download
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class Employee {
  7. private:
  8. char name[50];
  9. char city[50];
  10. char address[100];
  11. char postcode[10];
  12.  
  13. public:
  14. // 构造函数
  15. Employee(const char* initName, const char* initCity, const char* initAddress, const char* initPostcode) {
  16. strcpy(name, initName);
  17. strcpy(city, initCity);
  18. strcpy(address, initAddress);
  19. strcpy(postcode, initPostcode);
  20. }
  21.  
  22. // change_name函数
  23. void change_name(const char* newName) {
  24. strcpy(name, newName);
  25. }
  26.  
  27. // display函数
  28. void display() const {
  29. cout << "Name: " << name << endl;
  30. cout << "City: " << city << endl;
  31. cout << "Address: " << address << endl;
  32. cout << "Postcode: " << postcode << endl;
  33. }
  34. };
  35.  
  36. int main() {
  37. // 输入
  38. char name[50], city[50], address[100], postcode[10], newName[50];
  39. cin.getline(name, 50);
  40. cin.getline(city, 50);
  41. cin.getline(address, 100);
  42. cin.getline(postcode, 10);
  43. cin.getline(newName, 50);
  44.  
  45. // 创建Employee对象实例
  46. Employee emp(name, city, address, postcode);
  47.  
  48. // 第一次调用display函数输出属性信息
  49. emp.display();
  50.  
  51. // 调用change_name函数改变姓名
  52. emp.change_name(newName);
  53.  
  54. // 再次调用display函数输出属性信息
  55. emp.display();
  56.  
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0.01s 5276KB
stdin
jack
Beijing
Tsinghua Univ. Department of Automation
100084
rose
stdout
Name: jack
City: Beijing
Address: Tsinghua Univ. Department of Automation
Postcode: 100084
Name: rose
City: Beijing
Address: Tsinghua Univ. Department of Automation
Postcode: 100084