0% found this document useful (0 votes)
108 views

Topic: Member Functions: Course Name

Here is a C++ program to swap private data members of two classes using a friend function: #include <iostream> using namespace std; class class_1 { private: int x; public: void setX(int a) { x = a; } friend void swap(class_1 &, class_2 &); }; class class_2 { private: int y; public: void setY(int b) { y = b; } friend void swap(class_1 &, class_2 &); }; void swap(class_1 &a, class_2 &b)

Uploaded by

zaibakhan8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Topic: Member Functions: Course Name

Here is a C++ program to swap private data members of two classes using a friend function: #include <iostream> using namespace std; class class_1 { private: int x; public: void setX(int a) { x = a; } friend void swap(class_1 &, class_2 &); }; class class_2 { private: int y; public: void setY(int b) { y = b; } friend void swap(class_1 &, class_2 &); }; void swap(class_1 &a, class_2 &b)

Uploaded by

zaibakhan8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Topic: Member Functions

Course Name:
Object Oriented
Programming with
C/C++
Course Code:
19008600
Accessing Member Functions Within
The Class
 A member function of a class can call
any other member function of its own
class as well irrespective of its
privilege and this situation is called
nesting of member functions.
 Method of calling a function of one’s
own class is similar to calling any
other standard(library) functions in
program.
 For example:
Data Hiding
 Declaring class members under private
keyword, provides some security to the data,
known as DATA HIDING.
 Data members are declared as private,
however sometimes member functions can
also be declared as private.
 In this case objects can’t access these
member functions also.
 To access them, objects must access a public
member function( Which in turn can access
the private member function).

$
Data Hiding(Summary)
 Data Hiding is also known as “Data Abstraction”
 It means SHOW that data which important for
user and HIDE data which is not important.
In other words, showing only that data with which
users wants to interact and to hide the details which
are not necessary for the user.

This is known as “DATA ABSTRACTION”


**Another Important factor of it is that it not only
“hide the data” but “also protect the data” i.e.
whatever information is hidden is automatically
protected.**
Class ,Objects & Memory Resources
 When a class is declared memory is not allocated
to the data members of the class.
 When an object is created for particular class,
memory is allocated to both data members &
member functions. THIS IS PARTLY TRUE.
Memory only allocated to data members.
 Allobjects of that class have access to the same
area in the memory where the member functions
are stored.
 Separate allocation for every object data
members since that they contain different values.
Static Members of a class

 A class may contain following type of


static member:
 Static data members
 Static member functions
 Static Data Member:
 When a data members is declared with
“static” keyword known as “static data
members”
Syntax:
Static data_type variable;
Static Members of a class(Cont…)
 Features:
 Static Data Member is associated with the
class not with object i.e. declared once and
then shared among objects of the class.
 Initialization of static member is extremely
important else it will store “Garbage Value”.
Syntax:
Data_type class_name : : variable_name=value;
Data_type class_name : : variable_name;

 Static is initialized only once after that it always retains


the last changed value.
 Static Data Member can be accessed using normal
member function & static member function.
Static Members of a class(Cont…)

 Static Member Function:


 When a member function is declared with
static keyword then it is known as static
member function.
Synatx:
static return_type function_name()
{
}
Static Members of a class(Cont….)

 Features:
 Static member function can be Public/Private.
 When Public: it invoked using its class name
without using object.
 When Private: the function is invoked inside a
static public member function.
 Static Member Function can access only static
members of a class.
 It is declared only once & the same copy of
static function is shared by the objects of that
class.
Static Objects
 When object is made static, then all data
members are initialized by a default value
as per data type.
 Only one object can be made static.
 For Example:
Example of Array of Object
Friend Function
 Access Privileges in C++
◦ You have access privileges in C++ such as
public, private, protected that helps in
encapsulation of data at various level.
 Private:
If data are declared as private in a class
then its accessible by the member
functions of the class where they are
declared.
The private member functions can be
accessed only by the member of the class.
By default, any member of the class is
considered as private by the C++ compiler,
if no specifier is declared for the member.
Friend Function(Cont…)
 Public:
 The member functions with public access specifier
can be accessed outside of the class. This kind of
members is accessed by creating instance of class.

 Protected:
 Protected members are accessible by the class itself
and its subclasses.
 This specifier specially used when you need to use
inheritance facility of C++.
 The protected members become private of a child
class in case of private inheritance, public in case of
public inheritance, stay protected in case of protected
inheritance.
Friend Function(Cont…)
 When we want our private data to be shared by non
member function.
Then:
• Basically , we declare something as a friend, you give it
access to your private data members.
• Single functions or entire classes may be declared as
friends of a class.
Friend Function(Cont…)
 A friend function is a non-member function of the class
that has been granted access to all private members of
the class.
 We simply declare the function within the class by
prefixing its declaration with keyword friend.
 Function definition must not use keyword friend.
 Definition of friend function is specified outside the
class body and is not treated as a part of the class.
 The MAJOR difference b/w member function and friend
function is that the member function is accessed
through object to be passed as parameter.
Syntax
 class<class_name>
{
……
public:
friend return_type function_name(class_name obj)

};
return_type function_name(class_name obj)
{

}
Note:
 Friend Function must present inside
the function. However, function must
be defined outside the class without
using “Friend” keyword.
 Since it is not a member of a class, a
friend function is directly called
without using object.
 A function can be friend to multiple
classes.
 A class can have multiple Friend
Function.
Ques Write a program in C++ to find out useful life of a product using class &
object. Use friend function to calculate the lifetime of product.
#include<iostream.h>
#include<conio.h>
Class PRO
{
int mfg, exp;
public:
void input ( );
friend void lifetime(PRO);
};
void PRO : : input( )
{
cout<< “Enter the year of manufacture & expiry: “;
cin>> mfg>> exp;
}
void lifetime(PRO p1)
{
int LOP=0;
LOP=p1.exp – p1.mfg;
cout<< “ Useful life of a product : “ << LOP <<‘’yrs”;
}
void main( )
{
PRO p2;
p2.input;
lifetime(p2); //Friend Function is called
getch();
}
Ques Write a program in C++ to illustrate the concept that a function can friend of
multiple classes.

#include<iostream.h>
#include<conio.h>
class TWO; //Forward declaration of class
class ONE
{
int n1;
public:
void input ( );
friend void add(ONE,TWO); //Friend Function of class one
};
void ONE: : input( )
{
cout<< “Enter the value of n1: “;
cin>> n1;
}
class TWO
{
int n2;
public:
void input ( );
friend void add(ONE,TWO); //Friend Function of class two
};
void TWO: : input( )
{
cout<< “Enter the value of n1: “;
cin>> n2;
}
Void add(ONE o1,TWO t1)
{
int sum=0;
sum=o1.n1 + t1.n2;
cout<< “ Sum is “ <<sum;
}
Void main( )
{
ONE o2;
o2.input();
TWO t2;
t2.input();
add(02,t2);
}

Ques (Do it own your own)


Write C++ program to illustrate the concept that a class can have
multiple friend function.
Ques (Do it own your own)
Write C++ program to illustrate the concept that a class can have
multiple friend function.

Ques (Do it own your own)


Write a program to find Maximum out of Two Numbers Using Friend
Function.

Ques (Do it own your own)


Write a program to swap private data members of classes named as
class_1, class_2 using friend function.

You might also like