Lecture 9: Friend functions and overloading operators - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture 9: Friend functions and overloading operators

Description:

Lecture 9: Friend functions and overloading operators Software Engineering: Friend functions Overloading operators Array of objects – PowerPoint PPT presentation

Number of Views:149
Avg rating:3.0/5.0
Slides: 23
Provided by: CityU
Category:

less

Transcript and Presenter's Notes

Title: Lecture 9: Friend functions and overloading operators


1
Lecture 9 Friend functions and overloading
operators
  • Software Engineering
  • Friend functions
  • Overloading operators
  • Array of objects

2
  • A friend function of a class is defined outside
    that classs scope, yet has the right to access
    private members of the class.
  • Friend functions enhances performance.
  • For instance, friend functions can be used to
    define overloading operators. An example will be
    given after overloading operators are discussed.

3
Friends can access private members of a
class include ltiostream.hgt Class Count
friend void setX(Count , int) //friend
declaration public Count(int y) xy
//constructor void print() coutltltxltlt\n pr
ivate int x counter.x after
instantiation 0 counter.x after call to setX 8
void setX(Cout c, int val) c.x val int
main() Count counter(0) coutltlt
counter.x after instantiation counter.print()
coutltltcounter.x after call to
setX setX(counter, 8) // set x with a
friend counter.print() Return 0
4
  • C is a type-sensitive and type-focused
  • operators -- , - , / etc.
  • Y153 or x16-x
  • operators with user-defined types can also be
    used.
  • object3object1 object2
  • no new operator can be created.
  • most of the existing operators can be
    overloaded so that when applied to class, they
    mean appropriately to the class.

5
Class Time public int hours, minutes,
second Time operator(const Time , const
Time ) //overloading Time
Timeoperator(const Time x, const Time y)
Time z z.hoursx.hoursy.hours
z.minutesx.minutesy.minutes
z.secondsx.secondsy.seconds return z
void main() Time x1, x2, x3 x1.hours0 x1.mi
nutes10 x1.seconds20 x2.hours1 x2.minutes11
x2.seconds11 x3x1x2
6
Another choice define as a friend
operator Class Time friend Time
operator(const Time , const Time )
//overloading public int hours,
minutes, second Time operator(const Time
x, const Time y) Time z
z.hoursx.hoursy.hours
z.minutesx.minutesy.minutes
z.secondsx.secondsy.seconds return z
7
Class Time public int hours, minutes,
seconds Time operator(const Time , const
Time ) Time(int x, int y, int
z)//constructor //overloading Time
Timeoperator(const Time x, const Time y)
Time z z.hoursx.hoursy.hours
z.minutesx.minutesy.minutes
z.secondsx.secondsy.seconds return z
TimeTime(int x, int y, int z) hoursx
minutesy secondsz void main() Time
x1(0, 10, 20), x2(1, 11, 11) , x3 //x1.hours0 /
/x1.minutes10 //x1.seconds20 //x2.hours1 //x
2.minutes11 //x2.seconds11 x3x1x2
8
  • class Money//an ADT for HK, in file money.h
  • public
  • Money(int d,int c) //constructor
    d-dollars,c-cents
  • //precondition d and c are assigned values
    clt100
  • //postcondition dollars d, cents c
  • void give_output() //precondition (dollars,
    cents) given
  • //postcondition and the amount is printed to
    screen
  • void get_input() //input from keyboard
  • //precondition amount1.amount2 is typed
  • //postcondition dollarsamount1, centsamount2
  • private
  • int dollars int cents //number of dollars and
    cents

9
Implement Constructor and Output for Money
  • MoneyMoney(int d,int c) //in money.cpp
  • dollarsd
  • centsc
  • void Moneygive_output() //in money.cpp
  • cout ltlt The amount is ltltdollarsltlt.ltltcents

10
Overloading operators
  • An operator is in principle the same as a
    function. It has some arguments. For example
    has two arguments, one before it and one after
    it.
  • We know general functions cannot access private
    members of a class. The same applies to
    operators. To overcome this hurdle, we use friend
    function.
  • friend Money operator (const Money x,const
    Money y)
  • //define operator for class Money and making
    private members //of Money available to by
    declared as its friend operator.
  • //precondition x and y are assigned values.
  • //postcondition the sum of dollar values of x
    and y is returned.

11
An Example of overloading
  • we need to add a line in class Money prototype
  • friend Money operator (const Money x,const
    Moeny y)
  • Then the definition
  • Money operator (const Money x,const Moeny y)
  • Money tmp
  • tmp.dollarsx.dollarsy.dollars
  • tmp.centsx.centsy.cents
  • if tmp.cents gt100)
  • tmp.centstmp.cents-100 tmp.dollarstmp.dollar
    s1
  • return tmp

