overview n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Overview PowerPoint Presentation
Download Presentation
Overview

Loading in 2 Seconds...

play fullscreen
1 / 31

Overview - PowerPoint PPT Presentation


  • 65 Views
  • Uploaded on

Overview. Class Scope Review: Object parameters passed by value reference constant reference Friend function Overloading operator. Object parameters. Objects can be passed by: Value Reference Constant Reference. Fraction Class. // class declaration class Fraction {

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Overview' - ivrit


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
overview
Overview

Class Scope

Review:

Object parameters passed by

value

reference

constant reference

Friend function

Overloading operator

object parameters
Object parameters
  • Objects can be passed by:
    • Value
    • Reference
    • Constant Reference
fraction class
Fraction Class

// class declaration

class Fraction

{

private:

int num, den;

public:

Fraction(int=0, int=1);

void add(Fraction, Fraction);

void reduce();

float decimal();

void reveal(int &, int &);

};

slide4

Object parameters

There are three ways to declare the add() member

function of the Fraction class:

version 1: void add(Fraction, Fraction);

version 2: void add(Fraction &, Fraction &);

version 3: void add(const Fraction &, const Fraction &);

slide5

Object parameters

Here is part of the main():

Fraction x(1,2);

Fraction y(3,4);

Fraction z;

z.add(x,y);

Function invocation same regardless of its declaration

slide6

Object parameters:

Diagram of version 1 when z.add(x,y) executes

main()

num 1

x

den 2

add()

num 3

y

den 4

num 1

A

den 2

z

num 0

num 3

den 1

B

den 4

slide7

Object parameters:

Explanation of version 1 when z.add(x,y) executes

Parameters passed by value

A copy of the actual parameters x and y are sent to

formal parameters A and B.

This is OK for small object but very inefficient for

larger objects

slide8

Object parameters:

Diagram of version 2 when z.add(x,y) executes

main()

num 1

x

den 2

add()

num 3

y

den 4

A

z

num 0

den 1

B

slide9

Object parameters:

Explanation of version 2 when z.add(x,y) executes

Parameters passed by reference

More efficient

Dangerous: function can inadvertently (or maliciously)

change an actual parameter (in the main) by changing the formal parameter.

It is not the case here.

slide10

Object parameters:

Diagram of version 3 when z.add(x,y) executes

main()

num 1

x

den 2

add()

num 3

y

den 4

A

z

num 0

den 1

B

slide11

Object parameters:

Explanation of version 3 when z.add(x,y) executes

Parameters passed by constant reference

Both of the best world: security and efficiency

No Danger: compiler will not allow to modify the actual parameter, even the formal parameters.

If the function tries to do this:

  • A.num = 1000; //Will not be allowed by the compiler
  • int i;
  • i = A.num ; //Will be allowed by the compiler
slide12

Friend Functions

A friend function is not a member function of a class

but is allowed to touch anything protected or private

It is not a category such as Private or Public

friend or foe
Friend or Foe??

Let's look at Fraction again. Consider the

Fraction::add() function.

It's a __________ function.

Can it be restructured as a friend function?

friend functions
Friend Functions

To convert Fraction::add() function from a member function to a friend function:

1. put it onto the friends list

2. change its return type

3. remove its class scope operator

add member version
add() – Member Version

declaration:

void add(Fraction, Fraction);

definition:

void Fraction::add(Fraction A, Fraction B)

{

num = A.num*B.den + B.num*A.den;

den = A.den * B.den;

}

add member version1
add() – Member Version

invocation:

Fraction x(1,2), y(3,4), z;

z.add(x,y); //now z holds the value 5/4

add friend version
add() – Friend Version

declaration:

friendFraction add(Fraction, Fraction);

definition:

Fraction add(Fraction A, Fraction B)

{

Fraction C;

C.num = A.num*B.den +B.num*A.den;

C.den = A.den * B.den;

return C;

}

add friend version1
add() – Friend Version

invocation:

Fraction x(1,2), y(3,4), z;

z = add(x,y);

friend functions1
Friend Functions

Member function example:

z.reduce();

Friend function example:

z = reduce(z);

How would you make reduce() a friend function???

friend functions2
Friend Functions

Review:

Function overloading

What does it mean to "overload" a function?

  same function name with different argument types

  functions must differ by more than return type!

slide21

Friend Functions

An example: a findLowest function

char findLowest(char, char, char);

int findLowest(int, int, int);

float findLowest(float, float, float);

double findLowest(double, double, double);

long findLowest(long, long, long);

BankAccount findLowest(BankAccount,BankAccount,BankAccount);

Another example: constructor functions: 

Triangle();

Triangle(int, int, int);

friend functions3
Friend Functions

It's a very short jump from the friend add function to

an overloaded operator:

Fraction add(Fraction A, Fraction B)

{

fraction C;

C.num = A.num + B.num;

C.den = B.den * A.den;

C.reduce();

return C;

}

friend functions4
Friend Functions

Fraction operator+ (fraction A, fraction B)

{

Fraction C;

C.num = A.num + B.num;

C.den = B.den * A.den;

C.reduce();

return C;

}

friend functions5
Friend Functions

Now in main instead of

  z = add(x, y)

we can say:

  z = x + y;

How would the declaration of the fraction class

need to change?

In fraction.h:

friend Fraction add(Fraction, Fraction);

friend Fraction operator+(Fraction, Fraction);

slide25

Operator Overloading

We can also overload C++ operators.

We can make our own = operator:

in .h file

void operator = (Fraction);

in .cpp file

void Fraction::operator= (Fraction rightSide)

{

num = rightSide.num;

den = rightSide.den;

}

slide26

Operator Overloading

We can make our own = operator:

in main

Fraction x(1,2);

Fraction z;

z.operator=(x);

//or just

z = x;

slide27

#include <iostream.h>

#include "fraction_class.h"

void main()

{

int i, j;

Fraction x(1,2);

x.reveal(i,j);

cout<<"Fraction x is "<<i<<"/"<<j<<endl;

Fraction z;

z.operator=(x);

z.reveal(i,j);

cout<<"Fraction z is "<<i<<"/"<<j<<endl;

z = x;

z.reveal(i,j);

cout<<"Fraction z is "<<i<<"/"<<j<<endl;

}

Fraction x is 1/2

Fraction z is 1/2

Fraction z is 1/2

slide28

Operator Overloading

What happens when Fraction::operator=() is called?

slide29

Operator Overloading

in .h file

void operator = (Fraction &);

in .cpp file

  void Fraction::operator= (Fraction & rightSide)

{

num = rightSide.num;

den = rightSide.den;

}

What is the danger now??

slide30

Operator Overloading

in .h file

void operator = (const Fraction&);

in .cpp file

void Fraction::operator= (const Fraction & rightSide)

{

num = rightSide.num;

den = rightSide.den;

}

This is an example of "overloading" an operator.

slide31

Operator Overloading

How would we overload the + operator for fractions??

Two ways:

1) use analogy to the operator= example

2) use a friend function