Object
Oriented
Programming
Using
C ++
Special Functions
Abdul Wahab
Lecturer
University of Science and Technology Bannu
abdul_bu@yahoo.com
1
Function
A function is a self contain block of statements that perform some specific task of some
kind
Problems prior to Functions
– Large program size
– Repeated code
– Difficult to debug
– Difficult to update
With Functions
– Programs are divided into segments
– Code duplication is avoided
– Bugs can be searched easily in small programs (functions)
– Testing is done easily
2
Advantages of Functions
1. Support for Modular Programming
2. Reduction in program size
3. Code reusability
4. A set of function can be used to form libraries.
3
Function Prototype
It consists of Function return type, function name and arguments list, terminated by a
semicolon.
It tells the compiler:
– Name of the Function
– Type of the value returned
– Type and number of arguments
For Example:
void Show(void);
int Square(int);
float Sum(float, float);
4
Function Definition
1st line of Function Definition is called ‘Function Declarator’
‘Function Declarator’ is followed by Function Body
The ‘Function Declarator’ and ‘Function Prototype’ declaration must match each other.
If the function is defined before its caller function, then its ‘Prototype’ declaration is
optional.
5
Formal & Actual Arguments
int Sum(int a, int b)
{
Return (a+b); Formal Arguments
}
void main()
Actual Arguments
{
float x=Sum(y, z);
}
6
Passing Values to Function
Pass by Value
Copies of Actual arguments are passed to formal arguments
All operations are done with the formal arguments
Any change in the formal arguments does not effect actual arguments as
Formal arguments are copies of actual arguments
Formal arguments are local to the block of called function
Once control returns back to the calling function, the changes made (to formal
arguments) vanish
7
Example:
void main()
{ void change(int a, int b)
int x, y; {
clrscr(); int k;
void change(int, int); k = a;
cout<<"Enter Values: "; a = b;
cin>>x>>y; b = k;
cout<<"Before change: "<<endl; }
cout<<"X= "<<x<<" and Y= "<<y<<endl;
change(x, y);
cout<<"After Change: "<<endl;
cout<<"X= "<<x<<" and Y= "<<y;
}
8
Output
Enter Values: 50 40
Before change:
X= 50 Y= 40
After Change:
X= 50 Y= 40
9
Passing Values to Function
Pass by Address
Instead of value, addresses are passed to the function
Function operates on addresses rather than values
Formal arguments are pointers to actual arguments
Changes made are permanent
10
Example:
void main()
{ void change(int *a, int *b)
int x, y; {
clrscr(); int *k;
void change(int *, int *); *k = *a;
cout<<"Enter Values: "; *a = *b;
cin>>x>>y; *b = *k;
cout<<"Before change: "<<endl; }
cout<<"X= "<<x<<" and Y= "<<y<<endl;
change(&x, &y);
cout<<"After Change"<<endl;
cout<<"X= "<<x<<" and Y= "<<y;
}
11
Output
Enter Values: 50 40
Before change:
X= 50 Y= 40
After Change:
X= 40 Y= 50
12
Passing Values to Function
Pass by Reference
C language passes arguments by Values and Addrees
C++ language passes arguments by Value, Address and Reference
‘&’ operator is nearly the same as pointer (*) operator, but not exaxtly
‘&’ operator declare aliases for variables
int k=0;
int &kk=k;
kk=2; //same as, writing k=2;
13
Example:
void main()
{ void change(int &a, int &b)
int x, y; {
clrscr(); int k;
void change(int &, int &); k = a;
cout<<"Enter Values: "; a = b;
cin>>x>>y; b = k;
cout<<"Before change: "<<endl; }
cout<<"X= "<<x<<" and Y= "<<y<<endl;
change(x, y);
cout<<"After Change"<<endl;
cout<<"X= "<<x<<" and Y= "<<y;
}
14
Output
Enter Values: 50 40
Before change:
X= 50 Y= 40
After Change:
X= 40 Y= 50
15
Another Example:
void FunA(int i)
{ ++i; } cout<<"\nAfter Calling Functions"<<endl;
void FunB(int &i) FunA(s);
{ ++i; } cout<<"Value of S= "<<s<<"\t by Value"<<endl;
void FunC(int *i) FunB(s);
{ ++*i; } cout<<"Value of S= "<<s<<"\t by Reference"<<endl;
void main() FunC(&s);
{ out<<"Value of S= "<<s<<"\t by Address"<<endl;
int s=4;
clrscr(); getch();
cout<<"Value of S= "<<s<<endl; }
16
Output
Value of S= 4
After Calling Functions
Value of S= 4 by Value
Value of S= 5 by Reference
Value of S= 6 by Address
17
Default Arguments
Usually, a function is called with all the arguments as declared in function prototype
declaration and function definition.
However, we can call a function with less or without parameters using default values.
Default values are values assigned to the arguments of a function
It is not allowed to assign default value which is in between the variable list.
Variable lacking default values are written first, followed by the variable containing
default values.
18
Default Arguments
Default values can be specified in function prototype or function declarator.
Normally, the default values are placed in function prototype declaration
In case, the function is defined before caller function, then there is no need to declare
function prototype, so default values are placed in function declarator.
It is useful when we update an old function by adding more arguments.
19
Another Example:
void main() cout<<"\nSum with 2 Args= "<<Sum(w, x);
{ cout<<"\nSum with 1 Args= "<<Sum(w);
clrscr(); cout<<"\nSum with 3 Args= "<<Sum(x, y, z);
int Sum(int a, int b=10, int c=15, int d=20);
int w=2; getch();
int x=3; }
int y=4;
int z=5; int Sum(int j, int k, int l, int m)
cout<<"Sum with 4 Args= "<<Sum(w, x, y, z); {
cout<<"\nSum with 3 Args= "<<Sum(w, x, y); return (j + k + l + m);
}
20
Output
Sum with 4 Args = 14
Sum with 3 Args = 29
Sum with 2 Args = 40
Sum with 1 Args = 47
Sum with 4 Args = 32
21
Inline function
Function are used to avoid code duplication
At each call to function, control passes to the function at a specified address in the
memory.
If the same function is called several times, each time the control is passed to the
function.
Due to this passing of control, execution speed decreases
C++ provides a mechanism called ‘Inline Function’.
22
Inline function
Compiler copies the code of ‘Inline function’ in the calling function i.e. function body is
inserted in the place of function call during compilation.
So transfer of control is avoided.
‘Inline Functions’ are mostly used when the function is small.
Execution performance is increased but ‘Inline Function’ needs more memory.
23
Another Example:
inline float Square (float j)
{
return (j * j);
}
void main()
{
clrscr();
int p=3, r;
r=Square(p); //In compilation ‘Square()’ code will be copied here
cout<<”R= ”<<r;
getch();
}
24
Inline function
Situation where Inline function may not work
– The function should not be recursive
– Function should not contain static variable
– Functions, containing control structure statements such as switch, if, for loop etc. can not be
Inline
– The main() function can not work as inline
25
Friend Function
The central idea of Encapsulation and Data Hiding is that any non-member function has
no access to the private data of the class
C++ allows a mechanism, in which a non-member function has access permission to the
private members of the class.
This can be done by declaring a non-member function Friend to the class whose private
data is to be accessed. Key word friend is used
Declaration is done in public or private section.
A function is declared as friend function in number of classes
These functions use objects as arguments.
26
Another Example:
class Acount void show(Acount Ac)
{ {
string Name; cout <<"\nName :"<<Ac.Name;
int AcNo; cout<<"\nAcount No: "<<Ac.AcNo;
float Bal; cout<<"\nBalance: "<<Ac.Bal;
}
public:
void Set() void main()
{ {
Name = "Bilal Khan"; clrscr();
AcNo = 2348; Acount AcObj;
Bal = 45000.98; AcObj.Set();
}
friend void show(Acount); show(AcObj);
}; getch();
}
27
Assignment
Last Date of submission is Next Class of OOP
1. Write a program to exchange values between two classes using a Friend Function
2. Write a program to declare three classes, declare integer array as data member in each
class. Perform addition of the two data member arrays of the two classes into the array
of third class using a Friend Function
28
Have a Good Day!