Question: In this lab you will create the implementation for a class called Transaction, which will represent a bank transaction. I will supply the header file;
In this lab you will create the implementation for a class called Transaction, which will represent a bank transaction. I will supply the header file; you only have to fill in the details in the Transaction.cpp file.
A Transaction has an account number and an amount, along with simple accessors for both those items. (The accessors in the supplied Transaction.h have a subtle problem you'll need to fix.)
We'll need both a default constructor and one which takes the account number and amount. Both constructors should initial everything in the constructor initializer list; neither should have any code in the body of the constructor.
We'll also need to define several operators for the class:
operator+= should be implemented as a member function. It should make sure the account number for the Transaction being added is the same as the one for the Transaction object being added to, and should throw a std::logic_error exception if not.
operator==`` should returntrueif the twoTransactions have the same account number, andfalse``` otherwise.
operator+ should take two transactions and return a Transaction whose amount is the sum of the two given transactions. This should also throw a std::logic_error exception if the account numbers do not match.
operator< should return true if the account number of the first argument is less than the account number for the second argument, and false otherwise. (Note that std::string has operator< defined to do exactly that.)
operator>> should read a std::string and a double from the given input stream, create a Transaction from those, and assign it to the Transaction object passed in as a function argument.
operator<< should write the given Transaction to the given output stream. The account number should be written first, followed by a space, followed by the amount. The amount must be written with two decimal places - you may want to review a previous assignment of you have forgotten how to do that.
For operator<< and operator>>, don't forget to return the given input or output stream.
Transaction.h
#pragma once
#include
#include
class Transaction
{
private:
std::string account;
double amount;
public:
// Create a transaction using the given account number and amount.
Transaction(const std::string & ac, double am);
// Create a transaction with a blank account number and an amount of 0.0.
Transaction();
// Return the acount number.
std::string get_account();
// Return the transaction amount.
double get_amount();
// Add the two transactions. Throw a logic_error if the account
// numbers are not the same
Transaction & operator+=(const Transaction & rhs);
};
// Read a transaction from an input stream.
std::istream & operator>>(std::istream & is, Transaction & trans);
// Write a transaction to an output stream.
std::ostream & operator<<(std::ostream & os, const Transaction & trans);
// Two transactions are equal if they have the same account number.
bool operator==(const Transaction & lhs, const Transaction & rhs);
// The < operator compares the account numbers, not the amounts.
bool operator<(const Transaction & lhs, const Transaction & rhs);
// Add two transactions. Throw a logic_error if the account numbers
// are not the same.
Transaction operator+(Transaction lhs, const Transaction & rhs);
Transaction.cpp
#include
#include "Transaction.h"
/***** member functions *****/
Transaction::Transaction(const std::string & ac, double am)
: /* initialize member variables here */
{
}
Transaction::Transaction()
: /* initialize member variables here */
{
}
/* accessors - Transaction::get_account() and Transaction::get_amount() */
/***** non-member functions *****/
/* Note that operator+= is a member function,
and is therefore called Transaction::operator+= */
Transaction & Transaction::operator+=(const Transaction & rhs)
{
/* your code here */
/* compare account numbers - if not the same, throw a logic_error */
}
std::istream & operator>>(std::istream & is, Transaction & trans)
{
/* I'll give you this one */
std::string account;
double amount;
is >> account >> amount;
trans = Transaction(account, amount);
return is;
}
/* operator<< - you're entirely on your own for this one */
/* operator==, operator<, operator+ */
bool operator==(const Transaction & lhs, const Transaction & rhs)
{
/* your code here - just compare the account numbers */
}
bool operator<(const Transaction & lhs, const Transaction & rhs)
{
/* your code here - just compare the account numbers */
}
/* operator+ - you need to provide everything for this one */
/* see page 219 of the text for a clue as to how to implement
this by using operator+= */
TestTransaction.cpp
#include
#include
#include
#include
#include "Transaction.h"
using namespace std;
template
void print_vector(const vector
{
for (const auto t : v)
std::cout << t << ' ';
}
void test1()
{
std::cout << "test1() ";
// This function tests the constructor and accessors.
vector
{
{ "S001", 10 },
{ "S002", 20 },
{ "S003", 30 },
{ "S001", 20 },
{ "S001", 30 },
{ "S002", 40 },
{ "S003", 30 },
{ "S002", 20 }
};
// This will NOT print out the way that operator<< does;
// it will not show the proper number of decimal places.
for (auto trans : tlist)
std::cout << trans.get_account() << ' ' << trans.get_amount() << ' ';
}
vector
{
std::cout << "test2() ";
// This function tests the input and output operators.
// It returns the vector of Transaction objects read
// from stdin; test3() will use this.
vector
Transaction trans;
while (cin >> trans)
{
tlist.push_back(trans);
}
std::copy(begin(tlist), end(tlist), std::ostream_iterator
return tlist;
}
void test3(vector
{
std::cout << "test3() ";
// This function tests operator<.
std::sort(begin(tlist), end(tlist));
std::copy(begin(tlist), end(tlist), std::ostream_iterator
}
void test4()
{
std::cout << "test4() ";
// This function tests the +=, ==, and + operators.
std::cout << Transaction(Transaction("a", 15) + Transaction("a", 25)) << ' ';
Transaction t("c", 20);
t += Transaction("c", 30);
std::cout << t << ' ';
Transaction t1("x", 10);
Transaction t2("x", 20);
Transaction t3("y", 30);
bool exception_expected = false;
try
{
std::cout << std::boolalpha;
std::cout << (t1 == t2) << ' ' << (t1 == t3) << ' ';
std::cout << (t1 + t2) << ' ';
exception_expected = true;
std::cout << (t1 + t3) << ' ';
std::cout << "Failure: not exception thrown when adding Transactions for different accounts ";
}
catch (std::logic_error & e)
{
if (!exception_expected)
std::cout << "Failure: unexpected logic_error thrown ";
}
catch (...)
{
std::cout << "Failure: unexpected exception type thrown ";
}
}
int main()
{
test1();
vector
test3(tlist);
test4();
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
