Learn With Steem Contest:Object Oriented Programming.

in Steem4Nigerialast year (edited)


Asslam o Alaikum!



I hope that you all will be good by the blessings and mercy of Almighty Allah. I warmly welcome you to this publication today here I am to create the publication in Steem4Nigeria for the weekly contest learn with Steem which is a wonderful initiative step by @fredquantam.

Copy of Black & Yellow Modern Abstract How To Tutorial Youtube Thumbnail.png

Image created from canva

As the world of technology continues to evolve and expand, so too does the need for efficient and organized coding practices. One such method that has proven to be highly effective is Object-Oriented Programming (OOP). In this article, we will dive into the fundamentals of OOP and explore its various concepts such as classes, objects, encapsulation, inheritance, and polymorphism. Whether you're a seasoned coder or just starting out, understanding the basics of OOP is essential for writing clear, maintainable, and reusable code. So, join me as we navigate the exciting world of OOP and discover how it can take your coding skills to the next level.



Object-Oriented Programming in C++



Introduction:

Object-oriented programming (OOP) is a programming that is centered around the concept of objects and their interchanges. It is established on the idea of creating objects, which are illustrations of a class, that include both data and forms to exploit that data. OOP is a powerful programming technique that promotes code reuse, easy supervision, and better association of the code. It is widely used in modern software development, and many popular programming languages, such as C++, Java, C#, and Python, support OOP. OOP allows for the modeling of real-world objects and their interactions in a more natural and instinctive way, making it a popular choice for complex software development projects.

image.png

Image taken from source

Object-oriented programming revolves around the idea of objects and their makings by using codes. This article will provide an in-depth understanding of OOP in C++, including its concepts, principles, and practical implementation.

What is OOP?

OOP is programming that is based on the idea of objects. An object is a self-contained commodity that incorporates data and behavior. The data is depicted by the object's attributes, and the behavior is represented by the entity methods. OOP allows for better code organization, code reuse, and easier code maintenance.

It mostly allows the user to create real-world objects in an organized way by coding and making the perfect objects when they develop games, graphics, sites, and other operating systems. This strategy leads to a more structured and modular codebase, allowing developers to better supervise and manage the data. OOP also promotes code reuse by providing the mechanism of inheritance, which allows classes to inherit properties and methods from other classes, resulting in less repetitious code.

Classes and Objects

Class:

A class is a prototype or model for creating objects. It defines the data and methods that an object can have, and serves as a template for creating new objects.
For example, consider the following class in C++ that defines a "Car" class:

image.png

class Car {
public:
int numberOfWheels;
string color;
string make;
string model;

void startEngine() {
// code to start the engine
}
};
image.png

This class defines the data members (numberOfWheels, color, make, model) and a method (startEngine) that a Car object can have.

Object:

An object is an instance of a class. It contains data and methods that are defined by its class.
For example,

image.png

Car myCar;
myCar.numberOfWheels = 4;
myCar.color = "red";
myCar.make = "Toyota";
myCar.model = "Camry";
myCar.startEngine();
image.png

Here "myCar" is an object of class "Car". It contains the data members that were defined in the class, such as a number of wheels, color, make, and model, and it can access the methods defined in the class, such as StartEngine ().In this example, the class "Car" serves as a blueprint for creating Car objects, and the object "myCar" is an instance of the "Car" class that contains specific data and can access specific methods.



Encapsulation



Encapsulation is the approach of masking the inner attributes of an object from the outer world. It allows for better data covering and protection, as well as more manageable code supervision. In C++, encapsulation is accomplished via the help of key modifiers such as general, private, and shielded.

For example, consider the following class in C++ that defines a "BankAccount" class:

image.png

class BankAccount{
private:
int accountNumber;
double balance;
public:
void deposit(double amount) {
// code to deposit money
balance += amount;
}
void withdraw(double amount) {
// code to withdraw money
if (amount > balance) {
cout << "Insufficient funds." << endl;
}
else {
balance -= amount;
}
}
double getBalance() {
// code to return balance
return balance;
}
};
image.png
In this example, the data members' account numbers and balances are declared private, meaning that they can only be accessed within the class. This encapsulates these data members and prevents them from being directly accessed or modified by external code.
Instead, external code can only access these data members through public methods such as deposit, withdraw, and get balance. This allows for better control over the data and prevents any unauthorized access or modification.

