输入信息C++员工类别
Employee class C++ inputting the information.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
string employee_firstname;
string employee_lastname;
string idnumber;
string address;
string phonenumber;
string tenure;
public:
Employee() {
employee_firstname = "";
employee_lastname = "";
idnumber = "";
address = "";
phonenumber = "";
tenure = "0";
}
Employee(string fn, string ln, string id, string ad, string ph, string ten) {
employee_firstname = fn;
employee_lastname = ln;
idnumber = id;
address = ad;
phonenumber = ph;
tenure = ten;
}
string getFirstName();
string getLastName();
string getidnumber();
string getAddress();
string getPhone();
string getTenure();
void setFirstname(string fn) {
employee_firstname = fn;
}
void setLastname(string ln) {
employee_lastname = ln;
}
void setidnumber(string id) {
idnumber = id;
}
void setaddress(string ad) {
address = ad;
}
void setphonenumber(string ph) {
phonenumber = ph;
}
void settenure(string ten) {
tenure = ten;
}
};
string Employee::getFirstName() {
return employee_firstname;
}
string Employee::getLastName() {
return employee_lastname;
}
string Employee::getidnumber() {
return idnumber;
}
string Employee::getAddress() {
return address;
}
string Employee::getPhone()
{
return phonenumber;
}
string Employee::getTenure() {
return tenure;
}
const int employee_num = 3;
int main()
{
Employee num[employee_num] = {
("John", "Smith", 4752, "8971 herlo st", "916-628-8452", 8),
("Cathy", "Guringo", 5826, "538 reed ct", "310-852-6654", 5),
("Kyle", "Ford", 7856, "292 murrietta st", "323-547-7423", 3),
};
for (int i = 0; i < employee_num; i++)
{
cout << num[i].getFirstName() << " ";
cout << num[i].getLastName() << " ";
cout << num[i].getidnumber() << " ";
cout << num[i].getAddress() << " ";
cout << num[i].getPhone() << " ";
cout << num[i].getTenure() << " ";
}
return 0;
}
我会完全诚实。我不明白如何在此处检索和显示员工的信息。我问过教授,他解释的方式对我来说真的没有意义。他真的无法用不同的方式解释它。
我的教授的提示是这样的:
编写包含以下字段的 Employee 类:
- 姓
- 名字
- 员工编号
- 地址
- 电话号码
- 受雇年限
该类应有两个构造函数: - 默认构造函数,将字段设置为空字符串 (") 和 0 表示使用年份 - 接受三个字段作为参数并将它们分配给姓氏、名字和员工 ID 的构造函数。
编写适当的突变器方法来存储字段中的值,并编写访问器方法来返回字段中的值。
在 main 函数中,通过从键盘输入每个对象的字段来创建三个 Employee 对象。
您的初始化存在一些问题:
- 您正在将
int
分配给string
- 使用
()
而不是{}
像这样更改它:
Employee num[employee_num] = {
{"John", "Smith", "4752", "8971 herlo st", "916-628-8452", "8"},
{"Cathy", "Guringo", "5826", "538 reed ct", "310-852-6654", "5"},
{"Kyle", "Ford", "7856", "292 murrietta st", "323-547-7423", "3"}
};
如果你想从用户那里获取这些数据,你可以使用std::getline
和你的 setter 函数将 give 字符串分配给你的类成员。
或者你可以重载operator >>
以你想要的方式获取用户输入,如下所示:
friend istream& operator>>(istream& is, Employee& emp)
{
std::cout << "Enter employee first name :";
std::getline(is, emp.employee_firstname);
std::cout << "Enter employee last name :";
std::getline(is, emp.employee_lastname);
std::cout << "Enter employee id number :";
std::getline(is, emp.idnumber);
std::cout << "Enter employee address :";
std::getline(is, emp.address);
std::cout << "Enter employee phone number :";
std::getline(is, emp.phonenumber);
std::cout << "Enter employee tenure :";
std::getline(is, emp.tenure);
return is;
}
另一件事是,您可以重载operator <<
,以便您的类按照您想要的方式进行打印,如下所示:
friend ostream& operator<<(ostream& os, Employee const & emp)
{
return os << emp.employee_firstname << " " << emp.employee_lastname << " " << emp.idnumber << " "
<< emp.address << " " << emp.phonenumber << " " << emp.tenure << " " << endl;
}
main
函数中像这样使用它:
for(int i = 0; i < employee_num; i++)
{
cout << num[i];
}
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何设置一个范围来提取我想要获得的信息
- 系统参数信息A 与 SPI_GETMOUSE 返回 0
- 为什么 gcovr 会生成空覆盖率统计信息?
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 将信息输入到下面显示的结构向量中的正确语法/格式是什么
- 调试符号中缺少 QObject 类信息(编辑但存在其他 Qt 类)
- 为什么数组大小信息可用于"sizeof"运算符和 delete[] 运算符,但在将数组作为参数传递到
- Pisarze - 来自波兰奥林匹克信息学的数据分析任务
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- C++20 个模块在 clang (Windows):在最简单的示例中键入信息错误
- C++ 如何暂停控制台输出,以便用户可以在继续之前阅读信息
- 如何从WebAssembly模块检测浏览器信息?
- Visual Studio 2017 C++,不能使用 typeid() 获取信息对象,缺少指针?;
- 如何实现具有多个平台__FILE__和__LINE__信息的 C/C++ 可变参数日志记录宏?
- 按字母顺序打印存储在字符数组中的信息
- std::extent 实现详细信息说明
- 命名空间信息会影响C++的可读性