-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef4ec67
commit 55f879c
Showing
7 changed files
with
403 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
--- | ||
sidebar_position: 2 | ||
title: "Class and Object" | ||
description: Classes are user-defined data types that encapsulate data (attributes) and functions (methods) into a single unit. They serve as blueprints for creating objects." | ||
sidebar_label: "Class and Object" | ||
slug: Class and Object | ||
--- | ||
|
||
### Class | ||
|
||
In OOP, a **class** is a blueprint or template for creating objects. It defines a blueprint for how an object should be structured (its attributes) and what actions it can perform (its methods). Think of a class as a blueprint for a house: it specifies what the house will look like and what features it will have. | ||
|
||
**Example:** | ||
|
||
```cpp | ||
// Example of a class in C++ | ||
#include <iostream> | ||
using namespace std; | ||
// Defining a class named Car | ||
class Car { | ||
private: // Private members are only accessible within the class | ||
string brand; | ||
int year; | ||
public: // Public members are accessible from outside the class | ||
// Constructor - initialize object data | ||
Car(string b, int y) { | ||
brand = b; | ||
year = y; | ||
} | ||
// Method to display details of the car | ||
void displayDetails() { | ||
cout << "Brand: " << brand << ", Year: " << year << endl; | ||
} | ||
}; | ||
``` | ||
|
||
**Explanation:** | ||
|
||
- **Class Definition**: In the example above, `Car` is a class that encapsulates the concept of a car. It has **attributes** (`brand` and `year`) and **methods** (`displayDetails()`). | ||
|
||
- **Private Members**: `brand` and `year` are private members of the class, which means they can only be accessed within the `Car` class itself. This is an example of **encapsulation**, where the internal state of an object is protected from outside interference. | ||
|
||
- **Public Members**: `Car(string b, int y)` is a **constructor**. Constructors are special methods that are automatically called when an object of the class is created. They initialize the object's state. In this case, it initializes the `brand` and `year` attributes based on the values provided during object creation. | ||
|
||
- **Methods**: `displayDetails()` is a method that belongs to the `Car` class. It is a **public method** because it's declared under the `public` section of the class. This method displays the details of the car (its `brand` and `year`). | ||
|
||
### Object | ||
|
||
An **object** is an instance of a class. It is a concrete entity based on the blueprint provided by the class. Using our previous analogy, if a class is a blueprint of a house, an object is an actual house built according to that blueprint. | ||
|
||
**Example:** | ||
|
||
```cpp | ||
int main() { | ||
// Creating objects of the Car class | ||
Car car1("Toyota", 2022); | ||
Car car2("BMW", 2023); | ||
// Using object methods to display details | ||
car1.displayDetails(); // Output: Brand: Toyota, Year: 2022 | ||
car2.displayDetails(); // Output: Brand: BMW, Year: 2023 | ||
return 0; | ||
} | ||
``` | ||
|
||
**Explanation:** | ||
|
||
- **Object Creation**: `car1` and `car2` are objects of the `Car` class. They are created using the constructor `Car(string b, int y)`. When `car1` is created, it initializes `brand` as "Toyota" and `year` as 2022. Similarly, `car2` initializes `brand` as "BMW" and `year` as 2023. | ||
|
||
- **Method Invocation**: `car1.displayDetails()` and `car2.displayDetails()` are method calls on the objects `car1` and `car2`, respectively. These calls invoke the `displayDetails()` method defined in the `Car` class, which outputs the details (`brand` and `year`) of each car object. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
--- | ||
sidebar_position: 3 | ||
title: "Encapsulation in OOPS" | ||
description: "Encapsulation is one of the fundamental concepts in Object-Oriented Programming (OOP) that facilitates the bundling of data (attributes) and methods (functions) that operate on the data into a single unit (class). " | ||
sidebar_label: "Encapsulation in OOPS" | ||
slug: Encapsulation in OOPS | ||
--- | ||
|
||
Encapsulation is one of the fundamental concepts in Object-Oriented Programming (OOP) that facilitates the bundling of data (attributes) and methods (functions) that operate on the data into a single unit (class). It is often described as the mechanism that restricts direct access to some of an object's components, usually to increase modularity and flexibility of the code. | ||
|
||
### Key Aspects of Encapsulation: | ||
|
||
1. **Data Hiding**: Encapsulation hides the internal state (data members) of an object from the outside world. This means that the data within an object is not directly accessible to other classes or functions, which prevents unintended or unauthorized access and modification. | ||
|
||
2. **Access Control**: By using access specifiers (`public`, `private`, `protected`), encapsulation controls how the data can be accessed and modified. Typically: | ||
- **Private members** are accessible only within the same class. They are hidden from the outside world. | ||
- **Public members** are accessible from outside the class. They define the interface through which the outside world interacts with the object. | ||
|
||
3. **Information Hiding**: Encapsulation enables information hiding, which means that the internal details or implementation of an object are hidden and inaccessible to users of the object. Only the interface (public methods) is exposed, which abstracts away the complex implementation details. | ||
|
||
### Example Demonstrating Encapsulation: | ||
|
||
Let's revisit the `Car` class example and demonstrate encapsulation: | ||
|
||
```cpp | ||
#include <iostream> | ||
#include <string> | ||
using namespace std; | ||
|
||
// Example of a class demonstrating encapsulation | ||
class Car { | ||
private: | ||
string brand; // Private member variable | ||
int year; // Private member variable | ||
|
||
public: | ||
// Constructor to initialize private variables | ||
Car(string b, int y) { | ||
brand = b; | ||
year = y; | ||
} | ||
|
||
// Public method to display details | ||
void displayDetails() { | ||
cout << "Brand: " << brand << ", Year: " << year << endl; | ||
} | ||
|
||
// Getter method for brand (Accesses private member) | ||
string getBrand() { | ||
return brand; | ||
} | ||
|
||
// Setter method for year (Modifies private member) | ||
void setYear(int y) { | ||
year = y; | ||
} | ||
}; | ||
|
||
int main() { | ||
// Creating an object of the Car class | ||
Car car1("Toyota", 2022); | ||
|
||
// Accessing and modifying private members using public methods | ||
car1.displayDetails(); // Output: Brand: Toyota, Year: 2022 | ||
|
||
// Accessing private member brand using getter method | ||
cout << "Brand of car1: " << car1.getBrand() << endl; // Output: Brand of car1: Toyota | ||
|
||
// Modifying private member year using setter method | ||
car1.setYear(2023); | ||
car1.displayDetails(); // Output: Brand: Toyota, Year: 2023 | ||
|
||
return 0; | ||
} | ||
``` | ||
|
||
### Explanation: | ||
|
||
- **Private Members**: In the `Car` class, `brand` and `year` are declared as private members. They are not accessible directly from outside the class, ensuring data hiding and encapsulation. | ||
|
||
- **Public Methods**: `displayDetails()`, `getBrand()`, and `setYear()` are public methods of the `Car` class. They provide controlled access to the private members: | ||
- `displayDetails()` method allows displaying the details of the car object (`brand` and `year`). | ||
- `getBrand()` method is a getter that returns the value of the private member `brand`, allowing controlled access to retrieve the brand information. | ||
- `setYear(int y)` method is a setter that modifies the value of the private member `year`, allowing controlled modification of the year information. | ||
|
||
- **Usage in `main()` Function**: In `main()`, we create an object `car1` of the `Car` class and demonstrate how to access and modify the private members (`brand` and `year`) using the public methods (`getBrand()` and `setYear()`). | ||
|
||
### Benefits of Encapsulation: | ||
|
||
- **Modularity**: Encapsulation promotes modularity by dividing the program into smaller, manageable parts (classes) with clearly defined interfaces. | ||
|
||
- **Security**: Data hiding ensures that sensitive data is not accessible directly, reducing the risk of accidental modification or misuse. | ||
|
||
- **Flexibility and Maintainability**: Encapsulation allows the internal implementation details of an object to change without affecting other parts of the program, as long as the public interface remains unchanged. | ||
|
||
- **Code Reusability**: Encapsulated classes can be reused in different parts of a program or in different programs, promoting code reusability. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
--- | ||
sidebar_position: 4 | ||
title: "Inheritance in OOPS" | ||
description: "Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows one class (subclass or derived class) to inherit properties and behaviors from another class (superclass or base class)" | ||
sidebar_label: "Inheritance in OOPS" | ||
slug: Inheritance in OOPS | ||
--- | ||
|
||
Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows one class (subclass or derived class) to inherit properties and behaviors from another class (superclass or base class). This promotes code reuse and facilitates the creation of hierarchical relationships between classes. | ||
|
||
### Key Aspects of Inheritance: | ||
|
||
1. **Base Class (Superclass)**: The class whose properties and behaviors are inherited by another class is called the base class or superclass. | ||
|
||
2. **Derived Class (Subclass)**: The class that inherits properties and behaviors from another class is called the derived class or subclass. | ||
|
||
3. **Extending Functionality**: Inheritance allows the subclass to extend or modify the behavior of the superclass without altering the original implementation of the superclass. | ||
|
||
4. **Types of Inheritance**: | ||
- **Single Inheritance**: A subclass inherits from only one superclass. | ||
- **Multiple Inheritance**: A subclass inherits from multiple superclasses (supported in some languages like C++). | ||
- **Multilevel Inheritance**: A subclass inherits from another subclass, forming a chain of inheritance. | ||
- **Hierarchical Inheritance**: Multiple subclasses inherit from the same superclass. | ||
|
||
### Example Demonstrating Inheritance: | ||
|
||
Let's illustrate inheritance with a practical example in C++: | ||
|
||
```cpp | ||
#include <iostream> | ||
#include <string> | ||
using namespace std; | ||
|
||
// Base class (Superclass) | ||
class Vehicle { | ||
protected: // Protected access specifier | ||
string brand; | ||
|
||
public: | ||
// Constructor of the base class | ||
Vehicle(string b) { | ||
brand = b; | ||
} | ||
|
||
// Method to display information | ||
void displayInfo() { | ||
cout << "Brand: " << brand << endl; | ||
} | ||
}; | ||
|
||
// Derived class (Subclass) inheriting from Vehicle | ||
class Car : public Vehicle { | ||
private: | ||
int year; | ||
|
||
public: | ||
// Constructor of the derived class | ||
Car(string b, int y) : Vehicle(b) { | ||
year = y; | ||
} | ||
|
||
// Method to display car details (overrides displayInfo() method) | ||
void displayDetails() { | ||
cout << "Car Details:" << endl; | ||
displayInfo(); // Accessing base class method | ||
cout << "Year: " << year << endl; | ||
} | ||
}; | ||
|
||
int main() { | ||
// Creating an object of the derived class Car | ||
Car car1("Toyota", 2022); | ||
|
||
// Using methods of the derived class | ||
car1.displayDetails(); | ||
|
||
return 0; | ||
} | ||
``` | ||
|
||
### Explanation: | ||
|
||
- **Base Class `Vehicle`**: The `Vehicle` class is defined as a base class with a `brand` attribute and a method `displayInfo()` to display the brand. | ||
|
||
- **Derived Class `Car`**: The `Car` class inherits from `Vehicle` using `public` inheritance (`class Car : public Vehicle`). This means that `Car` inherits all the public and protected members of `Vehicle`. | ||
|
||
- **Constructor Initialization**: In the `Car` constructor (`Car(string b, int y) : Vehicle(b)`), the `Vehicle` constructor is called explicitly to initialize the `brand` attribute inherited from `Vehicle`. | ||
|
||
- **Method Overriding**: The `displayDetails()` method in `Car` overrides the `displayInfo()` method from `Vehicle`. It calls the `displayInfo()` method of the base class (`Vehicle`) using `displayInfo()` and then displays additional information (`year` of the car). | ||
|
||
- **Usage in `main()` Function**: In `main()`, an object `car1` of class `Car` is created with the brand "Toyota" and year 2022. It demonstrates how the derived class (`Car`) inherits and extends functionality from the base class (`Vehicle`) and uses both inherited and new methods (`displayInfo()` and `displayDetails()`). | ||
|
||
### Benefits of Inheritance: | ||
|
||
- **Code Reuse**: Inheritance promotes code reuse by allowing subclasses to inherit existing functionality from their superclass, reducing redundancy and improving maintainability. | ||
|
||
- **Hierarchy**: It enables the creation of hierarchical relationships among classes, reflecting real-world relationships and enhancing the organization of code. | ||
|
||
- **Polymorphism**: Inheritance facilitates polymorphism, where objects of different classes can be treated as objects of their superclass, allowing for more flexible and modular code. | ||
|
||
- **Extensibility**: Subclasses can extend or modify the behavior of their superclass, adding new features or refining existing ones without modifying the base class. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
sidebar_position: 1 | ||
title: "OOPS in cpp" | ||
description: "Object-Oriented Programming (OOP) in C++ revolves around the concept of organizing your code into objects that interact with each other." | ||
sidebar_label: "OOPS Introduction" | ||
slug: OOPS in CPP | ||
--- | ||
|
||
Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of objects. Here are the key concepts of OOP: | ||
|
||
1. **Objects**: Objects are instances of classes that encapsulate data (attributes) and behaviors (methods) into a single entity. | ||
|
||
2. **Class**: A class is a blueprint for creating objects. It defines the attributes and methods that all objects of the class will have. | ||
|
||
3. **Encapsulation**: Encapsulation refers to bundling the data (attributes) and methods that operate on the data into a single unit (class). It allows data hiding and access control to protect the internal state of objects. | ||
|
||
4. **Inheritance**: Inheritance allows one class (subclass or derived class) to inherit properties and behaviors from another class (superclass or base class). It promotes code reuse and hierarchical relationships between classes. | ||
|
||
5. **Polymorphism**: Polymorphism means the ability of objects to be treated as instances of their parent class or their actual class. It allows methods to be defined in multiple forms (method overloading and method overriding). | ||
|
||
These concepts collectively provide a structured approach to software design, promoting modularity, reusability, and easier maintenance of code. OOP is widely used across various programming languages, including C++, Java, Python, and others, due to its advantages in managing complexity and improving code organization. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
--- | ||
sidebar_position: 5 | ||
title: "Polymorphism in OOPS" | ||
description: "Polymorphism is a core concept in Object-Oriented Programming (OOP) that allows objects of different classes to be treated as objects of a common superclass." | ||
sidebar_label: "Polymorphism in OOPS" | ||
slug: Polymorphism in OOPS | ||
--- | ||
|
||
Polymorphism is a core concept in Object-Oriented Programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. It enables methods to be defined in multiple forms, allowing them to behave differently based on the object they are invoked upon. Polymorphism enhances flexibility and extensibility in software design by promoting code reuse and allowing code to be written that can work with objects of multiple types. | ||
|
||
### Types of Polymorphism: | ||
|
||
1. **Compile-time Polymorphism (Static Binding)**: | ||
- **Function Overloading**: This occurs when multiple functions in the same scope have the same name but different parameters. The correct function is selected during compile-time based on the number and types of arguments passed. | ||
|
||
```cpp | ||
// Example of function overloading | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class MathOperations { | ||
public: | ||
int add(int a, int b) { | ||
return a + b; | ||
} | ||
|
||
double add(double a, double b) { | ||
return a + b; | ||
} | ||
}; | ||
|
||
int main() { | ||
MathOperations math; | ||
|
||
cout << math.add(5, 3) << endl; // Output: 8 (int version of add is called) | ||
cout << math.add(2.5, 3.7) << endl; // Output: 6.2 (double version of add is called) | ||
|
||
return 0; | ||
} | ||
``` | ||
2. **Run-time Polymorphism (Dynamic Binding)**: | ||
- **Function Overriding**: This occurs when a derived class provides a specific implementation of a method that is already defined in its base class. The correct method is selected at runtime based on the object's actual type. | ||
```cpp | ||
// Example of function overriding | ||
#include <iostream> | ||
using namespace std; | ||
// Base class | ||
class Animal { | ||
public: | ||
// Virtual function | ||
virtual void makeSound() { | ||
cout << "Animal makes a sound" << endl; | ||
} | ||
}; | ||
// Derived class | ||
class Dog : public Animal { | ||
public: | ||
// Overridden function | ||
void makeSound() override { | ||
cout << "Dog barks" << endl; | ||
} | ||
}; | ||
int main() { | ||
Animal* animal; // Pointer to base class | ||
Dog dog; // Object of derived class | ||
animal = &dog; // Assigning address of derived class object to base class pointer | ||
// Virtual function call | ||
animal->makeSound(); // Output: Dog barks | ||
return 0; | ||
} | ||
``` | ||
|
||
### Explanation: | ||
|
||
- **Function Overloading**: In the `MathOperations` class example, `add()` is overloaded with two versions—one for `int` parameters and another for `double` parameters. The compiler determines which version of `add()` to call based on the arguments passed at compile-time. | ||
|
||
- **Function Overriding**: In the `Animal` and `Dog` classes example, `makeSound()` is a virtual function in the base class `Animal` and is overridden in the derived class `Dog`. The `makeSound()` method of `Dog` is invoked through a base class pointer pointing to a `Dog` object. This demonstrates run-time polymorphism where the method called is determined dynamically at runtime based on the actual type of the object. | ||
|
||
### Benefits of Polymorphism: | ||
|
||
- **Flexibility**: Polymorphism allows the same code to be used with different types of objects, promoting code reuse and reducing redundancy. | ||
|
||
- **Extensibility**: It enables adding new classes and methods without modifying existing code, facilitating easy extension and maintenance of software systems. | ||
|
||
- **Modularity**: Polymorphism supports modular programming by encapsulating related behaviors within objects and allowing them to be treated uniformly through their common interfaces. | ||
|
||
- **Dynamic Behavior**: Run-time polymorphism allows for dynamic behavior where the specific implementation of a method is determined based on the type of object at runtime, rather than compile-time. | ||
|
Oops, something went wrong.