๐ Scenario: Imagine you are creating a simple bank account system. You want to keep the account balance safe so that it cannot be changed directly from outside the account. This is called data hiding.
๐ฏ Goal: You will create a class called BankAccount that hides the balance inside it. You will add a way to check the balance safely and a way to add money to the account.
๐ What You'll Learn
Create a class called BankAccount with a private variable balance of type double.
Add a public method getBalance() that returns the current balance.
Add a public method deposit(double amount) that adds money to the balance.
Use data hiding to prevent direct access to balance from outside the class.
๐ก Why This Matters
๐ Real World
Data hiding is used in real bank systems to protect account balances from accidental or unauthorized changes.
๐ผ Career
Understanding data hiding is important for writing secure and reliable code in software development jobs.
Progress0 / 4 steps
1
Create the BankAccount class with a private balance
Create a class called BankAccount with a private variable balance of type double initialized to 0.0.
C++
Hint
Use the private: keyword to hide the balance variable inside the class.
2
Add a public method to get the balance
Add a public method called getBalance() that returns the value of balance. Use public: to make the method accessible outside the class.
C++
Hint
The getBalance() method should return the private balance variable.
3
Add a public method to deposit money
Add a public method called deposit that takes a double amount parameter and adds it to balance.
C++
Hint
The deposit method should add the amount to the private balance.
4
Create an object and test the methods
Create an object called myAccount of class BankAccount. Use deposit(100.0) to add money and then print the balance using getBalance().
C++
Hint
Use std::cout to print the balance returned by getBalance().
Practice
(1/5)
1. What is the main purpose of private members in a C++ class?
easy
A. To hide data from outside access and protect it
B. To allow direct access from anywhere
C. To make data global
D. To speed up the program execution
Solution
Step 1: Understand the role of private members
Private members in a class are not accessible from outside the class, which means they are hidden from direct access.
Step 2: Recognize the purpose of data hiding
Data hiding protects important data by restricting access, making the program safer and easier to manage.
Final Answer:
To hide data from outside access and protect it -> Option A
Quick Check:
Data hiding = private members protect data [OK]
Hint: Private means hidden from outside, protecting data [OK]
Common Mistakes:
Thinking private allows direct access
Confusing private with public
Assuming private makes data global
2. Which of the following is the correct syntax to declare a private integer variable named age inside a class?
easy
A. private: int age;
B. int private age;
C. int age private;
D. private int age;
Solution
Step 1: Recall C++ access specifier syntax
In C++, access specifiers like private are followed by a colon and then the members are declared.
Step 2: Identify correct declaration
The correct way is to write private: then declare variables like int age; inside the class.
Final Answer:
private: int age; -> Option A
Quick Check:
Access specifier ends with colon before members [OK]
Hint: Use 'private:' label before variables inside class [OK]
Common Mistakes:
Writing 'private' before type without colon
Placing 'private' after variable name
Using 'int private' which is invalid
3. Consider this code snippet:
class Box {
private:
int length;
public:
void setLength(int len) { length = len; }
int getLength() { return length; }
};
int main() {
Box b;
b.setLength(10);
std::cout << b.length;
return 0;
}
What will happen when this code is compiled?
medium
A. It will print 10
B. It will print 0
C. Compilation error due to accessing private member
D. Runtime error
Solution
Step 1: Identify access to private member
The code tries to access b.length directly in main(), but length is private.
Step 2: Understand C++ access rules
Private members cannot be accessed outside the class, so this causes a compilation error.
Final Answer:
Compilation error due to accessing private member -> Option C
Quick Check:
Private members cause compile error if accessed outside [OK]
Hint: Private members cannot be accessed directly outside class [OK]
Common Mistakes:
Assuming private members print default value
Expecting runtime error instead of compile error
Trying to access private members directly
4. Find the error in this class definition:
class Person {
int age;
public:
void setAge(int a) { age = a; }
int getAge() { return age; }
};
int main() {
Person p;
p.age = 25;
std::cout << p.getAge();
return 0;
}
medium
A. No error, code runs fine
B. Error: 'age' is private and accessed directly
C. Error: Missing semicolon after class
D. Error: getAge() should be private
Solution
Step 1: Check member access in main()
The code tries to assign p.age = 25; but age is private by default in class.
Step 2: Understand default access in classes
In C++, class members are private by default, so direct access outside class causes error.
Final Answer:
Error: 'age' is private and accessed directly -> Option B
Quick Check:
Private members cannot be accessed directly outside class [OK]
Hint: Class members default to private, cannot access directly [OK]
Common Mistakes:
Assuming no error without explicit private label
Thinking getAge() must be private
Missing semicolon is not the issue here
5. You want to protect a class's data but allow controlled access. Which design correctly applies data hiding and access control?