简体   繁体   中英

How can I overload the << operator so I can write the data of an object to a file?

I have an object of type piggyBank and I need to write data of this object into a file and then read it. I am aware of how to write/read to a text file but how can I overload the << operator so I can write data about the object into a file?

My code for the class here:

piggyBank.h

#include <string>
#ifndef PIGGYBANK_H
#define PIGGYBANK_H

class PiggyBank
{
 private:
        std::string owner; 
        int balance; 
        bool broken; 
        int id;
        static int nrOfObjects;
    public:
        PiggyBank(void);
        PiggyBank(std::string name);
        std::string getOwnerName() const;
        void setOwnerName(std::string name);
        bool isBroken() ;
        int getBalance(int & amount) ;


};



#endif /* PIGGYBANK_H */

piggyBank.cpp

#include "PiggyBank.h"
#include "readWrite.h"
#include <string>
#include <iostream>
using namespace std;

int PiggyBank::nrOfObjects = 0; // outside constructor


PiggyBank::getNrOfObjects(){

    return nrOfObjects;

}

PiggyBank::PiggyBank(void){
    {this->owner="";this->balance=0;this->broken=false;}
     id = ++nrOfObjects;

}


PiggyBank::PiggyBank(std::string name, int startBalance){
     {this->owner=name;this->balance=startBalance;this->broken=false;}
     id = ++nrOfObjects;
}

string PiggyBank::getOwnerName() const{
    return this->owner;
}
void PiggyBank::setOwnerName(string name){
    this->owner = name;
}
bool PiggyBank::isBroken() {
    return this->broken;
}

int PiggyBank::getBalance(int & amount) {
    if(!broken){
        amount = balance;
        return 0;
    }else{
        return -1;
    }
}

You want the << operator to be a friend to the class and to return ostream& . Here is a simple example to get an idea about how it works.

friend ostream& operator << (ostream& os, const PiggyBank& obj)
{
   // For example something like this
   os << "Information that you want to output to the file:\n";
   os << "Owner: " << obj.owner << "\n";
   return os;
}

And then you can use it like this:

PiggyBack obj;
ofstream fout("file.txt");

// also check to see if the file opened correctly
if(fout.fail())
{
   cout << "File failed to open\n";
   return 0;
}
fout << obj;
// now you have written the owner information onto the file as well as the message before it
// inside the  operator<< overload

// close the resource at the end
fout.close();

The cool part is that you can use it to print to the console too by changing fout to be cout . For example:

cout << obj; // will print to the console

Very simple. Overload the inserter operator. Write this into your class:

friend std::ostream& operator << (std::ostream& os, const PiggyBank& pb) {
    return os << pb.owner << . . .   // Whatever you want

Then you can use the inserter operator as for any other data type:

int main() {

    PiggyBank pb;

    if (std::ofstream os("FileName"); os) {
        os << pb << "\n";
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM