C++ Encapsulation: Private Members, Getters & Setters
Module 16: Encapsulation in C++
Encapsulation is one of the four fundamental principles of Object-Oriented Programming (OOP). It is the process of wrapping data (variables) and methods (functions) into a single unit (class) while restricting direct access to the data.
Encapsulation protects object data from unauthorized access and modification. Instead of accessing variables directly, controlled access is provided through getter and setter methods.
Benefits of Encapsulation
- Improves data security.
- Prevents accidental modification.
- Makes code easier to maintain.
- Allows validation before updating data.
- Promotes modular and reusable code.
1. Private Members
A private member can only be accessed from within the same class. It cannot be accessed directly outside the class.
Example
1#include <iostream> 2 3using namespace std; 4 5class Student 6{ 7private: 8 string name; 9 int age; 10 11public: 12 void display() 13 { 14 cout << name << endl; 15 cout << age; 16 } 17}; 18 19int main() 20{ 21 Student s; 22 23 // s.name = "Ankit"; // Error 24 // s.age = 22; // Error 25 26 return 0; 27}
The above code produces an error because name and age are private members.
2. Getter Functions
A getter is a public member function used to read the value of a private variable.
Example
1#include <iostream> 2 3using namespace std; 4 5class Student 6{ 7private: 8 string name = "Ankit"; 9 10public: 11 string getName() 12 { 13 return name; 14 } 15}; 16 17int main() 18{ 19 Student s; 20 21 cout << s.getName(); 22 23 return 0; 24}
Output
1Ankit
3. Setter Functions
A setter is a public member function used to update the value of a private variable.
Example
1#include <iostream> 2 3using namespace std; 4 5class Student 6{ 7private: 8 string name; 9 10public: 11 void setName(string n) 12 { 13 name = n; 14 } 15 16 string getName() 17 { 18 return name; 19 } 20}; 21 22int main() 23{ 24 Student s; 25 26 s.setName("Ankit"); 27 28 cout << s.getName(); 29 30 return 0; 31}
Output
1Ankit
4. Validation
One of the biggest advantages of encapsulation is data validation. Before updating a private member, you can verify that the new value is valid.
Example
1#include <iostream> 2 3using namespace std; 4 5class Student 6{ 7private: 8 int age; 9 10public: 11 void setAge(int a) 12 { 13 if (a >= 0 && a <= 120) 14 { 15 age = a; 16 } 17 else 18 { 19 cout << "Invalid Age" << endl; 20 } 21 } 22 23 int getAge() 24 { 25 return age; 26 } 27}; 28 29int main() 30{ 31 Student s; 32 33 s.setAge(22); 34 35 cout << s.getAge(); 36 37 return 0; 38}
Output
122
Validation Example (Invalid Input)
1Student s; 2 3s.setAge(-5);
Output
1Invalid Age
The invalid value is rejected, protecting the object's data.
Complete Example
1#include <iostream> 2 3using namespace std; 4 5class Employee 6{ 7private: 8 string name; 9 double salary; 10 11public: 12 void setName(string n) 13 { 14 name = n; 15 } 16 17 void setSalary(double s) 18 { 19 if (s >= 0) 20 { 21 salary = s; 22 } 23 } 24 25 string getName() 26 { 27 return name; 28 } 29 30 double getSalary() 31 { 32 return salary; 33 } 34}; 35 36int main() 37{ 38 Employee emp; 39 40 emp.setName("Ankit"); 41 emp.setSalary(75000); 42 43 cout << "Name: " << emp.getName() << endl; 44 cout << "Salary: " << emp.getSalary(); 45 46 return 0; 47}
Output
1Name: Ankit 2Salary: 75000
Encapsulation Workflow
1Private Data 2 │ 3 ▼ 4 Setter Function 5 │ 6 Validation 7 │ 8 ▼ 9 Store Data 10 │ 11 ▼ 12 Getter Function 13 │ 14 ▼ 15 Return Data
Best Practices
- Keep data members
private. - Access data through public getters and setters.
- Validate user input inside setter functions.
- Return read-only values using
constwhere appropriate. - Avoid exposing internal implementation details.
Common Mistakes
Public Data Members
❌ Incorrect
1class Student 2{ 3public: 4 int age; 5};
Anyone can modify age without validation.
Correct
1class Student 2{ 3private: 4 int age; 5 6public: 7 void setAge(int a) 8 { 9 if (a >= 0) 10 age = a; 11 } 12 13 int getAge() 14 { 15 return age; 16 } 17};
Practice Problems
- Create a
Studentclass with private members. - Write getter and setter methods for all data members.
- Validate age so it cannot be negative.
- Validate salary so it cannot be less than zero.
- Create a
BankAccountclass with encapsulated balance and controlled deposit/withdraw methods.
Interview Questions
- What is encapsulation in C++?
- Why are private members used?
- What is the purpose of getter functions?
- What is the purpose of setter functions?
- Why is validation important in setter methods?
- How does encapsulation improve security?
- What is data hiding?
- Can private members be accessed directly outside the class?
- What are the advantages of encapsulation in OOP?
Summary
In this module, you learned:
- What encapsulation is and why it is important in Object-Oriented Programming.
- How private members hide implementation details.
- How getter functions provide controlled read access.
- How setter functions safely update private data.
- How validation prevents invalid data from entering an object.
- Best practices for creating secure, maintainable, and reusable C++ classes.
Next Module: Module 17: Inheritance — Single Inheritance, Multiple Inheritance, Multilevel Inheritance, Hierarchical Inheritance, Hybrid Inheritance, Access Specifiers in Inheritance, Virtual Base Classes, and Method Overriding.