C++ Operator overloading for << with abstract class - c++

Here are my header files:
//Frame.h
#pragma once
class Frame {
string frameName;
protected:
double fileSize;
vector<Attribute> attributes;
public:
Frame(string f, double size, vector<Attribute> d) :frameName(f), fileSize(size), attributes(d) {}
virtual ~Frame() {}
string& GetFrameName() { return frameName; }
Attribute& operator[](int);
int size() { return attributes.size(); }
virtual void Compress() = 0;
friend ostream& operator<<(ostream&, Frame&);
};
// AudioFrame
#pragma once
class AudioFrame :public Frame {
static const int RATES = 3;
static constexpr double BITRATE[]{128,160,192};
static constexpr double COMPRESSION_RATIO[]{11.1,9.1,7.1};
public:
AudioFrame(string frameName, double fileSize, vector<Attribute> d) :Frame(frameName,fileSize, d) {}
~AudioFrame(){}
void Compress();
friend ostream& operator<<(ostream&, AudioFrame&);
};
//ImageFrame.h
#pragma once
class ImageFrame :public Frame {
static const int BITS = 8;
static constexpr double COMPRESSION_RATIO = 6.0;
static constexpr double BITDEPTH_FACTOR[] {11.1,4.6,3.5,2.4,1.9,1.5,1.2,1.0};
public:
ImageFrame(string fileName, double fileSize, vector<Attribute> d) :Frame(fileName, fileSize, d) {}
~ImageFrame(){}
void Compress();
friend ostream& operator<<(ostream&, ImageFrame&);
};
Each one of them have a **friend ostream& operator<<(ostream&, ImageFrame&);**
But when i do this
ImageFrame test;
cout << test << endl;
Only Frame class's operator << is called. Any solution to this?
Also, professor doesn't want me to change anything from the header files!
EDIT:
here is how i implement the operator <<:
// code from AudioFrame.cpp
ostream& operator<<(ostream& os, AudioFrame& obj) {
os << "AudioFrame" << endl;
os << "Name = " << obj.GetFrameName() << endl;
for (unsigned int i = 0; i < obj.attributes.size(); i++) {
os << "\tBandwidth #" << i << ": " << obj.attributes[i] << endl;
}
return os;
}
// code from ImageFrame.cpp
ostream& operator<<(ostream& os, ImageFrame& obj) {
os << "ImageFrame" << endl;
os << "Name = " << obj.GetFrameName() << endl;
for (unsigned int i = 0; i < obj.attributes.size(); i++) {
os << "\tResolution #" << i << ": " << obj.attributes[i] << endl;
}
return os;
}
// code from Frame.cpp
ostream& operator<<(ostream& os, Frame& obj) {
return os;
}
But when i run the test code, only the code from Frame.cpp is run.
EDIT2:
So i feel like i should also share my real test code:
int type;
deque<Frame*> frames; // all frames are stored here
// user is promoted a console menu for selecting a frame type (ImageFrame or AudioFrame)
if (type == 1)
frames.push_back(new AudioFrame(...));
else
frames.push_back(new ImageFrame(...));
// now when i need to print all frames i do
for (unsigned int i = 0; i < frames.size(); i++)
cout << *(frames[i]) << endl;

If you REALLY want cout << *(frames[i]) << endl; to dispatch polymorphically, and you REALLY cannot alter the headers, then you have no choice but to rely on dynamic typing.
ostream& operator<<(ostream& stream, Frame& frame) {
AudioFrame* as_audio_frame = dynamic_cast<AudioFrame*>(&frame);
ImageFrame* as_image_frame = dynamic_cast<ImageFrame*>(&frame);
if(as_audio_frame) {
return stream << *as_audio_frame;
}
if(as_image_frame) {
return stream << *as_image_frame;
}
//normal frame code
}
However, this is TERRIBLE code, and signals that some reorganizing is severely needed in the headers. So i suspect there is something wrong in your intrepretation of your assignment.
Edit, well, actually, there is a hilariously overengineered alternative...
class FrameAdapterInterface {
public:
virtual ~FrameAdapterInterface() {};
protected:
virtual std::ostream print(std::ostream&) = 0;
friend ostream& operator<<(ostream&, FrameAdapterInterface &);
};
template<typename FRAME_T>
class AdapatedFrameType : public FrameAdapterInterface {
FRAME_T data_;
public:
template<typename... ARGS_T>
AdapatedFrameType(ARGS_T&&... args)
: data_(std::forward<ARGS_T>(args)...) {}
ostream& print(std::ostream& stream) override {
return stream << data_;
}
};
ostream& FrameAdapterInterface::operator<<(ostream& stream, FrameAdapterInterface& frame) {
return frame.print(stream);
}

