Presentation is loading. Please wait.

Presentation is loading. Please wait.

Working with Classes. A method dealing with two objects C++ has a pointer which call this. It gives the address of an object. Suppose we have two objects.

Similar presentations


Presentation on theme: "Working with Classes. A method dealing with two objects C++ has a pointer which call this. It gives the address of an object. Suppose we have two objects."— Presentation transcript:

1 Working with Classes

2 A method dealing with two objects C++ has a pointer which call this. It gives the address of an object. Suppose we have two objects of BankAccount class and we want to know which one has more balance.

3 Solution : Have a method that returns a reference to the object with larger balance.

4 Header file for BankAccount class #ifndef BANK1_H #define BANK1_H class BankAccount { private : double balance; string name; public : BankAccount(); BankAccount(double initial_balance); void deposit (double amount); void withdraw (double amount ); double getBalance(); const BankAccount & topval( const BankAccount & s) const; ~BankAccount(); }; #endif

5 CPP file #include #include “bank1.h” BankAccount :: BankAccount() { balance = 0; } BankAccount :: BankAccount(double initialBalance, string str) { balance = initialBalance; name=str; }

6 void BankAccount ::deposit(double amount { balance =balance+amount ; } void BankAccount::withdraw(double amount) { if ( amount > balance ) amount=0; else balance=balance-amount; }

7 double BankAccount :: getBalance() { return balance; } BankAccount :: ~BankAccount() { cout<<‘good bye”; }

8 const BankAccount & BankAccount :: topval( const BankAccount & s) const { if (s.balance > balance) return s; else return *this; } /* const in parenthesis says that the function won’t modify the explicitly accessed object */

9 /* the const after parenthesis says that the function won’t modify the implicitly accessed objects*/ The function returns a reference to one of the two const objects has to be constant. top=account1.topval(account2);

10 Operator Overloading Like function overloading we can have operator overloading. We have already used operator overloading we use * for multiplication and * for pointer. C++ allows user to overload operators.

11 Suppose we want to overload “+” to add two arrays. for(int i=0; i<20;i++) Array3[i]=Array1[i]+Array2[i]; // add element by element We can define a class that overload + so we can have Array3=Array1+Array2;

12 To overload an operator we use a special function call. operatorop(arguments-list); here op is a operator known for C++. For example: operator+(); // overload + operator@(); // error @ is not a C++ operator

13 #ifndef MYTIME0_H_ #define MYTIME0_H_ class Time { private: int hours; int minutes; public: Time(); Time(int h, int m); void AddMin(int m); void AddHr(int h); void Reset(int h, int m); Time Sum (const Time &t) const ; void Show(); }; #endif

14 #include #include “mytime0.h”; Time :: Time() { hours=minutes=0; } Time::Time(int h, int m) { hours=h; minutes=m; }

15 void Time::AddMin(int m) { minutes+=m; hours+=minuets/60; minuets %=60; } void Time::AddHr(int h) { hours+=h; }

16 void Time::Reset(int h, int m) { hours=h; minutes=m; } Time Time::Sum(const Time &t) const { Time sum; sum.minutes=minuets+t.minutes; sum.hours=hours+t.hours+sum.minuets/60; sum.minuets %=60; return sum; }

17 void Time:: Show() { cout<<hours<<“hours,” <<minutes<<“ minuets”; }

18 #include #include “mytime0.h”; int main() { Time coding(2,40); Time fixing(5,55); Time total; total=coding.Sum(fixing); total.Show(); return 0; }

19 #ifndef MYTIME0_H_ #define MYTIME0_H_ class Time { private: int hours; int minutes; public: Time(); Time(int h, int m); void AddMin(int m); void AddHr(int h); void Reset(int h, int m); Time operator+(const Time &t) const; Time operator *(double mult) const; void Show(); }; #endif

20 #include #include “mytime0.h”; Time :: Time() { hours=minutes=0; } Time::Time(int h, int m) { hours=h; minutes=m; }

21 void Time::AddMin(int m) { minutes+=m; hours+=minuets/60; minuets %=60; } void Time::AddHr(int h) { hours+=h; }

22 void Time::Rese(int h, int m) { hours=h; minutes=m; } Time Time::operator+(const Time &t) const { Time sum; sum.minutes=minuets+t.minutes; sum.hours=hours+t.hours+sum.minuets/60; sum.minuets %=60; return sum; }

23 Time Time::operator*(double mult) const { Time result; long totalmin=hours*mult*60+minuets*mult; result.hours=totalmin/60; result.minuets =totalmin % 60; return result; } void Time:: Show() { cout<<hours<<“hours,” <<minutes<<“ minuets”; }

24 #include #include “mytime0.h”; int main() { Time coding(2,40); Time fixing(5,55); Time total; total=coding+fixing; total.Show(); Time morefixing(3,28); total=morefixing.operator+(total); total.Show(); total=morefixing* 2.5; total.Show(); return 0; }

25 Time t1,t2,t3,t4; t4=t1+t2+t3; t4=t1.operator+(t2+t3); t4=t1. operator+(t2.operator+(t3));

26 Overloading Restriction Should preserve the syntax for the original operator. Time test, %test /// invalid We can’t create new operator symbols like operator **(); We can’t overload the following operators : sizeof. :: ?:

27 Operators can be overloaded + - * / % ^ > += -= ! = %= ^= &= |= << && -- () [ ] new -> delete

28 = Assignment () Function call [ ] Subscripting -> class member access by pointer Can be overloaded only by member function.

29 Friend Function When a function f is a friend of a class, f has the same access to class as a function member has. We could write : Time A,B; A=B*2.5 ; // A=B.operator*(2.5); But not A=2.5*B;

30 Creating a Friend function friend Time operator*( double m, const Time &t); Place it in the class declaration. Time operator*(double m, const Time &t) { Time result; long totalmin=t.hours*m*60+t.minuets*m; result.hours=totalmin/60; result.minuets =totalmin % 60; return result; }

31 A=2.5*B; Is A=operator*(2.5,B); Time operator*(double m, const Time &t) { return t*m; }

32 Overload << We want to have Time t; cout<<t; instead of t.Show(); both cout and t are objects. If we want to overload << with a member function then we should write t<<cout; //odd

33 Add friend Time operator<<(ostream &os,const Time &t); Then the body : void operator<<(ostream &os,const Time &t) { os<<t.hours<<“ hours, “<<t.minutes<<“minutes”; }

34 Can we write cout<< “ time “ << t<< “ Tuesday \n”; ?? No “ Note that cout<<x; return an object cout of class ostrem”; int x,y; cout<<x<<y; we have (cout<<x) <<y;

35 ostream & operator<<(ostream &os,const Time &t) { os<<t.hours<<“hours,“<<t.minutes<<“minutes”; return os; }


Download ppt "Working with Classes. A method dealing with two objects C++ has a pointer which call this. It gives the address of an object. Suppose we have two objects."

Similar presentations


Ads by Google