ch 2 oops
1️⃣ Encapsulation (C++)
🔹 Definition
Encapsulation means wrapping data (variables) and methods (functions) together into a single unit called a class, and restricting direct access to data.
👉 Achieved using:
-
private -
protected -
public
🔹 Why Needed?
-
Data security
-
Prevents unauthorized access
-
Controls modification
🔹 Example in C++
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance; // Hidden data
public:
void deposit(double amount) {
balance += amount;
}
double getBalance() {
return balance;
}
};
int main() {
BankAccount obj;
obj.deposit(5000);
cout << obj.getBalance();
}
✅ balance cannot be accessed directly.
This is data hiding + encapsulation.
2️⃣ Data Abstraction (C++)
🔹 Definition
Abstraction means hiding internal implementation details and showing only essential features.
👉 Real Example:
ATM machine — you press buttons, but don’t know internal logic.
🔹 Achieved by:
-
Classes
-
Access specifiers
-
Abstract classes (using virtual functions)
🔹 Example
class Shape {
public:
virtual void area() = 0; // Pure virtual function
};
User only knows area() exists, not how it works internally.
3️⃣ Concept of Object
An Object is an instance of a class.
Every object has:
🔹 1. State
Represents data/attributes of object.
Example:
int speed;
string color;
🔹 2. Behavior
Functions that object performs.
Example:
void accelerate();
void brake();
🔹 3. Identity
Unique name/reference of object in memory.
Example:
Car c1, c2;
c1 and c2 are different identities.
Object Representation Diagram
Object: Car c1
-----------------
State: color=Red
speed=80
-----------------
Behavior:
accelerate()
brake()
-----------------
Identity:
Memory Address
4️⃣ Classes
🔹 Definition
A Class is a blueprint or template for creating objects.
class Student {
public:
int roll;
string name;
};
5️⃣ Identifying Classes and Candidate Classes
When designing software:
Step 1: Identify Nouns from Problem Statement
Example:
Library Management System
Nouns:
-
Book
-
Student
-
Librarian
-
Issue
-
Return
These are Candidate Classes.
Step 2: Filter Relevant Classes
Final Classes:
-
Book
-
Student
-
Library
6️⃣ Attributes and Services
🔹 Attributes
Data members of class.
Example:
int roll;
string name;
🔹 Services
Functions provided by class.
Example:
void display();
void input();
7️⃣ Access Modifiers in C++
| Modifier | Accessibility |
|---|---|
| private | Inside class only |
| protected | Inside class + derived class |
| public | Anywhere |
Example
class Demo {
private:
int a;
protected:
int b;
public:
int c;
};
8️⃣ Static Members of a Class
🔹 Static Data Member
Shared among all objects.
class Student {
public:
static int count;
};
int Student::count = 0;
🔹 Static Function
static void showCount() {
cout << count;
}
👉 Accessed using:
Student::showCount();
9️⃣ Instances
An Instance is another word for Object.
Student s1; // Instance
Student s2; // Another instance
Each instance has separate data.
🔟 Message Passing
Objects communicate by calling functions.
Example:
s1.display();
Here:
-
s1sends message todisplay()function.
1️⃣1️⃣ Construction of Objects (Constructor)
🔹 Definition
Special function with same name as class.
Used to initialize object.
🔹 Example
class Student {
public:
Student() {
cout << "Constructor Called";
}
};
Types:
-
Default Constructor
-
Parameterized Constructor
-
Copy Constructor
1️⃣2️⃣ Destruction of Objects (Destructor)
🔹 Definition
Special function with ~ symbol.
Called automatically when object is destroyed.
class Student {
public:
~Student() {
cout << "Destructor Called";
}
};
Used for:
-
Memory cleanup
-
File closing
-
Resource releasing
🔥 Full C++ Example (All Concepts Together)
#include <iostream>
using namespace std;
class Student {
private:
int roll;
public:
static int count;
Student(int r) {
roll = r;
count++;
cout << "Constructor called\n";
}
void display() {
cout << "Roll: " << roll << endl;
}
~Student() {
cout << "Destructor called\n";
}
};
int Student::count = 0;
int main() {
Student s1(101);
Student s2(102);
s1.display();
s2.display();
cout << "Total Students: " << Student::count;
}
Comments
Post a Comment