Hello! In this lesson, we're revisiting Encapsulation, Private Attributes, and Private Methods in Object-Oriented Programming (OOP). Imagine encapsulation as an invisible fence safeguarding a garden from outside interference, keeping data and methods safe within. Within this garden, certain plants (Private Attributes
and Private Methods
) are only for the gardener's eyes. These are crucial for making your classes more robust and secure!
Encapsulation in OOP wraps up data and methods into a class
. This organizational approach tidies the code and reinforces security. If you were to code a multiplayer game, for example, you could create a Player
class, encapsulating data (health
, armor
, stamina
) and methods (receiveDamage
, shieldHit
, restoreHealth
).
C++1#include <iostream> 2using namespace std; 3 4class Player { 5public: 6 // Constructor 7 Player(int health, int armor, int stamina) : 8 health(health), armor(armor), stamina(stamina) {} 9 10 // Public methods 11 void receiveDamage(int damage) { 12 health -= damage; // Reduce health 13 } 14 15 void shieldHit(int armorDecrease) { 16 armor -= armorDecrease; // Decrease armor 17 } 18 19 void restoreHealth(int healthIncrease) { 20 health += healthIncrease; // Restore health 21 } 22 23private: 24 // Private attributes 25 int health; 26 int armor; 27 int stamina; 28}; 29 30int main() { 31 Player player(100, 50, 77); 32 player.receiveDamage(10); 33 player.shieldHit(5); 34 player.restoreHealth(15); 35 return 0; 36}
Now, player
is an instance of the Player
class on which you can call methods.
In C++, the private
access specifier designates attributes or methods as private. Private members are only accessible within the same class.
C++1#include <iostream> 2using namespace std; 3 4class PrivateExample { 5public: 6 PrivateExample() { 7 publicAttribute = "Public"; 8 privateAttribute = "Private"; 9 } 10 11 // Method to print attributes (for demonstration purposes) 12 void printAttributes() const { 13 cout << "Public Attribute: " << publicAttribute << endl; 14 // This line is fine inside the class method 15 cout << "Private Attribute: " << privateAttribute << endl; 16 } 17 18 string publicAttribute; // Public attribute 19 20private: 21 string privateAttribute; // Private attribute 22}; 23 24int main() { 25 PrivateExample example; 26 example.printAttributes(); 27 // This line is fine because publicAttribute is public 28 cout << example.publicAttribute << endl; 29 // Uncommenting the following line would cause an error because privateAttribute is private 30 // cout << example.privateAttribute << endl; 31 return 0; 32}
Private attributes and methods are inaccessible directly from an instance in non-member functions. This arrangement helps maintain integrity. Moreover, in C++, if no access specifier is provided, methods and attributes are private by default; this default behavior helps ensure that class internals are not accidentally exposed.
Private Attributes, which can only be altered via class methods, limit outside interference. For instance, a BankAccount
class might feature a balance
private attribute that one could change only through deposits or withdrawals.
C++1#include <iostream> 2using namespace std; 3 4class BankAccount { 5public: 6 // Constructor 7 BankAccount(int accountNumber, double balance) : 8 accountNumber(accountNumber), balance(balance) {} 9 10 // Public method to deposit money 11 void deposit(double amount) { 12 balance += amount; // Deposit money 13 } 14 15 // Method to print balance (for demonstration purposes) 16 void printBalance() const { 17 cout << "Balance: " << balance << endl; 18 } 19 20private: 21 // Private attributes 22 int accountNumber; 23 double balance; 24}; 25 26int main() { 27 BankAccount bankAccount(1234, 100.0); 28 bankAccount.printBalance(); 29 // Uncommenting the following line would cause an error because balance is private 30 // cout << bankAccount.balance << endl; 31 return 0; 32}
Here, balance
is private, thus ensuring the integrity of the account balance.
Like private attributes, private methods are accessible only within their class. Here's an example:
C++1#include <iostream> 2using namespace std; 3 4class BankAccount { 5public: 6 // Constructor 7 BankAccount(int accountNumber, double balance) : 8 accountNumber(accountNumber), balance(balance) {} 9 10 // Public method calling the private method 11 void addYearlyInterest() { 12 addInterest(0.02); // Adds 2% interest 13 } 14 15 // Method to print balance (for demonstration purposes) 16 void printBalance() const { 17 cout << "Balance: " << balance << endl; 18 } 19 20private: 21 // Private method 22 void addInterest(double interestRate) { 23 balance += balance * interestRate; // Calculation of interest 24 } 25 26 // Private attributes 27 int accountNumber; 28 double balance; 29}; 30 31int main() { 32 BankAccount bankAccount(1234, 100.0); 33 bankAccount.addYearlyInterest(); 34 bankAccount.printBalance(); 35 // Uncommenting the following line would cause an error because addInterest is private 36 // bankAccount.addInterest(0.1); 37 return 0; 38}
Here, addYearlyInterest
is a public method that calls the private method addInterest
.
Great job refreshing your understanding of encapsulation, private attributes, and private methods concepts in C++! Correctly understanding and applying these foundational principles of OOP make your code concise, robust, and secure.
Coming up next is hands-on practice. Keep up the good work – exciting exercises are just around the corner!