[go: up one dir, main page]

0% found this document useful (0 votes)
56 views5 pages

38-Encapsulation in C++-04-09-2023

Encapsulation in C++ involves combining data and methods that operate on the data into a single class. C++ provides access specifiers like public, private, and protected to control access to class members. Member functions can access private members and are used to set or retrieve data through mutator and accessor functions, hiding implementation details and achieving data encapsulation. In this example, an Employee class encapsulates private data members for ID, name, and salary, along with public mutator and accessor functions to set and get these private members.

Uploaded by

Dhruv Gupta
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)
56 views5 pages

38-Encapsulation in C++-04-09-2023

Encapsulation in C++ involves combining data and methods that operate on the data into a single class. C++ provides access specifiers like public, private, and protected to control access to class members. Member functions can access private members and are used to set or retrieve data through mutator and accessor functions, hiding implementation details and achieving data encapsulation. In this example, an Employee class encapsulates private data members for ID, name, and salary, along with public mutator and accessor functions to set and get these private members.

Uploaded by

Dhruv Gupta
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/ 5

Encapsulation in C++

A key idea in object-oriented programming (OOP) is encapsulation,


which permits data hiding and gives classes a way to isolate their
implementation details from their clients. C++ is an OOP language
that provides several features for implementing encapsulation, such as
access specifiers and member functions. In this post, we'll examine the
C++ implementation of the encapsulation principle.

Encapsulation in C++:

The act of encapsulating involves combining data and the methods


that manipulate it into a single entity, referred to as a class. A class in
C++ is a user-defined data type that has member methods and data
members. The member functions are methods that work on the data
members, which are variables that hold an object's state.

Access specifiers:

C++ provides three access specifiers, public, private, and protected, to


control the accessibility of class members. Anywhere in the programme
can access the members thanks to the public access specifier. Only the
member functions of the class are permitted access to the members
thanks to the private access specifier. The member functions of the class
and its derived classes are able to access the members thanks to the
protected access specifier.

By default, all the members of a class are private. Therefore, we need to


specify the access specifiers explicitly to control the accessibility of the
members.
Member functions:

Member functions are functions that are defined inside a class and
operate on the data members of that class. Because they are a component
of the class, member functions can access the private members of the
class. Member functions can be classified into two types: accessor
functions and mutator functions.

Accessor functions are member functions that provide read-only access


to the data members of a class. Accessor functions do not modify the
state of the object. Examples of accessor functions include get() and
display() functions.

Mutator functions are member functions that modify the state of the
object by changing the value of the data members. Mutator functions
have access to and control over a class's private members. Examples of
mutator functions include set() and update() functions.

Data hiding:

Data hiding is an important aspect of encapsulation that enables the data


members of a class to be hidden from the clients of the class. By making
the data members private and enabling accessor and mutator functions to
access and modify the data members, data hiding can be accomplished.
1. #include <iostream>
2. using namespace std;
3.
4. class Employee {
5. private:
6. int empId;
7. string empName;
8. float empSalary;
9.
10. public:
11. void setEmpId(int id) {
12. empId = id;
13. }
14.
15. void setEmpName(string name) {
16. empName = name;
17. }
18.
19. void setEmpSalary(float salary) {
20. empSalary = salary;
21. }
22.
23. int getEmpId() {
24. return empId;
25. }
26.
27. string getEmpName() {
28. return empName;
29. }
30.
31. float getEmpSalary() {
32. return empSalary;
33. }
34. };
35.
36. int main() {
37. Employee emp;
38. emp.setEmpId(15964);
39. emp.setEmpName("Poongundran");
40. emp.setEmpSalary(150000.0);
41.
42. cout << "Employee ID: " << emp.getEmpId() << endl;
43. cout << "Employee Name: " << emp.getEmpName() << endl;
44. cout << "Employee Salary: " << emp.getEmpSalary() << endl;
45.
46. return 0;
47. }

In this example, we have defined a class named Employee that has three
private data members: empId, empName, and empSalary.
We have also defined six public member functions: setEmpId(),
setEmpName(), setEmpSalary(), getEmpId(), getEmpName(), and
getEmpSalary().

The values of the private data members are set using the set functions,
which are mutator functions. The values of the private data members are
retrieved using the get functions, which are accessor functions.

In the main() function, we have created an object of the Employee class


and used the mutator functions to set the values of the private data
members. We have then used the accessor functions to get the values of
the private data members and printed them to the console.
#include <iostream>
using namespace std;

class Employee {
private:
// Private attribute
int salary;

public:
// Setter
void setSalary(int s) {
salary = s;
}
// Getter
int getSalary() {
return salary;
}
};

int main() {
Employee myObj;
myObj.setSalary(50000);
cout << myObj.getSalary();
return 0;
}

You might also like