-
Notifications
You must be signed in to change notification settings - Fork 140
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
15e6071
commit 0b56ce3
Showing
4 changed files
with
195 additions
and
0 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,8 @@ | ||
{ | ||
"label": "Day 13", | ||
"position": 14, | ||
"link": { | ||
"type": "generated-index" | ||
} | ||
} | ||
|
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,82 @@ | ||
--- | ||
sidebar_position: 1 | ||
title: "Concepts of OOP - Introduction to Object-Oriented Programming" | ||
description: "In this tutorial, we will learn about the fundamentals of Object-Oriented Programming (OOP), including its core principles and benefits." | ||
sidebar_label: "Introduction to OOP" | ||
slug: concepts-of-oop-introduction | ||
--- | ||
|
||
## Introduction to Object-Oriented Programming | ||
|
||
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code to manipulate that data. OOP is designed to increase the flexibility and maintainability of programs. | ||
|
||
- ### **Core Principles of OOP:** | ||
1. **Encapsulation:** | ||
- Encapsulation is the mechanism of hiding the internal details of an object and only exposing a controlled interface. | ||
- Example: | ||
```cpp | ||
class Person { | ||
private: | ||
string name; | ||
int age; | ||
public: | ||
void setName(string n) { name = n; } | ||
string getName() { return name; } | ||
}; | ||
``` | ||
|
||
2. **Abstraction:** | ||
- Abstraction means representing essential features without including background details. | ||
- It allows focusing on what an object does instead of how it does it. | ||
- Example: | ||
```cpp | ||
class Animal { | ||
public: | ||
virtual void makeSound() = 0; // Pure virtual function | ||
}; | ||
class Dog : public Animal { | ||
public: | ||
void makeSound() override { | ||
cout << "Bark" << endl; | ||
} | ||
}; | ||
``` | ||
|
||
3. **Inheritance:** | ||
- Inheritance allows a class to inherit properties and behavior from another class. | ||
- It promotes code reusability. | ||
- Example: | ||
```cpp | ||
class Vehicle { | ||
public: | ||
string brand = "Ford"; | ||
}; | ||
class Car : public Vehicle { | ||
public: | ||
string model = "Mustang"; | ||
}; | ||
``` | ||
|
||
4. **Polymorphism:** | ||
- Polymorphism means "many forms" and allows methods to do different things based on the object it is acting upon. | ||
- Example: | ||
```cpp | ||
class Shape { | ||
public: | ||
virtual void draw() { | ||
cout << "Drawing Shape" << endl; | ||
} | ||
}; | ||
class Circle : public Shape { | ||
public: | ||
void draw() override { | ||
cout << "Drawing Circle" << endl; | ||
} | ||
}; | ||
``` | ||
|
||
- **Benefits of OOP:** | ||
- **Modularity:** Code is divided into classes and objects, making it easier to manage and understand. | ||
- **Reusability:** Classes can be reused in other programs. | ||
- **Scalability:** Easy to add new features or objects. | ||
- **Maintainability:** Easier to update and maintain code. |
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,42 @@ | ||
--- | ||
sidebar_position: 2 | ||
title: "Concepts of OOP - Classes" | ||
description: "In this tutorial, we will learn about the concept of classes in Object-Oriented Programming (OOP) with the help of examples. Classes are the blueprint for creating objects." | ||
sidebar_label: "Classes" | ||
slug: concepts-of-oop-classes | ||
--- | ||
|
||
## Classes | ||
|
||
Classes are the fundamental building blocks of Object-Oriented Programming (OOP). A class is a blueprint for creating objects, providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). | ||
|
||
- ### **Defining a Class:** | ||
- A class definition starts with the keyword `class` followed by the class name and a pair of curly braces `{}`. | ||
- Member variables and member functions are declared within the class body. | ||
- Syntax: | ||
```cpp | ||
class ClassName { | ||
public: | ||
// Member variables | ||
// Member functions | ||
}; | ||
``` | ||
|
||
- **Example:** | ||
- Defining a simple class `Car`: | ||
```cpp | ||
class Car { | ||
public: | ||
string brand; | ||
string model; | ||
int year; | ||
|
||
void displayInfo() { | ||
cout << "Brand: " << brand << endl; | ||
cout << "Model: " << model << endl; | ||
cout << "Year: " << year << endl; | ||
} | ||
}; | ||
``` | ||
|
||
In this example, `Car` is a class with three member variables (`brand`, `model`, `year`) and one member function (`displayInfo`). |
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,63 @@ | ||
--- | ||
sidebar_position: 3 | ||
title: "Concepts of OOP - Objects" | ||
description: "In this tutorial, we will learn how to create and use objects in Object-Oriented Programming (OOP) with the help of examples. Objects are instances of classes." | ||
sidebar_label: "Objects" | ||
slug: concepts-of-oop-objects | ||
--- | ||
|
||
## Objects | ||
|
||
Objects are instances of classes. They are created using the class definition and can be used to access member variables and functions. | ||
|
||
- **Creating an Object:** | ||
- Syntax: | ||
```cpp | ||
ClassName objectName; | ||
``` | ||
- Example: | ||
```cpp | ||
Car myCar; | ||
``` | ||
|
||
- **Accessing Members:** | ||
- Member variables and functions are accessed using the dot operator (`.`). | ||
- Example: | ||
```cpp | ||
myCar.brand = "Toyota"; | ||
myCar.model = "Corolla"; | ||
myCar.year = 2020; | ||
myCar.displayInfo(); | ||
``` | ||
|
||
- **Example Program:** | ||
- Creating and using an object of the `Car` class: | ||
```cpp | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class Car { | ||
public: | ||
string brand; | ||
string model; | ||
int year; | ||
|
||
void displayInfo() { | ||
cout << "Brand: " << brand << endl; | ||
cout << "Model: " << model << endl; | ||
cout << "Year: " << year << endl; | ||
} | ||
}; | ||
|
||
int main() { | ||
Car myCar; | ||
myCar.brand = "Toyota"; | ||
myCar.model = "Corolla"; | ||
myCar.year = 2020; | ||
myCar.displayInfo(); | ||
|
||
return 0; | ||
} | ||
``` | ||
|
||
In this example, an object `myCar` of the class `Car` is created, and its member variables are accessed and modified. |