Related

Overloading operator<< twice in same class for different member variables

Sorry if this question has been asked before, but I'm struggling with overloading the << operator to stream different data into multiple files.
I have a Player class, which has the following attributes:
char* name;
char* password;
int hScore;
int totalGames;
int totalScore;
int avgScore;
I want to overload the << operator twice: one to stream the name, password and hScore to a "Players.txt" file, and a second overload to stream the totalGames, totalScore and avgScore to a different .txt file which is based off each player's name, e.g. "Player1.txt".
Here's what my operator looks like in the Player class:
friend ostream& operator<< (std::ostream& os, Player& player)
{
os << player.name << "\n" << player.encryptPassword((player.password), 3) << "\n" << player.hScore << "\n";
return os;
}
And here's where I am calling it, from a PlayerLibrary class which contains a vector of Players:
ofstream out("Yahtzee.txt");
if (out.is_open())
{
for_each(playerList.begin(), playerList.end(), [&out](Player* player) {out << (*player);});
}
else
{
cout << "THERE WAS AN ERROR WRITING TO FILE\n";
}
Basically, I want to stream the other variables into another file which is named after the player name, and contains a scorecard for each game they've played. So far it looks like:
for (auto it = playerList.begin(); it != playerList.end(); ++it)
{
auto position = it - playerList.begin();
string filename(playerList[position]->getName());
filename = filename + ".txt";
ofstream out2(filename);
for (int i = 0; i < playerList[position]->getNumberOfScorecards(); i++)
{
out2 << *playerList[position]->getScorecard(i);
}
}
This only streams the scorecard and not the totalGames, totalScore and avgScore, like I want it to.
I have tried just moving those variables into the scorecard class, but I feel that it makes more sense to have them where they are.
I understand that I can't overload operator<< twice if both overloads have the same parameters, is there another way of going about this? Is there anyway perhaps in the overloaded function to use the output stream and check the name of the .txt file or something.
Hope the question makes sense.
Rather than defining an operator<< for Player itself, create a couple of utility types that refer to the Player and have their own operator<<s, let them decide which portions of the Player to stream, eg:
class Player
{
private:
std::string name;
std::string password;
int hScore;
int totalGames;
int totalScore;
int avgScore;
...
public:
...
std::string getName{} const { return name; }
...
std::string EncryptPassword(int arg) const { return ...; }
int getNumberOfScorecards() const { return ...; }
Scorecard* getScorecard(int index) const { return ...; }
class Info
{
const Player &m_player;
void print(std::ostream &os) const {
os << m_player.name << "\n" << m_player.encryptPassword(3) << "\n" << m_player.hScore << "\n";
}
public:
Info(const Player &player) : m_player(player) {}
friend std::ostream& operator<<(std::ostream &os, const Info &info)
{
info.print(os);
return os;
}
};
friend class Info;
struct Stats
{
const Player &m_player;
void print(std::ostream &os) const
{
os << m_player.totalGames << "\n" << m_player.totalScore << "\n" << m_player.avgScore << "\n";
}
public:
Stats(const Player &player) : m_player(player) {}
friend std::ostream& operator<<(std::ostream &os, const Stats &stats)
{
stats.print(os);
return os;
}
};
friend class Stats;
};
And then you can use them like this:
ofstream out("Yahtzee.txt");
if (out.is_open())
{
for(auto *player : playerList)
out << Player::Info(*player);
}
else
{
cout << "THERE WAS AN ERROR WRITING TO FILE\n";
}
for (auto *player : playerList)
{
ofstream out2(player->getName() + ".txt");
out2 << Player::Stats(*player);
for (int i = 0; i < player->getNumberOfScorecards(); ++i)
{
out2 << *player->getScorecard(i);
}
}
Online Demo

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

Reading from file writing to class member functions and printing on console in C++?

