Working with Classes. A method dealing with two objects C++ has a pointer which call this. It gives...

35
Working with Classes
  • date post

    18-Dec-2015
  • Category

    Documents

  • view

    212
  • download

    0

Transcript of Working with Classes. A method dealing with two objects C++ has a pointer which call this. It gives...

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 of BankAccount class and we want to know which one has more balance.

Solution :

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

Header file for BankAccount class#ifndef BANK1_H#define BANK1_Hclass 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

CPP file

#include<iostream.h>#include “bank1.h”BankAccount :: BankAccount()

{ balance = 0;}

BankAccount :: BankAccount(double initialBalance, string

str){ balance = initialBalance;

name=str;}

void BankAccount ::deposit(double amount{

    balance =balance+amount ;}

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

double BankAccount :: getBalance()

{

return balance;

}

BankAccount :: ~BankAccount()

{ cout<<‘good bye”;

}

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 */

/* 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);

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.

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;

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

#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

#include<iostream>

#include “mytime0.h”;

Time :: Time()

{

hours=minutes=0;

}

Time::Time(int h, int m)

{

hours=h; minutes=m;

}

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

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; }

void Time:: Show()

{

cout<<hours<<“hours ,” <<minutes<<“ minuets”;

}

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

#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

#include<iostream>

#include “mytime0.h”;

Time :: Time()

{

hours=minutes=0;

}

Time::Time(int h, int m)

{

hours=h; minutes=m;

}

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

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; }

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”;}

#include<iostream>#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;}

Time t1,t2,t3,t4;

t4=t1+t2+t3;

t4=t1.operator+(t2+t3);

t4=t1. operator+(t2.operator+(t3));

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 . :: ?:

Operators can be overloaded

+ - * / % ^

> += -= ! = %=

^= &= |= << && --

() [ ] new -> delete

= Assignment

() Function call

[ ] Subscripting

-> class member access by pointer

Can be overloaded only by member function.

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;

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; }

A=2.5*B;

Is

A=operator*(2.5,B);

Time operator*(double m, const Time &t)

{

return t*m;

}

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

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”;

}

• 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;

ostream & operator<<(ostream &os ,const Time &t)

{ os<<t.hours<<“hours,“<<t.minutes<<“minutes”;

return os;

}