Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Lab help In C++, we can overload the add operator, ''+, to do different types of addition, for example: an the addition of money

C++ Lab help

In C++, we can overload the add operator, ''+", to do different types of addition, for example: an the addition of money in the previous programs (dollars + dollars and cents + cents). By using function overloading, we will give the power of function add to operator "+", which is also defined in the class definition.

let's modify our program and get new version of addMonday. In this program the add should be defined as a friend and has the type AltMoney as well.

Now, let's overload the "+" operator so that is does what the function add is doing.

By making these changes, you can now add two objects of type AltMoney. Note that you may overload "+" in another class definition. Depending on how you have defined it, "+" operation may have different meanings in such cases.

Overloading Operators >> and <<

The input and output operators >> and << can be overloaded like other operators. In such a case, the value returned must be the stream and the type for the value returned must have the & symbol added to the end of the type name. Here is the declaration:

declaration: class Class_Name { public: ... friend istream& operator >>(istream& parameter_1, Class_Name& parameter_2); friend ostream& operator <<(ostream& parameter_3, const Class_Name& parameter_4); ...

definition: istream& operator >>(istream& parameter_1, Class_Name& parameter_2) { ... }

ostream& operator <<(istream& parameter_3, Class_Name& parameter_4) { ... }

Part 1: Open the new version of the program AddMoney.cpp, 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 #include #include using namespace std;

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

3rd Edition

978-1119907466

More Books

Students also viewed these Databases questions