Working with Classes - PowerPoint PPT Presentation

About This Presentation
Title:

Working with Classes

Description:

int hours; int minutes; public: Time(); Time(int h, int m); void ... cout hours 'hours ,' minutes ' minutes'; #include iostream #include 'mytime0.h' ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 45
Provided by: Ara8
Category:

less

Transcript and Presenter's Notes

Title: Working with Classes


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
  • includeltiostream.hgt
  • include bank1.h
  • BankAccount BankAccount() balance 0
  • BankAccount BankAccount(double initialBalance,
    string str) balance initialBalance
  • namestr

6
  • void BankAccount deposit(double amount
  •     balance balanceamount
  • void BankAccountwithdraw(double amount)
  • if ( amount gt balance )
  • amount0
  • else
  • balancebalance-amount

7
  • double BankAccount getBalance()
  • return balance
  • BankAccount BankAccount()
  • coutltltgood bye

8
  • const BankAccount BankAccount topval( const
    BankAccount s) const
  • if (s.balance gt balance)
  • return s
  • else
  • return this
  • / const in parenthesis says that the function
    wont modify the explicitly accessed object /

9
  • / the const after parenthesis says that the
    function wont modify the implicitly accessed
    objects/
  • The function returns a reference to one of the
    two const objects has to be constant.
  • topaccount1.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 i0 ilt20i)
  • Array3iArray1iArray2i
  • // add element by element
  • We can define a class that overload so we can
    have
  • Array3Array1Array2

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_at_()
  • // error _at_ 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
  • includeltiostreamgt
  • include mytime0.h
  • Time Time()
  • hoursminutes0
  • TimeTime(int h, int m)
  • hoursh minutesm

15
  • void TimeAddMin(int m)
  • minutesm
  • hoursminutes/60
  • minutes 60
  • void TimeAddHr(int h)
  • hoursh

16
  • void TimeReset(int h, int m)
  • hoursh
  • minutesm
  • Time TimeSum(const Time t) const
  • Time sum
  • sum.minutesminutest.minutes
  • sum.hourshourst.hourssum.minutes/60
  • sum.minutes 60
  • return sum

17
  • void Time Show()
  • coutltlthoursltlthours , ltltminutesltlt minutes

18
  • includeltiostreamgt
  • include mytime0.h
  • int main()
  • Time coding(2,40)
  • Time fixing(5,55)
  • Time total
  • totalcoding.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
  • includeltiostreamgt
  • include mytime0.h
  • Time Time()
  • hoursminutes0
  • TimeTime(int h, int m)
  • hoursh minutesm

21
  • void TimeAddMin(int m)
  • minutesm
  • hoursminutes/60
  • minutes 60
  • void TimeAddHr(int h)
  • hoursh

22
  • void TimeRese(int h, int m)
  • hoursh
  • minutesm
  • Time Timeoperator(const Time t) const
  • Time sum
  • sum.minutesminutest.minutes
  • sum.hourshourst.hourssum.minutes/60
  • sum.minutes 60
  • return sum

23
  • Time Timeoperator(double mult) const
  • Time result
  • long totalminhoursmult60minutesmult
  • result.hourstotalmin/60
  • result.minutes totalmin 60
  • return result
  • void Time Show()
  • coutltlthoursltlthours , ltltminutesltlt minutes

24
  • includeltiostreamgt
  • include mytime0.h
  • int main()
  • Time coding(2,40)
  • Time fixing(5,55)
  • Time total
  • totalcodingfixing
  • total.Show()
  • Time morefixing(3,28)
  • totalmorefixing.operator(total)
  • total.Show()
  • totalmorefixing 2.5
  • total.Show()
  • return 0

25
  • Time t1,t2,t3,t4
  • t4t1t2t3
  • t4t1.operator(t2t3)
  • t4t1. operator(t2.operator(t3))

26
Overloading Restriction
  • Should preserve the syntax for the original
    operator.
  • Time test,
  • test /// invalid
  • We cant create new operator symbols like
  • operator ()
  • We cant overload the following operators
  • sizeof . ?

27
Operators can be overloaded
  • - /
  • gt - !
  • ltlt --
  • () new -gt delete

28
  • Assignment
  • () Function call
  • Subscripting
  • -gt 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
  • AB2.5 // AB.operator(2.5)
  • But not
  • A2.5B

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 totalmint.hoursm60t.minutesm
  • result.hourstotalmin/60
  • result.minutes totalmin 60
  • return result

31
  • A2.5B
  • Is
  • Aoperator(2.5,B)
  • Time operator(double m, const Time t)
  • return tm

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

33
  • Add
  • friend void operatorltlt(ostream os ,const Time
    t)
  • Then the body
  • void operatorltlt(ostream os ,const Time t)
  • osltltt.hoursltlt hours, ltltt.minutesltltminutes

34
  • Can we write
  • coutltlt time ltlt tltlt Tuesday \n ??
  • No
  • Note that coutltltx return an object cout of
    class ostream
  • int x,y
  • coutltltxltlty
  • we have
  • (coutltltx) ltlty

35
  • ostream operatorltlt(ostream os ,const Time t)
  • osltltt.hoursltlthours,ltltt.minutesltltminutes
  • return os

36
Type Casting for Classes
  • When we write
  • long count8
  • double time11
  • we have conversion automatically.

37
  • ifndef STONEWT_H_
  • define STONEWT_H_
  • class Stonewt
  • private
  • enumlbs_per_stn14
  • int stone
  • double pds_left
  • double pounds
  • public
  • Stonewt(double lbs)
  • Stonewt(int stn, doub lbs)
  • Stonewt()
  • Stonewt()
  • void show_lbs()
  • void show_stn()
  • endif

38
  • includeltiostreamgt
  • include stonewt.h
  • StonwtStonewt(double lbs)
  • stoneint(lbs) / lbs_per_stn
  • pds_leftint(lbs)lbs_per_stnlbs-int(lbs)
  • poundslbs
  • StonewtStonewt(int stn, double lbs)
  • stonestn
  • pds_leftlbs
  • poundsstnlbs_per_stnlbs

39
  • StonewtStonewt()
  • stone0
  • poundpds_left0.0
  • void Stonewtshow_stn()
  • coutltltstoneltlt stone , ltltpds_leftltlt pounds\n
  • void Stonewtshow_lbs()
  • coutltltpoundsltlt pounds\n

40
  • now we can write
  • Stonewt mycat
  • mycat19.6
  • this conversion automatically happen and is
  • implicit conversion.
  • // it can lead to unexpected conversion
  • In order to make a conversion explicit we can
    write
  • explicit Stonewt(double lbs)

41
  • Stonewt mycat
  • mycat19.6 // is not valid
  • mycatStonewt(19.6)
  • Can we convert an object to double ?
  • Yes, but not using constructors. We can use
  • conversion function.

42
  • ifndef STONEWT_H_
  • define STONEWT_H_
  • class Stonewt
  • private
  • enumlbs_per_stn14
  • int stone
  • double pds_left
  • double pounds
  • public
  • Stonewt(double lbs)
  • Stonewt(int stn, double lbs)
  • Stonewt() Stonewt()
  • void show_lbs()
  • void show_stn()
  • operator int() const
  • operator double() const

43
  • Stonewtoperator int() const
  • return int (pounds0.5)
  • Stonewtoperator double() const
  • return pounds

44
  • We can write
  • Stonewt poppins(9,2.8)
  • double p_wt poppins //implicit
  • coutltltp_wtltltendl
  • coutltltint( poppins)
Write a Comment
User Comments (0)
About PowerShow.com