I have 2 classes, I make an object from the second class (I pass the filename as an argument). It should print the contents of the file which contains the following data in the following format:
A.Yordanov 1234567819
S.Todorov 3456789120
D.Lazarov 2569789054
P.Pavlov 4329549823
M.Kalinova 2367892343
B.Georgiev 659045324
I have overloaded the << operator but I when I try to cout << cty2 nothing appears on the console even though I have put the file in the same directory?
// OOP_Homework.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
fstream f;
class CPerson {
string name;
string egn;
public:
CPerson(string n, string e) {
name = n;
egn = e;
}
friend bool operator<(CPerson p1, CPerson p2) {
if (p1.egn < p2.egn) {
return true;
}
return false;
}
friend bool operator==(CPerson p1, CPerson p2) {
if (p1.egn == p2.egn) {
return true;
}
return false;
}
friend ostream& operator<< (ostream o, CPerson pn) {
o << "Egn" << endl;
o << pn.egn << endl;
o << "Name:" << endl;
o << pn.name << endl;
}
friend istream& operator>> (istream is, CPerson p) {
is >> p.egn;
is >> p.name;
return is;
}
string getName() {
return name;
}
string getEgn() {
return egn;
}
void setName(string n) {
name = n;
}
void setEgn(string e) {
egn = e;
}
};
class CCity {
vector<CPerson> pr;
public:
CCity(string fl) {
f.open(fl, ios::out);
string name = "", fn = "";
while (!f.eof()) {
//формата на данните във файла е във вида:
//<име на студент> <факултетен номер>
cin >> name >> fn;
pr.push_back(CPerson(name, fn));
}
f.close();
}
//помощен getter за вектора pr;
vector<CPerson> getV() {
return pr;
}
friend ostream& operator<<(ostream& os, CCity& psn) {
vector<CPerson> ps = psn.getV();
for (auto x = ps.begin(); x != ps.end(); x++) {
os << x->getName() << endl;
os << x->getEgn() << endl;
}
return os;
}
vector<CPerson> getDuplicate() {
set<CPerson> temp;
vector<CPerson> duplicates;
for (auto i = pr.begin(); i != pr.end(); i++) {
for (auto x : pr) {
if (*i == x) {
temp.insert(*i);
}
}
}
for (auto j : temp) {
duplicates.push_back(j);
}
temp.clear();
//връщаме студентите с повтарящи се егн-та
return duplicates;
}
void removeDuplicates() {
sort(pr.begin(),pr.end());
pr.erase(unique(pr.begin(), pr.end()));
}
void removeVector(vector<CPerson> ob) {
for (auto i:ob) {
for (auto x = pr.begin(); x != pr.end(); x++) {
if (i == *x) {
pr.erase(x);
}
}
}
}
};
int main()
{
CCity cty2("persons.txt");
//cout << cty1 << endl;
cout << cty2 << endl;
return 0;
}
The code for your overload for operator<< should be
friend ostream& operator<< (ostream &o, const CPerson &pn) {
// ^ ^^^^^ ^
o << "Egn" << endl;
o << pn.egn << endl;
o << "Name:" << endl;
o << pn.name << endl;
return o; // since return type is ostream&
}
You should be passing the stream by reference and returning it once you've finished using it. Passing by-reference ensures that the same stream is used (unlike pass-by-value).
Another thing to note would be your CPerson p function arguments. These should be passed by-reference as well. With operator<<, your CPerson won't be modified, so we can keep it as const.
You may also want to watch out your operator>>. Again, istream is should be istream &is. With the CPerson p parameter, you will modify it, so you'll definitely want to be passing it as a reference.
friend istream& operator>> (istream &is, CPerson &p) {
// ^ ^
is >> p.egn;
is >> p.name;
return is;
}
In your other overloads, you should be using const CPerson &p as well. Moreover, your overloads for operator< and operator== don't need to be friended since they're not accessing any other class's private members. Thus,
bool operator<(const CPerson &p1, const CPerson &p2) {
if (p1.egn < p2.egn) {
return true;
}
return false;
}
bool operator==(const CPerson &p1, const CPerson &p2) {
if (p1.egn == p2.egn) {
return true;
}
return false;
}
You'll want to get used to passing your classes by-reference as it saves space (not having to copy-construct another instance of the class as passing by-value would do). It's thus, more efficient both space-wise and performance-wise to pass your classes by reference.

debugging error at the end of the console

