c++ - 如何将无效打印函数(在类中)打印到文本文件中
问题描述
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Person //class Person
{
public: //declare variables in the Person class
string lastName, firstName, address, city, state, zip, phone;
Person();
void printPerson() //function used to print out the content inside this class
{
cout << firstName << " ";
cout << lastName << endl;
cout << address << ", ";
cout << city << ", ";
cout << state << " ";
cout << zip << endl;
cout << "Phone Number: " << phone << endl;
}
};
class Customer : public Person { //declare class Customer; Customer is a derived class of Person
public:
int customerNumber; //declare variables
bool mailingList;
string comments;
Customer();
void printCustomer() //function used to print the content in the Customer class as well as the Person class
{
printPerson();
cout << "Customer Number: " << customerNumber << endl;
if (mailingList == true)
{
cout << "Mailing List: True" << endl;
}
if (mailingList == false)
{
cout << "Mailing List: False" << endl;
}
cout << "Comments: " << comments << endl;
}
};
int main()
{
ofstream outfile;
outfile.open("testOutput.txt", ios::out);
PreferredCustomer Igottaquestion; //create an instance for PreferredCustomer
Igottaquestion.lastName = "Igottaquestion";
Igottaquestion.firstName = "Ed";
Igottaquestion.address = "4901 Evergreen";
Igottaquestion.city = "Dearborn";
Igottaquestion.state = "MI";
Igottaquestion.zip = "48126";
Igottaquestion.phone = "313-436-9145";
Igottaquestion.customerNumber = 1;
Igottaquestion.mailingList = true;
Igottaquestion.comments = "class quentioner";
Igottaquestion.printPreferredCustomer();
Customer Kool; //create an instance for Customer
Kool.lastName = "Kool";
Kool.firstName = "James";
Kool.address = "1313 Colin Blvd";
Kool.city = "Dearborn Heights";
Kool.state = "MI";
Kool.zip = "48127";
Kool.phone = "313-836-9168";
Kool.customerNumber = 3;
Kool.mailingList = false;
Kool.comments = "Class Answerer";
Kool.printCustomer();
cout << endl;
system("pause");
return 0;
}
我的问题是,不仅在屏幕上,我还需要将结果打印/输出到文本文件。
通常,我可以这样做:
ofstream 输出文件;
outfile.open("testOutput.txt", ios::out);
输出文件 << 名字 << " ";
但是,在这种情况下,如何将 void print 函数(嵌入在类中)打印到文本文件中?
解决方案
这很简单,只需将您希望输出的位置作为参数传递给您的函数。像这样
void printPerson(std::ostream& out) // out is the destination we are writing to
{
out << firstName << " ";
out << lastName << endl;
out << address << ", ";
out << city << ", ";
out << state << " ";
out << zip << endl;
out << "Phone Number: " << phone << endl;
}
同样的变化printCustomer
等。
然后你使用这样的功能
ofstream outfile;
outfile.open("testOutput.txt", ios::out);
Customer Kool;
...
Kool.printCustomer(outFile); // write the customer to outfile
outFile << endl;
Person Aplus;
...
Aplus.printPerson(outFile); // write the person to outfile
推荐阅读
- povray - 根据Povray中的图像宽度计算图像高度?
- java - 如何重构此 Java 代码,以便创建键值对的字符串列表,而不是将它们打印出来?
- html - 做不出来
- javascript - 如何在电子阅读器应用程序中制作和拆分文本以使其水平
- r - 表函数省略底行
- java - Cordova 运行 android 错误:线程“主”java.lang.NullPointerException 中的异常
- if-statement - 如何在 sap 水晶报告中给出适当大小写和大写的 if 条件
- javascript - 如何定位导航栏并将其固定到页面中心?(CSS/引导程序)
- java - 如何在java中比较整个字符串中的单词并增加其计数?
- css - 响应式 CSS 导航栏中的徽标链接不起作用