In this example, encapsulation allows the bank account balance to be hidden from the outside world and can only be accessed via withdrawal and deposit methods. This provides better security and allows the class to control the consistency of the data.

Inheritance

Inheritance is a means that allows one class to inherit the effects and methods of another class. This allows classes to be managed in a hierarchical structure and encourages code reuse. In C++, a class can inherit from another class using the keyword ':'

image.png

class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};

class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
image.png

In this example, we have a Shape class that has two methods setWidth and setHeight to set the width and height of the shape. It also has two protected variables width and height which are used to store the width and height of the shape.
Then, we have a Rectangle class which inherits from the Shape class using the keyword public. This means that the Rectangle class has access to all the public and protected members of the Shape class. The Rectangle class also has its own method getArea which calculates and returns the area of the rectangle using the width and height inherited from the Shape class. This is an example of how inheritance allows for code reuse and organization.



Polymorphism



Polymorphism is the capability of an object to take on numerous forms. In OOP, polymorphism allows entities of various classes to minister as objects of a typical class. This is accomplished through the help of virtual functions and function overloading.
image.png

class Shape {
public:
virtual void draw() {
// code to draw a shape
}
};

class Rectangle: public Shape {
public:
void draw() {
// code to draw a rectangle
}
};

class Circle: public Shape {
public:
void draw() {
// code to draw a circle
}
};

void drawShapes(Shape *shapes[], int size) {
for(int z = 0; z< size; z++) {
shapes[z]->draw();
}
}
image.png

In this example, we have a Shape class with a virtual function draw(). We also have two classes Rectangle and Circle which inherit from the Shape class. Each class has its own implementation of the draw() function.
Then, we have a function to draw shapes which takes an array of Shape pointers and a size as its arguments. Inside the function, we use a loop to iterate through the array and call the draw() function on each element of the array. Since the draw() function is virtual, at runtime the correct version of the function will be called for each object, whether it is a rectangle or a circle. This is an example of polymorphism, where the same function call can be used to call different implementations of a function depending on the type of object

Advantages of OOP in C++

image.png

Image taken from source

  • Improved code organization and format
  • Encapsulation allows for better information covering and protection
  • Inheritance promotes code reuse and reduces redundancy
  • Polymorphism permits for more flexibility and dynamic behavior
  • Allows for more uncomplicated code supervision and adaption


Conclusion



OOP in C++ is a powerful programming paradigm that allows for better code organization, code reuse, and easier code maintenance. The concepts of classes, objects, encapsulation, inheritance, and polymorphism are essential for understanding and utilizing OOP in C++. The use of OOP in C++ allows for more robust and efficient code and makes it a popular choice for complex software development. It is important for developers to have a solid understanding of OOP in C++ to effectively design and implement software projects.

So this was the introduction to object-oriented programming. Next week I will participate and we will explore more about the topic. I hope that you all will learn a lot from this article. happy reading!

Special mentions

@fredquantum
@steemchiller

I would like to invite @steemdoctor1,@liasteem,@waterjoe,@chant to participate in this contest

Regards
@mateenfatima

Sort:  

Thank you, friend!
I'm @steem.history, who is steem witness.
Thank you for witnessvoting for me.
image.png
please click it!
image.png
(Go to https://steemit.com/~witnesses and type fbslo at the bottom of the page)

The weight is reduced because of the lack of Voting Power. If you vote for me as a witness, you can get my little vote.

Congratulations!
This post has been upvoted through steemcurator07.
We support quality posts anywhere and any tags.
Curated by : @steemdoctor1

TEAM 4 CURATORS

 last year 

Thank you very much for this article at least I have learn about (Opp) ortunity Object-oriented programming and the useful of it, I also believe it others ready this post.

 last year 

Thanks for reading and valuable comment.

Having experience in other programming languages I believe going to learn more faster with your explanation introduction to OOP. Thanks

Loading...

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.029
BTC 66521.28
ETH 3454.20
USDT 1.00
SBD 2.67