Presentation is loading. Please wait.

Presentation is loading. Please wait.

Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:

Similar presentations


Presentation on theme: "Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:"— Presentation transcript:

1 Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab: Friends and Overloaded Operators)

2 Friends and Overloaded Operators1 Exercise 1 Write, compile, and run the following program: #include using namespace std; class AltMoney { public: AltMoney(); AltMoney(int d, int c); void add(const AltMoney& m1, const AltMoney& m2); void display_money(); private: int dollars; int cents; }; continue

3 2 Exercise 1 void read_money(int& d, int& c); int main( ) { int d, c; AltMoney m1, m2, sum; read_money(d, c); m1 = AltMoney(d,c); cout << "The first money is: "; m1.display_money(); read_money(d, c); m2 = AltMoney(d,c); cout << "The second money is: "; m2.display_money(); sum.add(m1,m2); cout << "The sum is: "; sum.display_money(); return 0; } continue Friends and Overloaded Operators

4 3 Exercise 1 AltMoney::AltMoney() { } AltMoney::AltMoney(int d, int c) { dollars = d; cents = c; } void AltMoney::add(const AltMoney& m1, const AltMoney& m2) { int extra = 0; cents = m1.cents + m2.cents; if(cents >=100) { cents = cents - 100; extra = 1; } dollars = m1.dollars + m2.dollars + extra; } continue Friends and Overloaded Operators

5 4 Exercise 1 void AltMoney::display_money() { cout << "$" << dollars << "." << cents << endl; } void read_money(int& d, int& c) { cout << "Enter dollar: "; cin >> d; cout << "Enter cents: "; cin >> c; if( d < 0 || c < 0) { cout << "Invalid dollars and cents, negative values\n"; exit(1); } Friends and Overloaded Operators

6 5 Exercise 2 Modify the previous program to include the following changes: Make read_money a member function. Note that if you make read_money a member function, then you can use it to directly initialize the dollars and cents of an AltMoney type object directly. Make function add a friend of class AltMoney. Thus, This function now computes the sum of dollars and cents and returns it as AltMoney. Friends and Overloaded Operators

7 6 Solution #include using namespace std; class AltMoney { public: AltMoney(); AltMoney(int d, int c); void read_money(); friend AltMoney add(const AltMoney& m1, const AltMoney& m2); void display_money( ); private: int dollars; int cents; }; continue Friends and Overloaded Operators

8 7 Solution int main( ) { AltMoney m1, m2, sum; m1.read_money(); cout << "The first money is: "; m1.display_money(); m2.read_money(); cout << "The second money is: "; m2.display_money(); sum = add(m1,m2); cout << "The sum is: "; sum.display_money(); return 0; } continue Friends and Overloaded Operators

9 8 Solution AltMoney::AltMoney() { } AltMoney::AltMoney(int d, int c) { dollars = d; cents = c; } void AltMoney::read_money() { cout << "Enter dollar: "; cin >> dollars; cout << "Enter cents: "; cin >> cents; if( dollars < 0 || cents < 0) { cout << "Invalid dollars and cents, negative values\n"; exit(1); } continue Friends and Overloaded Operators

10 9 Solution AltMoney add(const AltMoney& m1, const 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 AltMoney::display_money() { cout << "$" << dollars << "." << cents << endl; } Friends and Overloaded Operators

11 Exercise 3 Modify the previous program to include the following changes: Overload the + operator so that it does what the function add is doing. Overload the >> operator so that it does what the function read_money is doing. Overload the << operator so that it does what the function display_money is doing. 10Friends and Overloaded Operators

12 Solution #include using namespace std; class AltMoney { public: AltMoney(); AltMoney(int d, int c); friend istream& operator >>(istream& ins, AltMoney& m); friend AltMoney operator +(const AltMoney& m1, const AltMoney& m2); friend ostream& operator <<(ostream& outs, const AltMoney& m); private: int dollars; int cents; }; continue 11Friends and Overloaded Operators

13 Solution int main( ) { AltMoney m1, m2, sum; cin >> m1; cout << "The first money is: " << m1; cin >> m2; cout << "The second money is: " << m2; sum = m1 + m2; cout << "The sum is: " << sum; return 0; } AltMoney::AltMoney() { } 12 continue Friends and Overloaded Operators

14 Solution AltMoney::AltMoney(int d, int c) { dollars = d; cents = c; } istream& operator >>(istream& ins, AltMoney& m) { cout << "Enter dollar: "; ins >> m.dollars; cout << "Enter cents: "; ins >> m.cents; if( m.dollars < 0 || m.cents < 0) { cout << "Invalid dollars and cents, negative values\n"; exit(1); } return ins; } 13 continue Friends and Overloaded Operators

15 Solution AltMoney operator +(const AltMoney& m1, const 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; } ostream& operator <<(ostream& outs, const AltMoney& m) { outs << "$" << m.dollars << "." << m.cents << endl; return outs; } 14Friends and Overloaded Operators


Download ppt "Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:"

Similar presentations


Ads by Google