I'm a student and I studying c++.
This is my cpp code
int _tmain(int argc, _TCHAR* argv[])
{
CFood output;
output.whatFunc();
cout<<"my outputs"<<endl<<output<<endl;
return 0;
}
ostream& operator <<(ostream& outputStream, const CFood& output)
{
for(int i=0; i<2; i++)
{
outputStream <<"1 : "<<output.m_strName[i]<<" 2 : "<<output.m_servingSize[i]<<"g "<<"3 : "<<
output.m_calorie[i]<<"cal "<<"4 : "<<output.m_transFat[i]<<"g"<<endl;
}
return outputStream;
}
When I debug it, It work. But the end of the console, it gives me error message;;;
It says "An unhandled win32 exception occurred in work.exe [5796]"
My header filed is
class CFood
{
public:
CFood(void);
~CFood(void);
private:
string m_strName[7];
double m_servingSize[7];
double m_calorie[7];
double m_transFat[7];
public:
void whatFunc(void);
friend ostream& operator <<(ostream& outputStream,const CFood& output);
}
I think there is something wrong in my code..And I think it's CFood output;(Just thinking..)
Do you know why it has debug error?
++Sorry, I forgot the whatFunc(void)
This is code
void CFood::whatFunc(void) //
{
m_strName[0]="chicken";
m_strName[1]="rice";
m_strName[2]="meat";
m_strName[3]="strawberry";
m_strName[4]="apple";
m_strName[5]="water";
m_strName[6]="juice";
m_servingSize[0]=10;
m_servingSize[1]=20;
m_servingSize[2]=30;
m_servingSize[3]=40;
m_servingSize[4]=50;
m_servingSize[5]=60;
m_servingSize[6]=70;
m_calorie[0]=10.45;
m_calorie[1]=20.57;
m_calorie[2]=30.78;
m_calorie[3]=40.23;
m_calorie[4]=50.85;
m_calorie[5]=60.73;
m_calorie[6]=70.27;
m_transFat[0]=0.01;
m_transFat[1]=0.02;
m_transFat[2]=0.03;
m_transFat[3]=0.04;
m_transFat[4]=0.05;
m_transFat[5]=0.06;
m_transFat[6]=0.07;
}
Well its difficult to tell what exactly goes wrong without full source code. In my humble opinion, entire source lefts much to be desired. Placing obviously linked data in the bunch of the unlinked arrays with static size is not a very good pattern. Instead try something like that:
#include <iostream>
#include <vector>
#include <string>
#include <ostream>
struct CFoodItem{
std::string m_strName;
double m_servingSize;
double m_calorie;
double m_transFat;
};
class CFood
{
public:
void AddFoodItem(const CFoodItem& cItem);
friend std::ostream& operator <<(std::ostream& outputStream, const CFood& output);
private:
std::vector<CFoodItem> m_vItems;
};
std::ostream& operator <<(std::ostream& outputStream, const CFood& output)
{
for (auto i = output.m_vItems.begin(); i != output.m_vItems.end(); ++i)
{
outputStream << "1 : " << i->m_strName << " 2 : " << i->m_servingSize << "g " << "3 : " <<
i->m_calorie << "cal " << "4 : " << i->m_transFat << "g" << std::endl;
}
return (outputStream);
}
void CFood::AddFoodItem(const CFoodItem& cItem)
{
m_vItems.push_back(cItem);
}
int __cdecl main(void)
{
CFood output;
CFoodItem itm;
itm.m_strName = "some food";
itm.m_servingSize = 100500;
itm.m_calorie = 42;
itm.m_transFat = 42;
output.AddFoodItem(itm);
std::cout << "my outputs" << std::endl << output << std::endl;
return 0;
}

C++ Indenting output class inheriting ofstream

