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

Friend Functions AND ACccess specifiers

This is an extremely helpful PPT . So you can download it if you want

Uploaded by

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

Friend Functions AND ACccess specifiers

This is an extremely helpful PPT . So you can download it if you want

Uploaded by

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

Friend Functions

In Object-Oriented Programming (OOP), particularly in C++, a friend function is a special


function that is not a member of a class but is allowed to access its private and protected
members. Normally, these members are only accessible by the class itself or its member
functions, but a friend function breaks this encapsulation by being granted special access.

Here’s how it works:

Characteristics of Friend Functions:

1. Not a Member: A friend function is not a member of the class but has access to the
class's private and protected members.
2. Declared using friend keyword: The function is declared inside the class with the
friend keyword, but it is defined outside the class.
3. Function Scope: Since it is not a member, it is called like a regular function and not with
the class object.
4. Can Be a Global or Member of Another Class: A friend function can either be a global
function or a member of another class.

Why Use Friend Functions?

1. Inter-Class Functionality: They allow non-member functions or other classes to interact


closely with the internals of a class.
2. Operator Overloading: Friend functions are commonly used to overload operators (e.g.,
<< and >> for input/output operations) when the operation requires access to the private
members of a class.

Drawbacks:

 Breaks Encapsulation: Since the friend function can access private and protected
members, it violates one of the key principles of OOP, which is data hiding.
 Maintenance: More friend functions may make the code harder to maintain since access
control becomes less strict.

 Friend Function
friend function can be granted special access to private and
protected members of a class in C++. They are not the member
functions of the class but can access and manipulate the private and
protected members of that class for they are declared as friends.
A friend function can be:
1. A global function
2. A member function of another class

Syntax:
friend return_type function_name (arguments); // for
a global function
or
friend return_type class_name::function_name
(arguments); // for a member function of another
class

Friend Function Syntax

1. Global Function as Friend Function


We can declare any global function as a friend function. The
following example demonstrates how to declare a global function as
a friend function in C++:
Example:
C++
// C++ program to create a global function as a friend
// function of some class
#include <iostream>
using namespace std;

class base {
private:
int private_variable;

protected:
int protected_variable;

public:
base()
{
private_variable = 10;
protected_variable = 99;
}

// friend function declaration


friend void friendFunction(base& obj);
};

// friend function definition


void friendFunction(base& obj)
{
cout << "Private Variable: " << obj.private_variable
<< endl;
cout << "Protected Variable: " << obj.protected_variable;
}

// driver code
int main()
{
base object1;
friendFunction(object1);

return 0;
}
Output
Private Variable: 10
Protected Variable: 99

ACCESS SPICIFIES
In Object-Oriented Programming (OOP), access specifiers (also called access modifiers) define
the visibility and accessibility of class members (variables and methods). They control how these
members are accessed from outside the class or within derived classes. Access specifiers help
implement encapsulation, one of the core principles of OOP.

In C++, there are three main access specifiers:

1. Private (private)

 Visibility: Members declared as private are accessible only within the same class or by
friend functions/classes.
 Default Access Level: In a class, if no access specifier is mentioned, members are
considered private by default.
 Inheritance: When a class inherits another class, private members of the base class are
not accessible in the derived class.

cpp
Copy code
class MyClass {
private:
int privateVar; // Accessible only within this class
public:
void setPrivateVar(int val) {
privateVar = val; // Can modify private member from within the class
}
};

Usage: Most data members (attributes) are kept private to prevent unintended access and
modification, ensuring control over how the data is used.

2. Public (public)

 Visibility: Members declared as public are accessible from anywhere: inside the class,
outside the class, or from derived classes.
 Inheritance: Public members of the base class become public members of the derived
class unless access is explicitly changed during inheritance.

cpp
Copy code
class MyClass {
public:
int publicVar; // Accessible from anywhere
};

int main() {
MyClass obj;
obj.publicVar = 5; // Can access and modify publicVar from outside the
class
return 0;
}

Usage: Public members are used when you want them to be freely accessible. For example,
public methods (functions) are used to provide controlled access to private data.

3. Protected (protected)

 Visibility: Members declared as protected are accessible within the same class and in
derived (child) classes. However, they are not accessible from outside the class or from
objects of the class.
 Inheritance: Protected members of the base class are accessible in the derived class but
are not directly accessible outside the derived class.

cpp
Copy code
class BaseClass {
protected:
int protectedVar; // Accessible in derived class
};

class DerivedClass : public BaseClass {


public:
void accessProtected() {
protectedVar = 10; // Accessible in derived class
}
};

Usage: Protected access specifier is commonly used when you want to allow derived classes to
access or modify certain members, but don't want them exposed to the rest of the program.

Example of All Three Access Specifiers:


cpp
Copy code
class MyClass {
private:
int privateVar; // Only accessible within this class

protected:
int protectedVar; // Accessible within this class and derived classes

public:
int publicVar; // Accessible everywhere

// Constructor to initialize variables


MyClass() {
privateVar = 1;
protectedVar = 2;
publicVar = 3;
}

// Public function to access private variable


int getPrivateVar() {
return privateVar;
}
};

class DerivedClass : public MyClass {


public:
void accessVars() {
// privateVar is NOT accessible here
protectedVar = 20; // Accessible in derived class
publicVar = 30; // Accessible in derived class
}
};

int main() {
MyClass obj;
// obj.privateVar = 10; // Error: privateVar is private
// obj.protectedVar = 20; // Error: protectedVar is protected
obj.publicVar = 30; // Allowed: publicVar is public

DerivedClass d;
d.accessVars(); // Can access protected and public members
return 0;
}

Summary of Access Levels:

Access Specifier Same Class Derived Class Outside Class/Object


Private Yes No No
Protected Yes Yes No
Public Yes Yes Yes
Why Use Access Specifiers?

 Encapsulation: They allow you to hide internal details and expose only what is
necessary.
 Data Security: Prevents accidental or malicious modification of data by limiting access.
 Code Maintenance: Helps maintain a clear interface for interacting with class objects.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy