Skip to content

Commit 51810f2

Browse files
authored
Add OOP Content (#105)
As Described.
2 parents d1906d2 + 93e4099 commit 51810f2

File tree

1 file changed

+239
-20
lines changed

1 file changed

+239
-20
lines changed

docs/Concepts/OOP/OOP.md

Lines changed: 239 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,252 @@
11
---
22
layout: default
3-
title: OOP
4-
description: "Warning: The following page is currently under construction, find more about the details in future patches, or if you choose to add in the article see info on the bottom of the page."
3+
title: OOOP
4+
description: An article dicussing Object Oriented Programming.
55
nav_order: 1
6-
parent: Concepts
6+
parent: OOP
7+
grand_parent: Concepts
78
has_children: false
8-
include_programming_language_switch_script: true
99
---
1010

1111
{{ page.title }}
1212
======================
1313

14-
{% include under_construction.html %}
14+
Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of objects, which are instances of classes, and allows for the organization and structure of code that is more closely aligned with real-world entities and concepts. OOP provides a way to structure software as a collection of objects that interact with each other to accomplish tasks. Here’s an in-depth exploration of the key principles and concepts of OOP:
1515

16-
<div id="cppContent">
17-
<!-- Your default C++ content goes here -->
18-
This is C++ content for Object-Oriented Programming.
19-
</div>
16+
## Key Concepts of Object-Oriented Programming
17+
### 1. Classes and Objects
2018

21-
<div id="csharpContent" style="display:none;">
22-
<!-- Your C# content goes here -->
23-
This is C# content for Object-Oriented Programming.
24-
</div>
19+
#### Class:
20+
A class is a blueprint or template for creating objects. It defines the data and behavior (methods) that objects of the class will have. For example, a Car class might define properties like color, model, and methods like start, accelerate, brake.
2521

26-
<div id="pythonContent" style="display:none;">
27-
<!-- Your C# content goes here -->
28-
This is python content for Object-Oriented Programming.
29-
</div>
22+
{: .code }
23+
```
24+
cpp
25+
// Example of a simple class in C++
26+
class Car
27+
{
3028
31-
<button onclick="setLanguageAndShowContent('cpp')">C++</button>
32-
<button onclick="setLanguageAndShowContent('csharp')">C#</button>
33-
<button onclick="setLanguageAndShowContent('python')">Python</button>
29+
private:
30+
31+
string Colour;
32+
string Model;
33+
int Year;
34+
35+
public:
36+
// Constructor
37+
Car(string C, string M, int Y)
38+
: Colour(C)
39+
, Model(M)
40+
, Year(Y)
41+
{
42+
}
43+
44+
// Method to start the car
45+
void Start()
46+
{
47+
cout << "Car started!" << endl;
48+
}
49+
50+
// Method to accelerate
51+
void Accelerate()
52+
{
53+
cout << "Car accelerating!" << endl;
54+
}
55+
56+
// Method to brake
57+
void Brake()
58+
{
59+
cout << "Car braking!" << endl;
60+
}
61+
};
62+
```
63+
#### Object:
64+
An object is an instance of a class. It represents a concrete entity based on the blueprint provided by the class. For example, an object `Ferrari` could be created from the `Car` class.
65+
66+
{: .code }
67+
```
68+
cpp
69+
// Creating an object of class Car
70+
Car Ferrari("Red", "F40", 1987);
71+
```
72+
73+
### 2. Encapsulation
74+
Encapsulation is the bundling of data (attributes) and methods (functions) that operate on the data into a single unit (class). It hides the internal state of an object from the outside world and only exposes a controlled interface to interact with the object.
75+
76+
#### Access Specifiers:
77+
In C++, access specifiers (public, private, protected) control the visibility of class members:
78+
79+
##### public: Members are accessible from outside the class.
80+
##### private: Members are accessible only from within the same class.
81+
##### protected: Members are accessible from within the same class and derived classes.
82+
83+
{: .code }
84+
```
85+
cpp
86+
87+
class SomeClass
88+
{
89+
90+
private:
91+
92+
int PrivateMember;
93+
94+
public:
95+
96+
void PublicMethod()
97+
{
98+
// Public method can access private member
99+
PrivateMember = 10;
100+
}
101+
};
102+
```
103+
104+
### 3. Inheritance
105+
Inheritance allows a class (derived class) to inherit properties and behavior from another class (base class or parent class). It promotes code reuse and establishes a hierarchical relationship between classes. The base class is the class being inherited from, while the derived class is the class that inherits from the base class.
106+
107+
{: .code }
108+
```
109+
cpp
110+
// Base class
111+
class Animal
112+
{
113+
114+
protected:
115+
116+
void Eat()
117+
{
118+
cout << "Omnomnom!" << endl;
119+
}
120+
};
121+
122+
// Derived class inheriting from Animal
123+
class Dog : public Animal
124+
{
125+
126+
public:
127+
128+
void Bark()
129+
{
130+
cout << "Dog is barking!" << endl;
131+
}
132+
133+
void Hungry()
134+
{
135+
if(true)
136+
{
137+
Eat();
138+
}
139+
}
140+
};
141+
```
142+
143+
### 4. Polymorphism
144+
Polymorphism means "many forms" and refers to the ability of objects of different classes to be treated as objects of a common superclass. It allows a single interface to be used for entities of different types, promoting flexibility and extensibility. Article here:
145+
[Polymorphism](/docs/Concepts/Polymorphism/Polymorphism.md)
146+
147+
#### Compile-Time Polymorphism:
148+
Achieved through function overloading and operator overloading.
149+
150+
{: .code }
151+
```
152+
cpp
153+
// Example of function overloading
154+
int Add(int A, int B)
155+
{
156+
return A + B;
157+
}
158+
159+
float Add(float A, float B)
160+
{
161+
return A + B;
162+
}
163+
```
164+
165+
#### Run-Time Polymorphism:
166+
Achieved through virtual functions and inheritance.
167+
168+
{: .code }
169+
```
170+
cpp
171+
// Example of virtual functions
172+
class Animal
173+
{
174+
175+
public:
176+
177+
virtual void MakeSound()
178+
{
179+
cout << "Animal sound" << endl;
180+
}
181+
};
182+
183+
class Dog : public Animal
184+
{
185+
186+
public:
187+
188+
void MakeSound() override
189+
{
190+
cout << "Bark!" << endl;
191+
}
192+
};
193+
```
194+
195+
### 5. Abstraction
196+
Abstraction focuses on the essential features of an object and hides the irrelevant details. It allows developers to create complex systems by breaking them down into smaller, more manageable parts.
197+
198+
#### Abstract Classes and Interfaces:
199+
An abstract class is a class that cannot be instantiated on its own and typically contains one or more pure virtual functions.
200+
An interface is a collection of abstract methods with no concrete implementation.
201+
202+
{: .code }
203+
```
204+
cpp
205+
// Example of abstract class and pure virtual function
206+
class Shape
207+
{
208+
209+
public:
210+
211+
virtual void Draw() = 0; // Pure virtual function
212+
virtual float Area() const = 0; // Another pure virtual function
213+
};
214+
215+
class Circle : public Shape
216+
{
217+
218+
public:
219+
220+
void Draw() override
221+
{
222+
cout << "Drawing Circle" << endl;
223+
}
224+
225+
float Area() const override
226+
{
227+
return 3.14 * radius * radius;
228+
}
229+
230+
private:
231+
232+
float Radius;
233+
};
234+
```
235+
236+
### Benefits of Object-Oriented Programming
237+
- Modularity: OOP promotes modular design, making it easier to understand, maintain, and debug code.
238+
- Code Reusability: Classes and objects can be reused across different parts of an application or in different applications.
239+
- Flexibility and Extensibility: New features can be added by creating new classes without modifying existing code, promoting scalability.
240+
- Encapsulation: Protects data and methods from outside interference, enhancing security and reliability.
241+
242+
### Best Practices
243+
- Single Responsibility Principle (SRP): Each class should have a single responsibility or reason to change.
244+
- Open/Closed Principle (OCP): Classes should be open for extension but closed for modification.
245+
- Liskov Substitution Principle (LSP): Objects of a superclass should be replaceable with objects of its subclasses without affecting the correctness of the program.
246+
- Interface Segregation Principle (ISP): Clients should not be forced to depend on interfaces they do not use.
247+
- Object-Oriented Programming is a powerful paradigm that has revolutionized software development by providing a clear and structured way to model complex systems. It encourages good design practices, enhances code quality, and improves productivity by enabling developers to think in terms of objects and their interactions.
248+
249+
---
250+
251+
#### Author: JDSherbert
252+
#### Published: 04/07/2024

0 commit comments

Comments
 (0)