12
  • An array of objects
  • void main(void)
  • int f51,1,1,1,1, k, i,q
  • int driver DETECT,mode
  • Elevator x5
  • char y'0'
  • for (k0 klt4 k)
  • xk.sign1
  • xk.k0
  • initgraph(driver,mode,"A\\bgi")
  • while(y!'x')
  • yread_key()
  • for (k0 klt4 k)
  • if(fk1)xk.kxk.kxk.sign5
  • setcolor(GREEN)
  • for (k0 klt4 k)
  • xk.selevator(0100k)
  • setcolor(GREEN)
  • for (k0 klt4 k)
  • if (xk.kgt250) xk.sign-1
  • if (xk.klt0) xk.sign1
  • delay (300)
  • for (k0 klt4 k)
  • xk.ereaseel(0100k)
  • closegraph()

13
Summary of class
  • What is a class ? (type of objects)
  • Definition of class -- member variables member
    functions
  • public vs private
  • public member variables and functions can be used
    everywhere
  • private member variables can only be accessed by
    the member functions of the sane class.
  • Friend functions can access the private member
    variables
  • overloading existing operators are possible
  • objects are like variables, thus we can define
    an array of objects.
  • We can try to do the same things for objects as
    for variables, e.g., pointers, etc. We will not
    mention too many.

14
A step by step example for different concepts
Question 1 define a class named Coursework
containing ass1, ass2 and total as public meber
variables and input and output as the member
functions for input and output. Class Coursework
public int
ass1, ass2, total void input() void
output() Coursework input ()
coutltltPlease enter the mark of
ass1 \n cin gtgt ass1 coutltltplease
enter the mark of ass2 \n cingtgtass2
Courseworkoutput() coutltltYour ass1 is
ltltass1ltltyour ass2 is ltlt ass2ltltthe totaal is
ltlttotal ltlt\n
15
A step by step example for different concepts
(continued) Question 2 write a main function to
deal with one object called x and input the data
and output the data. void main () Coursework
x x.input() x.output() Question 3 How
about two objects? void main () Coursework
x, y x.input() x.output()
y.input() y.output()
Courseworkoutput() coutltltYour ass1 is
ltltass1ltltyour ass2 is ltlt ass2ltltthe totaal is
ltlttotal ltlt\n
16
A step by step example for different concepts
(continued) Question 4 define the
constructor Class Coursework
public int ass1, ass2, total
Coursework(int a1, int a2) void
input() void output()
CourseworkCoursework(int a1, int a2)
ass1a1 ass2a3 totalass1ass2
Question 5 How to use the construct? Void
main() Coursework x(100, 99),y
x.output() y.input() y.output()
17
A step by step example for different concepts
(continued) Question 6 define two private
variables w1 and w2. Class Coursework
public int ass1, ass2,
total Coursework(int a1, int a2)
void input() void output() private
float w1, w2
CourseworkCoursework(int a1, int a2)
ass1a1 ass2a3 totalw1ass1w2ass2
18
A step by step example for different concepts
(continued) Question 7 overload the operator
for the class. Class Coursework
friend Coursework operator (const Coursework
x, const Coursework y)
public int ass1, ass2, total
Coursework(int a1, int a2) void input()
void output() private float w1, w2

Coursework operator (const Coursework x, const
Coursework y) Coursework temp
temp.ass1x.ass1y.ass1 temp.ass2x.ass2y.as
s2 temp.totalx.totaly.total
temp.w1x.w1 temp.w2x.w2 return
temp
19
A step by step example for different concepts
(continued) Question 8 How to use the
overloaded operator ? void main()
Coursework x(100, 99), y(99,100), z zxy
z.output()
20
Exercise 1 Give the output of the following
program. include ltiostream.hgt Class Count
friend void setX(Count , int) public
Count(int y) xy //constructor void print()
coutltltxltlt\n private int x
Void setX(Cout c, int val) c.x val int
main() Count counter(0), c1(1) coutltlt
counter.x after instantiation counter.print()
coutltltcounter.x after call to
setX setX(counter, 8) // set x with a
friend counter.print() c1.print() Return 0
21
Exercise 2 Give the output of the following
program. include ltiostream.hgt Class Count
friend void setX(Count , int) public
Count(int y) xy //constructor void print()
const coutltltxltlt\n private int x
Void setX(Cout c, int val) c.x val Count
counter(9) int main() Count counter(0),
c1(1) coutltlt counter.x after
instantiation counter.print() coutltltcounter.x
after call to setX setX(counter, 8) // set
x with a friend counter.print() c1.print() Retur
n 0
22
Exercise 3 Give the output of the following
program. include ltiostream.hgt Class Count
friend void setX(Count , int) public
Count(int y) xy //constructor void print()
const coutltltxltlt\n private int x
void setX(Cout c, int val) c.x val Count
counter(9) void f() counter.print() int
main() Count counter(0), c1(1) f()
counter.print() setX(counter, 8) // set
x with a friend counter.print()
c1.print() Return 0
Write a Comment
User Comments (0)
About PowerShow.com