So I want to write an indent output class that can be used like this:
Debug f;
f.open("test.txt");
f << Debug::IndS << "Start" << std::endl;
f << Debug::Ind << "test" << std::endl;
f << Debug::IndE << "End" << std::endl;
which would output:
Start
test
End
So IndS would print out current indent and increment indent, Ind would print out the current indent and IndE would decrement indent and print out the current indent. I have tried to create it like so:
class Debug : public std::ofstream {
public:
Debug();
~Debug();
private:
std::string IndentText;
int _Indent;
public:
void SetIndentText(const char* Text);
inline void Indent(int Amount);
inline void SetIndent(int Amount);
inline std::ofstream& Ind (std::ofstream& ofs);
inline std::ofstream& IndS(std::ofstream& ofs);
inline std::ofstream& IndE(std::ofstream& ofs);
};
Debug::Debug () : std::ofstream() {
IndentText = " ";
}
Debug::~Debug () {
}
void Debug::SetIndentText (const char* Text) {
IndentText = Text;
}
void Debug::Indent (int Amount) {
_Indent += Amount;
}
void Debug::SetIndent(int Amount) {
_Indent = Amount;
}
std::ofstream& Debug::Ind (std::ofstream& ofs) {
for (int i = 0;i < _Indent;i++) {
ofs << IndentText;
}
return ofs;
}
std::ofstream& Debug::IndS (std::ofstream& ofs) {
ofs << Ind;
_Indent++;
return ofs;
}
std::ofstream& Debug::IndE (std::ofstream& ofs) {
_Indent--;
ofs << Ind;
return ofs;
}
So I think there are a few problems with this:
It does not compile. Errors with no match for 'operator<<' (operand types are 'std::ofstream {aka std::basic_ofstream<char>}' and '<unresolved overloaded function type>') ofs << Ind; candidates are: blah blah
I don't override all constructors. Is there a way to do this? I think I just have to rewrite all the constructors to do IndentText = " "; and delegate the overloaded constructor
Could someone help me with this? Thanks!
You usually shouldn't inherit std::ostream or implementations of it like std::ofstream. Wrap them into another class instead.
Here's a short sketch of my ideas mentioned in the comments
#include <iostream>
#include <fstream>
using namespace std;
class Logger {
public:
Logger(ostream& os) : os_(os), curIndentLevel_(0) {}
void increaseLevel() { ++curIndentLevel_; }
void decreaseLevel() { --curIndentLevel_; }
private:
template<typename T> friend ostream& operator<<(Logger&, T);
ostream& os_;
int curIndentLevel_;
};
template<typename T>
ostream& operator<<(Logger& log, T op) {
for(int i = 0; i < log.curIndentLevel_ * 4; ++i) {
log.os_ << ' ';
}
log.os_ << op;
return log.os_;
}
int main() {
Logger log(cout);
log.increaseLevel();
log << "Hello World!" << endl;
log.decreaseLevel();
log << "Hello World!" << endl;
return 0;
}
Output
Hello World!
Hello World!
Live Sample
Here's a little variant, showing how you can shortcut the coding with operator<<() overloads:
class Logger {
public:
Logger(ostream& os) : os_(os), curIndentLevel_(0) {}
Logger& increaseLevel() { ++curIndentLevel_; return *this; }
Logger& decreaseLevel() { --curIndentLevel_; return *this; }
// ... as before ...
};
int main() {
Logger log(cout);
log.increaseLevel() << "Hello World!" << endl;
log.decreaseLevel() << "Hello World!" << endl;
return 0;
}
Live Sample
The same way you can go to provide additional I/O manipulator style free functions.
Alternative solution:
#include <iostream>
#include <fstream>
class IndentClass {
public:
IndentClass();
~IndentClass();
private:
std::string IndentText;
int _Indent;
public:
inline void SetIndentText(const char* Text);
inline void Indent(int Amount);
inline void SetIndent(int Amount);
inline void ind (std::ostream& ofs);
class Ind_t {
public:
IndentClass& state;
Ind_t (IndentClass& _state) : state(_state) {}
friend inline std::ostream& operator<< (std::ostream& ofs, Ind_t& ind);
};
class IndS_t {
public:
IndentClass& state;
IndS_t (IndentClass& _state) : state(_state) {}
friend inline std::ostream& operator<< (std::ostream& ofs, IndS_t& ind);
};
class IndE_t {
public:
IndentClass& state;
IndE_t (IndentClass& _state) : state(_state) {}
friend inline std::ostream& operator<< (std::ostream& ofs, IndE_t& ind);
};
Ind_t Ind;
IndS_t IndS;
IndE_t IndE;
};
IndentClass::IndentClass () : IndentText(" "), _Indent(0), Ind(*this), IndS(*this), IndE(*this) {
}
IndentClass::~IndentClass () {
}
void IndentClass::SetIndentText (const char* Text) {
IndentText = Text;
}
void IndentClass::Indent (int Amount) {
_Indent += Amount;
}
void IndentClass::SetIndent(int Amount) {
_Indent = Amount;
}
void IndentClass::ind (std::ostream& ofs) {
for (int i = 0;i < _Indent;i++) {
ofs << IndentText;
}
}
std::ostream& operator<< (std::ostream& ofs, IndentClass::Ind_t& ind) {
ind.state.ind(ofs);
return ofs;
}
std::ostream& operator<< (std::ostream& ofs, IndentClass::IndS_t& inds) {
inds.state.ind(ofs);
inds.state.Indent(1);
return ofs;
}
std::ostream& operator<< (std::ostream& ofs, IndentClass::IndE_t& inde) {
inde.state.Indent(-1);
inde.state.ind(ofs);
return ofs;
}
int main () {
IndentClass i;
std::cout << i.IndS << "test" << std::endl;
std::cout << i.Ind << "test" << std::endl;
std::cout << i.IndE << "test" << std::endl;
return 0;
}
Ideone Example