#include<iostream> #include<string> using namespace std; class employee { private: string name; int age; double salary; public: employee(string name="",int age=0,double salary=0) { this->name = name; this->age = age; this->salary = salary; } void setname(string name){ this->name = name; } void setage(int age) { this->age = age; } void setsalary(double salary) { this->salary = salary; } void display() { cout << "姓名:" << name << endl; cout << "年龄:" << age << endl; cout << "工资:" << salary << endl; } string getname() { return name; } int getage() { return age; } double getsalary() { return salary; } }; int main() { employee emp("王鹏",18,18000); emp.display(); emp.getname("王赟煜"); emp.setsalary(200000); emp.display(); return 0; }怎么修改程序
时间: 2024-01-02 08:03:45 浏览: 93
你需要将 `emp.getname("王赟煜");` 修改为 `emp.setname("王赟煜");`,因为 `getname()` 是获取姓名的函数,而不是设置姓名的函数。修改后的程序如下:
```
#include<iostream>
#include<string>
using namespace std;
class employee {
private:
string name;
int age;
double salary;
public:
employee(string name="",int age=0,double salary=0) {
this->name = name;
this->age = age;
this->salary = salary;
}
void setname(string name){
this->name = name;
}
void setage(int age) {
this->age = age;
}
void setsalary(double salary) {
this->salary = salary;
}
void display() {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "工资:" << salary << endl;
}
string getname() {
return name;
}
int getage() {
return age;
}
double getsalary() {
return salary;
}
};
int main() {
employee emp("王鹏",18,18000);
emp.display();
emp.setname("王赟煜");
emp.setsalary(200000);
emp.display();
return 0;
}
```
阅读全文