Friend Functions AND ACccess specifiers
Friend Functions AND ACccess specifiers
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.
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
class base {
private:
int private_variable;
protected:
int protected_variable;
public:
base()
{
private_variable = 10;
protected_variable = 99;
}
// 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.
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
};
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.
protected:
int protectedVar; // Accessible within this class and derived classes
public:
int publicVar; // Accessible everywhere
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;
}
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.