Tải bản đầy đủ (.ppt) (35 trang)

Object oriented programming with C++ - Session 3 Function Overloading and References ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (226.41 KB, 35 trang )


Object Oriented Programming with C++/ Session 3 / 1 of 35
Function Overloading and
References
Session 3
Object Oriented
Programming with C++/
Session 3/ 2 of 35
Session Objectives

Understand the concept of functions with
default arguments

Define and use Friend functions

advantages

disadvantage

friend classes

Describe function overloading

various data types

different number of arguments
Object Oriented
Programming with C++/
Session 3/ 3 of 35
Session Objectives (Contd.)


describe the scope for function overloading

Explain the use of reference arguments

passing references to functions

returning references from functions

Define and use Inline functions
Object Oriented
Programming with C++/
Session 3/ 4 of 35
Functions

A function declaration gives:

the name of the function

the type of the value returned (if any) by the
function

the number and types of the arguments that
must be supplied in a call of the function

A function declaration may or may not
contain argument names.

Possible to call a function without
specifying all its arguments.
Object Oriented

Programming with C++/
Session 3/ 5 of 35
Functions with default arguments

The function declaration must provide
default values for those arguments that are
not specified.

Whenever a call is made to a function without
specifying an argument, the program will
automatically assign values to the parameters
from the default declaration.
void func(int = 1, int = 3, char = '*');
//prototype declaration
or
void func(int num1,int num2 = 3,char ch = '*');
Object Oriented
Programming with C++/
Session 3/ 6 of 35
Default values for arguments

Once an argument is given a default value in the
list of formal arguments, all of the remaining
must have default values also.

Only the trailing values can be defaulted.
void func(int num1=2,int num2, char ch='+');
//error

Default values must be of the correct types or the

compiler will issue an error.

Default values can be given in either the prototype or
the function definition header, but not in both.
• Highly recommended that the default values be given
in the prototype declaration rather than in the function
definition.
Object Oriented
Programming with C++/
Session 3/ 7 of 35
Default values for arguments (cont.)

The following calls to the function func declared
above
func(2,13,'+');
func(1);
//default values for second and third arguments
func(2,25); //default value for third argument
func(); //default values for all three args
func(2,,'+'); //invalid

If you leave out any arguments in the middle
the compiler would not know what you are
referring to and will indicate an error.
Object Oriented
Programming with C++/
Session 3/ 8 of 35
Advantages

Default arguments are useful if you want to

use arguments, which will almost always
have the same value in a function.

Also useful when, after a program is
written, the programmer decides to
increase the capability of a function by
adding an argument.

Existing function calls can continue to use the
old number of arguments, while new function
calls can use more.
Object Oriented
Programming with C++/
Session 3/ 9 of 35
Friend Functions

Private data values cannot be read or
written to by non-member functions.

We need a means to allow a function
access to the private part of a class without
requiring membership.

A non-member function that is allowed
access to the private part of a class is
called a friend of the class.
Object Oriented
Programming with C++/
Session 3/ 10 of 35
Friend Functions (Contd.)

Class
Friend
Function
Private!
Keep out!
Except members and friends
Object Oriented
Programming with C++/
Session 3/ 11 of 35
Friend Functions (Contd.)

