Search code examples
c++classvoid

How to get void print functions (that are within classes) printed to a text file


#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;
}

My question is, not only to the screen, I also need to print/output the result to a text file.

Normally, I can just do something like this:

ofstream outfile;

outfile.open("testOutput.txt", ios::out);

outfile << firstName << " ";

But, in this case, how can I get void print functions (that are embedded in classes) printed into a text file?


Solution

  • It's simple, just pass the place you want the output to go as a parameter to your functions. Like this

       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;
        }
    

    Same change for printCustomer etc.

    Then you use the functions like this

      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