1️⃣ What are Getters and Setters?
Getters and Setters are special functions in C++ that control access to private data members of a class.
- Getter (
get()
function) retrieves the value of a private variable. - Setter (
set()
function) modifies the value of a private variable with validation if needed.
🔹 Why use Getters and Setters?
✔ Encapsulation – Keeps data secure by preventing direct access.
✔ Validation – Ensures data integrity before modification.
✔ Controlled Access – Allows read-only or write-only permissions.
2️⃣ Example: Getters & Setters in C++
#include <iostream>
using namespace std;
class Student {
private:
string name;
int age;
public:
// Setter function (modifies private variables)
void setName(string n) {
name = n;
}
void setAge(int a) {
if (a > 0) // Validation
age = a;
else
cout << "Age must be positive!" << endl;
}
// Getter function (retrieves private variables)
string getName() {
return name;
}
int getAge() {
return age;
}
};
int main() {
Student s;
s.setName("Alice");
s.setAge(20);
cout << "Student Name: " << s.getName() << endl;
cout << "Student Age: " << s.getAge() << endl;
return 0;
}
🔹 Output
Student Name: Alice
Student Age: 20
✅ Encapsulation achieved – Direct access to name
and age
is not allowed.
3️⃣ this
Keyword in C++
The this
keyword in C++ is a pointer that refers to the current object inside a class.
🔹 Why use this
?
✔ Differentiates between instance variables and method parameters if they have the same name.
✔ Returns the current object’s address, allowing method chaining.
4️⃣ Example: Using this
to Differentiate Variables
#include <iostream>
using namespace std;
class Employee {
private:
string name;
int salary;
public:
// Setter with 'this' pointer
void setInfo(string name, int salary) {
this->name = name; // 'this' differentiates instance variable from parameter
this->salary = salary;
}
void display() {
cout << "Employee Name: " << name << ", Salary: " << salary << endl;
}
};
int main() {
Employee e;
e.setInfo("John", 50000);
e.display();
return 0;
}
🔹 Output
Employee Name: John, Salary: 50000
✅ Why use this->name = name;
?
- Without
this->
, C++ assumes both variables refer to the method parameter. this->
ensures we are assigning the value to the class variable.
5️⃣ Example: this
for Method Chaining
Method chaining allows multiple function calls on the same object in a single statement.
#include <iostream>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person* setName(string name) {
this->name = name;
return this; // Returns current object
}
Person* setAge(int age) {
this->age = age;
return this; // Returns current object
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Person p;
p.setName("David")->setAge(25)->display(); // Method Chaining
return 0;
}
🔹 Output
Name: David, Age: 25
✅ Method chaining improves readability by avoiding multiple statements.
6️⃣ Key Takeaways
✔ Getters & Setters provide controlled access to private data.
✔ Encapsulation prevents direct modification of sensitive data.
✔ this
pointer refers to the current object inside a class.
✔ Method chaining allows multiple function calls in a single line.
Would you like an example with constructor and this
keyword? 🚀
0 Comments