Question: Part 1: Open the new version of the program AddMoney.cpp (This program can be seen below) , in which we read the two input values
Part 1: Open the new version of the program AddMoney.cpp (This program can be seen below), in which we read the two input values from an input file, in_file.dat, and writes the results into an output file, out_file.dat.
Overload the >> so that writing an object of type AltMoney can be done using >>.
// lab6_AddMoney_n.cpp: This program adds money of two different people // It reads the amounts for two people from an // input file in_file.dat and writes the result into a file out_file.dat
#include
class AltMoney { public: AltMoney(); friend void read_money(istream& ins, AltMoney& m); friend AltMoney operator +(AltMoney m1, AltMoney m2); friend void write_money(ofstream& ous, AltMoney m); private: int dollars; int cents; };
void read_money(istream& ins, AltMoney& m); void get_streams(ifstream& ins, ofstream& ous); void write_money(ofstream& ous, AltMoney m);
int main( ) { ifstream ins; ofstream ous; AltMoney m1, m2, sum;
get_streams(ins, ous);
read_money(ins, m1); ous << "The first money is:"; write_money(ous, m1);
read_money(ins, m2); ous << "The second money is:"; write_money(ous, m2);
sum = m1+m2; ous << "The sum is:"; write_money(ous, sum);
ins.close(); ous.close();
return 0; }
AltMoney::AltMoney() { }
void write_money(ofstream& ous, AltMoney m) { ous << "$" << m.dollars << "."; if(m.cents <= 9) ous << "0"; //to display a 0 on the left for numbers less than 10 ous << m.cents << endl; }
AltMoney operator +(AltMoney m1, AltMoney m2) { AltMoney temp; int extra = 0; temp.cents = m1.cents + m2.cents; if(temp.cents >=100){ temp.cents = temp.cents - 100; extra = 1; } temp.dollars = m1.dollars + m2.dollars + extra;
return temp; }
void read_money(istream& ins, AltMoney& m) { int d, c; ins >> d; ins >> c; if( d < 0 || c < 0) { cout << "Invalid dollars and cents, negative values "; exit(1); } m.dollars = d; m.cents = c; }
void get_streams(ifstream& ins, ofstream& ous) {
ins.open("in_file.dat"); if(ins.fail()) { cout << "Failed to open the input file. "; exit(1); }
ous.open("out_file.dat"); if(ous.fail()) { cout << "Failed to open the output file. "; exit(1); } }
Part 2: Set Class
Define a class for set elements. A set class is used to represent sets of integers. It supports most of the standard set operations. The program will demonstrate overloaded operators, member functions, friend functions and random number generation.
****************************************************************************************
Set data structure:
A set is a collection of objects need not to be in any particular order. Elements
should not be repeated.
Union: Combine two or more sets (here two sets)
Intersection: Gathering common elements in both the sets together as a single set
Difference: Forming a set with elements which are in first set and not in second set
A-B= {x| for all x belongs to A but not in B}
***************************************************************************************
Sample class declaration:
You class should have all of the followings.
Can be defined as friend functions
class Set {
public:
//default constructor
Set();
//add element to set
void addElement (int element);
//remove element from set
void removeElement(int element);
//check for membership
bool isMember(int element);
//set union, modifies curremtn set
void Union (Set s);
//set difference modifiers current set
void difference (Set s);
//size of set
int size();
//get element i
int getElement (int i);
private:
//binary search for element, returns index
bool search(int element, int& index);
//set members
int elements[maxElements];
//next empty position in elements
int next;
};
Overload the following operators the class Set:
A suitable overloading of the quality operator ==
A suitable overloading of the inquality operator !=
A suitable overloading of the add element operator +
A suitable overloading of the set union operator +
A suitable overloading of the remove operator -
A suitable overloading of the set difference operator -
Hints: some regular functions might helpful.
void printSet(Set s);
void generateSample(int sample[], int n);
Generate a set of value of a certain size. Produce an array that is used to initialize the sets in our experiment.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