A function is made a friend of a class by a
friend declaration in that class
class person{
public:
void getdata();
friend void display(person abc);
};
void display(person abc) //friend function
//without :: operator
{//… some code…}

The keyword friend is not repeated in the
function definition.
Object Oriented
Programming with C++/
Session 3/ 12 of 35
Friend Functions (Contd.)

If the same function needed to access objects

from different classes it would be most useful to
make it a friend of the different classes
class Teacher;
//forward declaration
class Student{
private:
int st_data;
public:
void getstuddata();
friend void display(Student abc, Teacher xyz);
};
Object Oriented
Programming with C++/
Session 3/ 13 of 35
Friend Functions (Contd.)
class Teacher{
private:
int th_data;
public:
void getteachdata();
friend void display(Student abc, Teacher xyz);
};
void display(Student abc, Teacher xyz)
{//… some code…}

Forward declaration: A class cannot be referred to
until it has been declared. Therefore, class
Teacher has been declared before the class
Student.
Object Oriented

Programming with C++/
Session 3/ 14 of 35
Features of a friend function

Nothing special about a friend function apart from
its right to access the private part of a class.

Friend function does not have a this pointer.

Friend declaration can be placed in either the
private or public part of a class specifier.

Definition of a friend function does not require
the class name with the scope resolution
operator prefixed to it.
Object Oriented
Programming with C++/
Session 3/ 15 of 35
Controversy about friend functions.

Friend functions increase flexibility in
programming but they are against the principles
of object-oriented programming.
• Breach of integrity in the coding can be controlled to
some extent.

A friend function has to be declared in the class
whose data it will access. This cannot be done if
the source code is not available to a programmer.
If the source code is available, then existing

classes should not be modified as far as possible.
Object Oriented
Programming with C++/
Session 3/ 16 of 35
Advantages

Friend functions provide a degree of
freedom in the interface design options.

Member functions and friend functions
are equally privileged.

Major difference is that a friend function is
called like func(xobject), while a member
function is called like xobject.func().

Designer can select the syntax that is
considered most readable.
Object Oriented
Programming with C++/
Session 3/ 17 of 35
Friend classes

Declare a single member function, a few member
functions or a whole class as a friend of another
class.

Example of single function as friend
class beta; //forward declaration
class alpha{

private:
int a_data;
public:
alpha(){a.data = 10;}
void display(beta);
};
Object Oriented
Programming with C++/
Session 3/ 18 of 35
Friend classes example(Contd.)
class beta{
private:
int b_data;
public:
beta(){b_data = 20;}
friend void alpha::display(beta bb);
};
void alpha::display(beta bb)
{ cout<<"\n data of beta ="<<bb.b_data;
cout<<"\n data of alpha ="<<a_data; }
void main(){
alpha a1;
beta b1;
a1.display(b1);
}
Object Oriented
Programming with C++/
Session 3/ 19 of 35
Friend classes (Contd.)


When all or most of the functions of a particular
class have to gain access to your class, you can
consider allowing the whole class friend
privileges.
class beta;
class alpha{
private:
int data;
public:
friend class beta;//beta is a friend class
};
Object Oriented
Programming with C++/
Session 3/ 20 of 35
Friend classes (Contd.)
class beta{
public:
void display(alpha d) //can access alpha
{cout<<d.data;}
void get_data(alpha d) //can access alpha
{int x = d.data;}
};

All the member functions of class beta can
access the private data members of alpha.

However, the public member functions of the
class alpha cannot access the private
members of the class beta.
Object Oriented

Programming with C++/
Session 3/ 21 of 35
Function Overloading

Used to define a set of functions that are given the
same name and perform basically the same
operations, but use different argument lists.

Polymorphism is essentially one thing having
many forms. Therefore functional polymorphism
means one function having several forms.
void display(); // Display functions
void display(const char*);
void display(int one, int two);
void display(float number);
Object Oriented
Programming with C++/
Session 3/ 22 of 35
Function Overloading (Contd.)

Compiler uses the context to determine
which definition of an overloaded function
is to be invoked: depends on the number
and type of arguments supplied in the call.

Only those functions that basically do the
same task, on different sets of data, should
be overloaded.
Object Oriented
Programming with C++/

Session 3/ 23 of 35
Advantages

Eliminates the use of different function
names for the same operation

Helps to understand and debug code easily

Maintaining code is easier
Object Oriented
Programming with C++/
Session 3/ 24 of 35
Overloading with various data types

Compiler can distinguish between overloaded
functions with the same number of arguments,
provided their type is different.
int square(int);
float square(float);
double square(double);

Can use as many overloadings as desired
provided all of the parameter patterns are unique.

Many programming languages have overloaded output
functions so that you can output any data with the
same function name.
Object Oriented
Programming with C++/
Session 3/ 25 of 35

Overloading with different number of arguments
int square(int); //function declarations
int square(int,int,int);
int asq = square(a) //function calls
int bsq = square(x,y,z)

Invoke the function that is the best match on the
arguments or else the compiler gives an error if no
function produces the best match.

Note that the way the compiler resolves the overloading is
independent of the order in which the functions are
declared.

Return types of the functions are not considered.

×