1.创建一个employee类,该类中有姓名、城市、邮政编码等数据成员。成员函数setValue设定对象数据,display函数输出对象数据。 具体要求如下: (1)数据成员设定成private (2)成员函数设定成public (3)建立指针对象,利用指针对象对成员进行引用 (4)将类声明和成员函数的定义分开,放到.h和.cpp中
时间: 2024-01-22 09:19:01 浏览: 106
employee.h
```cpp
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
class Employee {
private:
std::string name;
std::string city;
std::string postalCode;
public:
void setValue(const std::string& name, const std::string& city, const std::string& postalCode);
void display() const;
};
#endif
```
employee.cpp
```cpp
#include "employee.h"
#include <iostream>
void Employee::setValue(const std::string& name, const std::string& city, const std::string& postalCode) {
this->name = name;
this->city = city;
this->postalCode = postalCode;
}
void Employee::display() const {
std::cout << "Name: " << name << std::endl;
std::cout << "City: " << city << std::endl;
std::cout << "Postal Code: " << postalCode << std::endl;
}
```
main.cpp
```cpp
#include "employee.h"
int main() {
Employee* employee = new Employee();
employee->setValue("John Smith", "New York", "10001");
employee->display();
delete employee;
return 0;
}
```
阅读全文