Object as function argument , friend and static function by shahzad younas
1. 2) Friend Function
1) Object as Function Argument
3) Static Function
By Shahzad Younas
Superior University Depalpur
E-Mail: shzadyounas@gmail.com
2.
3. Group Leader M.Shahzad Roll# 15
Group Member Fiaz Tabish Roll# 40
Group Member Ali Shahzad Roll# 28
4. 1) Object as Function Argument
3) Static Function
2) Friend Function
Present by
M.Shahzad
Present by Fiaz
Tabish
Present by Ali
Shahzad
5. Review
Defining Member Function Outside
Class The member function of a class can also be
defined outside the class
The resolution Operator :: is used in function
declarator if the function is defined outside the
class
Syntax:
Return_type class_name ::
function_name(Parameters)
6. Defining Member
Function in Class
class test
{
public:
void disp()
{
cout<<“Show”;
}
};
Defining Member
Function Outside Class
class test
{
public:
void disp();
};
void test :: disp()
{
cout<<“Show”;
}
7. Object as Function Argument
Object pass as parameters to
member function.
Same method as passing simple
variables.
11. Who is a Friend ????????
A friend is one who has access to all
your “PRIVATE” stuff.
12. Friend Function
Has access to all the private and
protected members of class
A non member function
Definition part should be kept outside
the class body, as it is not a member
function of the class.
14. Rules for Friend Function
Scope resolution :: not used
Call as simple function as
exp: disp();
Void disp(BSCS a, MCS b)
Objects of class
15. #include<conio.h>
#include<iostream.h>
class A; // Forward Decleration
class B
{
private:
int b;
public:
void getval(int x)
{
b=x;
}
void showval(void)
{
cout<<"Value of b:"<<b<<endl;
}
friend void add(A,B);
};
class A
{
private:
int a;
public:
void getval(int x)
{
a=x;
}
void showval(void)
{
cout<<"Value of A:"<<a<<endl;
}
friend void add(A,B);
};
16. void add (A obj1, B obj2)
{
cout<<"Addition of A and B:"<<obj1.a+obj2.b;
}
void main()
{
A a;
B b;
clrscr();
a.getval(100);
b.getval(200);
a.showval();
b.showval();
add(a,b);
getch();
}
Program Continue . . . .
17. Static Functions
A type of member function that can be
accessed without any object of class
is called static Function.