C++ 将数据从文件读入多个类对象C++;
如何使用默认类构造函数创建多个新类对象 对于一个项目,我必须编写一个程序,将三个类对象写入一个文件。我所做的。。。下一部分是使用readData函数将数据读回三个单独的类对象,然后显示数据。我完全不知道该怎么做,所以readData函数中没有任何代码 下面是对象写入文件时的外观示例C++ 将数据从文件读入多个类对象C++;,c++,constructor,ifstream,C++,Constructor,Ifstream,如何使用默认类构造函数创建多个新类对象 对于一个项目,我必须编写一个程序,将三个类对象写入一个文件。我所做的。。。下一部分是使用readData函数将数据读回三个单独的类对象,然后显示数据。我完全不知道该怎么做,所以readData函数中没有任何代码 下面是对象写入文件时的外观示例 employee name(21, "first last", "45 East State", "661-9000", 30, 12.00); 这是我的大部分代码employee类相当基本,但这里是默认的类构造函
employee name(21, "first last", "45 East State", "661-9000", 30, 12.00);
这是我的大部分代码employee类相当基本,但这里是默认的类构造函数
employee::employee ();
employee::employee(int locEmpNumber, string locName, string locaddress, string locphone, double locHrWorked, double locHrWage)
#include "employee.h"
#include <string>
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
void writeData (const employee& e);
void readData (const employee& e);
void printCheck (const employee& e);
int main( )
{
//Declarations
const int ONE = 1;
const int TWO = 2;
int userInput;
cout << "This program has two options:" << endl;
cout << "1 - Create a data files called 'EmployeeInfo.txt', or" << endl;
cout << "2 - Read data from a file and print paychecks." << endl;
cout << "Please enter (1) to create a file or (2) to print checks: ";
cin >> userInput;
if (userInput == ONE)
{
//Create employee objects:
employee joe(37, "Joe Brown", "123 Main St.", "123-6788", 45, 10.00);
employee sam(21, "Sam Jones", "45 East State", "661-9000", 30, 12.00);
employee mary(15, "Mary Smith", "12 High Street", "401-8900", 40, 15.00);
ofstream empFile ("EmployeeInfo.txt");
//Employee objects to write themselves out to the file.
writeData(joe);
writeData(sam);
writeData(mary);
//Close the file.
empFile.close();
//Print an message that creation of the file is complete.
system("CLS");
cout << "\nCreation of 'EmployeeInfo.txt' has completed.\n";
cout << "\nYou can now run option 2.\n";
//Exit.
system("PAUSE");
return 0;
}
else if (userInput == TWO)
{
//Create three new Employee objects, using the default Employee constructor.
//Open the file that you just saved.
//Have each object read itself in from the file.
//Call the printCheck( ) function for each of the three new objects, just as you did in the previous project.
}
else
{
system("CLS");
cout << "Incorrect entry.... Please try again and follow directions closely! \n" << endl;
system("PAUSE");
return 0;
}
}
void writeData(const employee& e)
{
fstream empFile;
empFile.open ("EmployeeInfo.txt", ios::app);
empFile << e.getEmpNumber() << "\n";
empFile << e.getName() << "\n";
empFile << e.getAddress() << "\n";
empFile << e.getPhone() << "\n";
empFile << e.getHrWorked() << "\n";
empFile << e.getHrWage() << "\n";
}
void readData(const employee& e)
{
fstream empFile;
empFile.open ("EmployeeInfo.txt", ios::in);
if(empFile.fail())
{
cout << "File could not be open. Please try option 1 then option 2.\n" << endl;
return;
}
}
employee::employee();
employee::employee(int locEmpNumber、string locName、string locaddress、string locphone、double locHrWorked、double locHrWage)
#包括“employee.h”
#包括
#包括
#包括
#包括
使用名称空间std;
无效书面数据(施工人员和设备);
无效读取数据(const employee&e);
作废打印检查(施工人员和设备);
int main()
{
//声明
常数int ONE=1;
常数int 2=2;
int用户输入;
cout很高兴看到您已经努力解决了这个问题。但是,您在问题中提出的内容与代码中的一些注释之间存在不匹配。我似乎很清楚,您的摘要的一个关键部分是employee对象本身需要能够将自身写入文件并读取自身从文件中
您已经编写了将对象内容写入文件的代码,而不是让对象将自身写入文件。这看起来像是在吹毛求疵,但这就是面向对象编程的本质。将功能封装在对象本身中才是真正的目标
我在下面提供了一些代码来帮助您。希望这对您有意义
class employee
{
private:
int _locEmpNumber;
std::string _locName;
std::string _locAddress;
std::string _locPhone;
double _locHrWorked;
double _locHrWage;
public:
employee();
employee(int locEmpNumber, std::string locName, std::string locAddress, std::string locPhone, double locHrWorked, double locHrWage);
//object should be able to save itself as per your project brief.
void writeData(std::ofstream &empFile);
//object should be able to read itself from file as per your project brief
void readData(std::ifstream &empFile);
};
employee::employee()
{
_locEmpNumber = 0;
_locHrWorked = _locHrWage = 0;
}
employee::employee(int locEmpNumber, std::string locName, std::string locAddress, std::string locPhone, double locHrWorked, double locHrWage)
{
_locEmpNumber = locEmpNumber;
_locName = locName;
_locAddress = locAddress;
_locPhone = locPhone;
_locHrWorked = locHrWorked;
_locHrWage = locHrWage;
}
//
//From what I can glean from your brief ...
//Employee objects to write themselves out to the file!!!
void employee::writeData(std::ofstream &empFile)
{
empFile << _locEmpNumber << std::endl;
empFile << _locName << std::endl;
empFile << _locAddress<< std::endl;
empFile << _locPhone << std::endl;
empFile << _locHrWorked << std::endl;
empFile << _locHrWage << std::endl;
}
//
//Again, from what I can glean from your brief ...
//Have each object read itself in from the file!!!
void employee::readData(std::ifstream &empFile)
{
//Normally you would have error handling in a method like this and
//would either return a response that indicates that the operation
//succeded or failed. You might alternatively use exception handling
//or indeed a combination of both.
//
//Normally you would reset all members to initial / empty values before
//reading values into them from your file. In this case we will omit that
//for the purposes of simplicity. The main reason you would reset members
//is to ensure that when reusing an object you don't end up with partial
//data from the current "read" operation mixed with partial data that
//was already in the object before you started reading.
std::string inputStr;
std::getline(empFile, inputStr);
_locEmpNumber = atoi(inputStr.c_str());
std::getline(empFile, _locName);
std::getline(empFile, _locAddress);
std::getline(empFile, _locPhone);
std::getline(empFile, inputStr);
_locHrWorked = atof(inputStr.c_str());
std::getline(empFile, inputStr);
_locHrWage = atof(inputStr.c_str());
}
int main(int argc, char* argv[])
{
//Declarations
const int ONE = 1;
const int TWO = 2;
int userInput;
std::cout << "This program has two options:" << std::endl;
std::cout << "1 - Create a data files called 'EmployeeInfo.txt', or" << std::endl;
std::cout << "2 - Read data from a file and print paychecks." << std::endl;
std::cout << "Please enter (1) to create a file or (2) to print checks: ";
std::cin >> userInput;
if (userInput == ONE)
{
//Create employee objects:
employee joe(37, "Joe Brown", "123 Main St.", "123-6788", 45, 10.00);
employee sam(21, "Sam Jones", "45 East State", "661-9000", 30, 12.00);
employee mary(15, "Mary Smith", "12 High Street", "401-8900", 40, 15.00);
std::ofstream empFile ("EmployeeInfo.txt");
//Employee objects to write themselves out to the file.
joe.writeData(empFile);
sam.writeData(empFile);
mary.writeData(empFile);
// writeData(joe);
// writeData(sam);
// writeData(mary);
//Close the file.
empFile.close();
//Print an message that creation of the file is complete.
system("CLS");
std::cout << "\nCreation of 'EmployeeInfo.txt' has completed.\n";
std::cout << "\nYou can now run option 2.\n";
//Exit.
system("PAUSE");
return 0;
}
else if (userInput == TWO)
{
//Create three new Employee objects, using the default Employee constructor.
employee joe;
employee sam;
employee mary;
//Open the file that you just saved.
std::ifstream empFile("EmployeeInfo.txt");
//Have each object read itself in from the file.
joe.readData(empFile);
sam.readData(empFile);
mary.readData(empFile);
empFile.close();
//Call the printCheck( ) function for each of the three new objects, just as you did in the previous project.
//I'll leave it to you to add this yourself.
}
else
{
system("CLS");
std::cout << "Incorrect entry.... Please try again and follow directions closely! \n" << std::endl;
system("PAUSE");
return 0;
}
return 0;
}
class员工
{
私人:
国际地点编号;
std::string\u locName;
std::字符串地址;
std::string_locPhone;
双罗氏加工;
双倍工资;
公众:
雇员();
员工(int LOCEMPNAME,std::string locName,std::string locAddress,std::string locPhone,双locHrWorked,双locHrWage);
//对象应该能够根据您的项目摘要保存自身。
void writeData(std::ofstream&empFile);
//对象应该能够根据项目概要从文件中读取自身
void readData(std::ifstream和empFile);
};
雇员::雇员()
{
_LOCEMPNAME=0;
_locHrWorked=_locHrWage=0;
}
employee::employee(int locEmpNumber,std::string locName,std::string locAddress,std::string locPhone,double locHrWorked,double locHrWage)
{
_LOCEMPNAME=LOCEMPNAME;
_locName=locName;
_locAddress=locAddress;
_locPhone=locPhone;
_locHrWorked=locHrWorked;
_locHrWage=locHrWage;
}
//
//从你的简报中我能收集到。。。
//员工对象将自己写入文件!!!
void employee::writeData(std::ofstream&empFile)
{
empFile欢迎使用StackOverflow!为了更好地回答您的问题,通常建议您提供一个关于问题的最小示例。因此,尽可能地分解代码。发布大块代码很可能会吓跑潜在的帮助者。请参阅sscce.org