Oop CPP Notes
Oop CPP Notes
Key Characteristics:
- Data Flow: Data passes openly between functions, causing a lack of data security.
- Global Data Sharing: Functions can modify global data, leading to potential inconsistencies.
Disadvantages:
- Difficult Real-World Modeling: Unlike OOP, POP doesn't naturally represent real-world entities.
Definition: OOP focuses on 'objects'-real-world entities combining both data and functions.
Key Characteristics:
- Data Hiding: Data within objects is protected, only accessible through specific methods.
Advantages:
- Data Security: Data is secured within objects, hidden from outside functions.
- Modularity: Objects form independent, modular units, simplifying maintenance.
Object:
Class:
Definition: A blueprint that defines objects, containing attributes (data) and behaviors (functions).
Example:
class Car {
private:
int speed;
public:
Encapsulation:
Definition: Wrapping data (attributes) and methods (functions) within a class for controlled access.
Advantages:
Example:
class Employee {
private:
int salary;
public:
Abstraction:
Example: A car's interface might include 'start()', 'stop()' without exposing the mechanics.
Inheritance:
Definition: New classes (child) inherit properties and methods from existing classes (parent).
Types of Inheritance:
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Advantages:
Example:
class Animal {
public:
public:
Polymorphism:
Types:
Example:
class Animal {
public:
public:
Types:
Characteristics:
- No return type.
Example:
class Box {
private:
int length;
public:
Box() { length = 0; }
Box(int l) { length = l; }
Destructor:
Definition: Special member function called when an object goes out of scope or is destroyed,
cleaning up resources.
Example:
class Box {
public:
Definition: Shared variables among all objects of the class. Only one copy exists.
Example:
class Counter {
public:
int Counter::count = 0;
Definition: Functions that can only access static data members and are called using the class name.
Example:
class Counter {
public:
Inline Functions:
Definition: Functions expanded in-place where called, reducing function call overhead.
Example:
return a + b;
Function Overloading:
Definition: Multiple functions with the same name but different parameter types or numbers.
Example:
Default Arguments:
Example: