OOPs in C++
1. Function Overloading
- Function overloading is a concept where multiple functions can have the same name but with different parameters.
- These functions perform similar tasks but with different inputs or different numbers of parameters.
- The compiler distinguishes overloaded functions by their signature (number and types of parameters).
- Overloaded functions are distinguished by their arguments but not their return type.
- Function overloading helps in increasing code readability and reusability.
2. Function Overriding
- Function overriding allows a derived class to provide a specific implementation of a function that is already defined
in the base class.
- The function in the base class must be declared as virtual, and it should have the same signature in the derived
class.
- Function overriding is a key feature of polymorphism in C++.
- This mechanism allows the derived class to change the behavior of the inherited function.
3. Operator Overloading
- Operator overloading allows operators to be used with user-defined types (like objects of classes).
- It is used to define how operators such as +, -, *, etc., behave with objects of a class.
- Operator overloading enables intuitive syntax and code readability by allowing familiar operators to be applied to
objects.
- It is done by defining a function that corresponds to the operator.
4. Constructor Overloading
- Constructor overloading refers to defining multiple constructors with the same name but with different parameter
lists.
- This allows an object to be initialized in different ways, depending on the arguments passed.
- Constructor overloading enhances flexibility by providing different ways to initialize an object.
- It enables creating objects with different initial values or behaviors.
5. Inheritance and Types
- Inheritance allows one class (derived class) to inherit properties and methods from another class (base class).
- It helps in code reusability and creating hierarchical relationships between classes.
- There are different types of inheritance:
1. **Single Inheritance**: A derived class inherits from a single base class.
2. **Multiple Inheritance**: A derived class inherits from more than one base class.
3. **Multilevel Inheritance**: A class inherits from a derived class, creating a chain of inheritance.
4. **Hierarchical Inheritance**: Multiple classes inherit from a single base class.
5. **Hybrid Inheritance**: A combination of more than one type of inheritance.
6. OOPs vs POPs
- **OOPs (Object-Oriented Programming)** and **POPs (Procedural-Oriented Programming)** are two different
paradigms.
- **OOPs** is based on **objects and classes**; it uses inheritance, polymorphism, encapsulation, and abstraction.
- **POPs** is based on **functions and procedures**; it focuses on sequences of steps and function calls.
- Key differences:
- OOP focuses on **data and objects**, while POP focuses on **functions**.
- OOP provides better **security** and **code reuse** through inheritance and polymorphism.
- POP is more suited for small, simple tasks, while OOP is better for complex, large applications.
7. Encapsulation
- **Encapsulation** refers to the bundling of data (variables) and methods (functions) that operate on the data
within a single unit (class).
- It hides the internal implementation details and only exposes necessary parts via public methods (getters and
setters).
- It enhances security by restricting access to certain parts of the object.
- It also improves code maintainability and readability.
8. Class vs Object
- A **class** is a blueprint for creating objects. It defines data and functions for the objects but doesn't allocate
memory until an object is created.
- An **object** is an instance of a class. It contains real data and uses the class's functions.
- **Class** is like a **template**, and an **object** is like a **real entity**.
- Objects are created from classes and occupy memory, while classes are just descriptions of the data and behavior.
9. Access Modifiers
- **Access modifiers** determine the visibility and accessibility of class members (variables and functions).
- There are three types of access modifiers in C++:
1. **public**: Members are accessible from outside the class.
2. **private**: Members are accessible only within the class.
3. **protected**: Members are accessible within the class and derived classes.
- Access modifiers are used to achieve **data hiding** and **encapsulation**.
10. Virtual Functions
- **Virtual functions** are functions that can be overridden in derived classes.
- They enable **runtime polymorphism**, meaning the function that gets called is determined at runtime based on
the actual type of the object.
- Declared using the **virtual** keyword in the base class.
- Virtual functions are useful for achieving dynamic method dispatch, where the correct function is called even when
accessed through a base class pointer.
11. Inline Function
- An **inline function** is a function that is **expanded in place** at the point of call, instead of performing a
function call.
- This helps reduce the overhead of function calls, especially for small functions.
- The `inline` keyword is used to define inline functions.
- Inline functions are recommended for small, frequently used functions, but using them excessively can increase
the size of the code.