Information Hiding
Introduction
Information hiding restricts access to an object's internal details, exposing only what is necessary through a public interface.
Definition
Information Hiding: Concealing implementation details; exposing only the interface.
C++ Code Example
#include
using namespace std;
class BankAccount {
double balance; // hidden
public:
void deposit(double a){ balance+=a; }
double getBalance(){ return balance; }
}; Important Questions
Short Questions
- What is information hiding?
- Difference hiding vs encapsulation?
- Why hide data?
- What is public interface?
Long Questions
- Explain information hiding with example.
- Discuss benefits of information hiding.
MCQ Practice
1. Information hiding is achieved by:
- Access specifiers
- Inheritance
- Templates
- Virtual functions
Answer: Access specifiers.
2. Hidden members are usually:
- private
- public
- protected
- static
Answer: private.
3. Public interface provides:
- Controlled access
- Direct data access
- Nothing
- Compilation
Answer: Controlled access.
4. Information hiding improves:
- Security
- Speed
- Memory
- None
Answer: Security.
5. Encapsulation is the mechanism for:
- Information hiding
- Inheritance
- Polymorphism
- Templates
Answer: Information hiding.
Quick Revision
- Hide data; expose interface.
- Improves security and maintainability.