In: Computer Science
How do I add an output operator to a class in C++?
The specific part of the task said to:
Define a class to hold accounts. Use the same stream variable! Write getters to access the fields in the accounts when printing. Add an output operator for your class. First, Repeat the print loop using this output operator.Now make the output operator a friend, by adding a friendprototype to your account class definition. Add a line in the output operator that prints the fields “directly”, i.e. without using the getters. The vector push_back method will be passed as temporary object defined as the argument to push_back. Repeat the filling of the vector using emplace_back and again display the contents.
Under my "class AccountClass":
I was supposed to add an "output operator". Based on the instructions above, how do you do this?
Code:
#include
#include
#include
#include
#include
#include
using namespace std;
struct Account
{
string name;
int accno;
};
class Transaction
{
private:
bool isDeposit;
int amount;
//Balance need to be static to maintain the state
across the transactions
static int balance;
public:
void deposit(int amt)
{
amount = amt;
balance += amount;
isDeposit = true;
}
void withdraw(int amt)
{
amount = amt;
balance -= amount;
isDeposit = false;
}
};
class AccountClass
{
private:
string name;
int accno;
vector vHistory;
public:
AccountClass(string nm, int acno) :name(nm),
accno(acno)
{
}
string getName()
{
return name;
}
int getaccno()
{
return accno;
}
void addTransaction(Transaction t)
{
vHistory.push_back(t);
}
};
void readandDisplayUsingStruct(string fileName=
"accounts.txt")
{
ifstream fin;
fin.open(fileName);
if (!fin.is_open())
return;
string line;
vector vAccounts;
//Read each line from the file
while (getline(fin, line))
{
stringstream ss(line);
string name;
int no;
//Split the line into name and id
using string stream
ss >> name;
ss >> no;
Account acc{ name,no };
vAccounts.push_back(acc);
}
cout << "The Read Values are" << endl;
for (Account acc : vAccounts)
{
cout << "Name : " <<
acc.name << "\n";
cout << "No : " <<
acc.accno << "\n";
}
//Clear the vector
vAccounts.clear();
fin.close();
//Again do the same operation using local variables
without Braces Initialisation.
fin.open(fileName);
if (!fin.is_open())
return;
string line1;
vector vAccounts2;
//Read each line from the file
while (getline(fin, line1))
{
stringstream ss(line1);
string name;
int no;
//Split the line into name and id
using string stream
ss >> name;
ss >> no;
Account acc;
//Explicit initialisation
acc.name = name;
acc.accno = no;
vAccounts2.push_back(acc);
}
std::cout << "The Read Values are" << endl;
for (Account acc : vAccounts)
{
std::cout << "Name : "
<< acc.name << "\n";
std::cout << "No : " <<
acc.accno << "\n";
}
fin.close();
}
void readandDisplayUsingClass(string fileName)
{
ifstream fin;
fin.open(fileName);
if (!fin.is_open())
return;
string line;
vector vAccounts;
//Read each line from the file
while (getline(fin, line))
{
stringstream ss(line);
string name;
int no;
//Split the line into name and id
using string stream
ss >> name;
ss >> no;
AccountClass acc(name, no);
vAccounts.push_back(acc);
}
std::cout << "The Read Values are" << endl;
for (AccountClass acc : vAccounts)
{
std::cout << "Name : "
<< acc.getName() << "\n";
std::cout << "No : " <<
acc.getaccno() << "\n";
}
//Clear the vector
vAccounts.clear();
fin.close();
}
int main()
{
ifstream fin;
fin.open("transactions.txt");
if (!fin.is_open())
return 0;
string line;
vector vAccounts;
//Read each line from the file
while (getline(fin, line))
{
stringstream ss(line);
string operation;
if (operation == "Deposit" ||
operation == "Withdraw")
{
int accno;
ss >>
accno;
auto itr =
std::find_if(vAccounts.begin(), vAccounts.end(),
[&](AccountClass anAccount)
{
return anAccount.getaccno()
== accno;
});
if (itr !=
vAccounts.end())
{
Transaction t;
int amount;
ss >> amount;
if (operation == "Deposit")
{
t.deposit(amount);
}
else
{
t.withdraw(amount);
}
itr->addTransaction(t);
}
}
else if (operation ==
"Account")
{
string
accName;
ss >>
accName;
auto itr =
std::find_if(vAccounts.begin(), vAccounts.end(),
[&](AccountClass anAccount)
{
return anAccount.getName() ==
accName;
});
if (itr ==
vAccounts.end())
{
int accno;
ss >> accno;
AccountClass newAccount(accName, accno);
vAccounts.push_back(newAccount);
}
}
}
}
// Output operator has been added to class AccountClass (bolded)
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <sstream>
using namespace std;
struct Account
{
string name;
int accno;
};
class Transaction
{
private:
bool isDeposit;
int amount;
//Balance need to be static to maintain the state across the transactions
static int balance;
public:
void deposit(int amt)
{
amount = amt;
balance += amount;
isDeposit = true;
}
void withdraw(int amt)
{
amount = amt;
balance -= amount;
isDeposit = false;
}
};
int Transaction::balance = 0; // initialize balance to be 0 at the start
class AccountClass
{
private:
string name;
int accno;
vector<Transaction> vHistory;
public:
AccountClass(string nm, int acno) :name(nm), accno(acno)
{
}
string getName()
{
return name;
}
int getaccno()
{
return accno;
}
void addTransaction(Transaction t)
{
vHistory.push_back(t);
}
// output operator declaration
friend ostream& operator<<(ostream &out, const AccountClass &account);
};
// output operator overloading to print the name and account no of the account
ostream& operator<<(ostream &out, const AccountClass &account)
{
out << "Name : " << account.name << "\n";
out << "No : " << account.accno << "\n";
return out;
}
void readandDisplayUsingStruct(string fileName= "accounts.txt")
{
ifstream fin;
fin.open(fileName);
if (!fin.is_open())
return;
string line;
vector<Account> vAccounts;
//Read each line from the file
while (getline(fin, line))
{
stringstream ss(line);
string name;
int no;
//Split the line into name and id using string stream
ss >> name;
ss >> no;
Account acc{ name,no };
vAccounts.push_back(acc);
}
cout << "The Read Values are" << endl;
for (Account acc : vAccounts)
{
cout << "Name : " << acc.name << "\n";
cout << "No : " << acc.accno << "\n";
}
//Clear the vector
vAccounts.clear();
fin.close();
//Again do the same operation using local variables without Braces Initialisation.
fin.open(fileName);
if (!fin.is_open())
return;
string line1;
vector<Account> vAccounts2;
//Read each line from the file
while (getline(fin, line1))
{
stringstream ss(line1);
string name;
int no;
//Split the line into name and id using string stream
ss >> name;
ss >> no;
Account acc;
//Explicit initialisation
acc.name = name;
acc.accno = no;
vAccounts2.push_back(acc);
}
std::cout << "The Read Values are" << endl;
for (Account acc : vAccounts)
{
std::cout << "Name : " << acc.name << "\n";
std::cout << "No : " << acc.accno << "\n";
}
fin.close();
}
void readandDisplayUsingClass(string fileName)
{
ifstream fin;
fin.open(fileName);
if (!fin.is_open())
return;
string line;
vector<AccountClass> vAccounts;
//Read each line from the file
while (getline(fin, line))
{
stringstream ss(line);
string name;
int no;
//Split the line into name and id using string stream
ss >> name;
ss >> no;
AccountClass acc(name, no);
vAccounts.push_back(acc);
}
std::cout << "The Read Values are" << endl;
for (AccountClass acc : vAccounts)
{
std::cout<<acc; // the operator<< class is used to output the name and accno
/*std::cout << "Name : " << acc.getName() << "\n";
std::cout << "No : " << acc.getaccno() << "\n";*/
}
//Clear the vector
vAccounts.clear();
fin.close();
}
int main()
{
ifstream fin;
fin.open("transactions.txt");
if (!fin.is_open())
return 0;
string line;
vector<AccountClass> vAccounts;
//Read each line from the file
while (getline(fin, line))
{
stringstream ss(line);
string operation;
if (operation == "Deposit" || operation == "Withdraw")
{
int accno;
ss >> accno;
auto itr = std::find_if(vAccounts.begin(), vAccounts.end(), [&](AccountClass anAccount)
{
return anAccount.getaccno() == accno;
});
if (itr != vAccounts.end())
{
Transaction t;
int amount;
ss >> amount;
if (operation == "Deposit")
{
t.deposit(amount);
}
else
{
t.withdraw(amount);
}
itr->addTransaction(t);
}
}
else if (operation == "Account")
{
string accName;
ss >> accName;
auto itr = std::find_if(vAccounts.begin(), vAccounts.end(), [&](AccountClass anAccount)
{
return anAccount.getName() == accName;
});
if (itr == vAccounts.end())
{
int accno;
ss >> accno;
AccountClass newAccount(accName, accno);
vAccounts.push_back(newAccount);
}
}
}
return 0;
}