What is Object-oriented programming (OOP)
Object-oriented programming (OOP) is a programming paradigm that focuses on the concept of objects and their interactions. OOP is a popular programming paradigm and is widely used in modern programming languages such as C++, Java, and C#. In this blog post, we will introduce the basic concepts of object-oriented programming in C++.
The main idea of OOP is to model real-world entities as objects in the program. Each object is an instance of a class, which defines the object’s properties and behaviors. Properties are represented by member variables, and behaviors are represented by member functions.
A class is a blueprint for creating objects. It defines the properties and behaviors of an object. In C++, a class is defined using the keyword “class” followed by the class name. The class definition is enclosed in curly braces {}. For example:
class Shape {
public:
int x, y;
void move(int dx, int dy);
};
In this example, we have defined a class called “Shape” with two member variables, “x” and “y”, and a member function “move()”. The keyword “public” indicates that the member variables and functions are accessible from outside the class.
Objects are instances of a class and are created using the keyword “new”. For example:
Copy codeShape *s = new Shape;
In this example, we have created an object “s” of the class “Shape”.
Member functions are used to define the behaviors of an object. In C++, member functions are defined inside the class definition. They have the same syntax as regular functions, but they have an additional parameter, “this”, which is a pointer to the object that is calling the function. For example:
void Shape::move(int dx, int dy) {
x += dx;
y += dy;
}
In this example, the member function “move()” is used to change the position of the shape object.
Inheritance is a feature of OOP that allows a new class to inherit the properties and behaviors of an existing class. The new class is called a derived class, and the existing class is called a base class. The derived class can add new properties and behaviors and can also override the base class’s properties and behaviors. In C++, inheritance is implemented using the keyword “:” followed by the base class name. For example:
class Rectangle: public Shape {
public:
int width, height;
int area();
};
In this example, we have defined a class “Rectangle” that inherits from the class “Shape”. The class “Rectangle” has two new member variables, “width” and “height”, and a new member function “area()”.
Polymorphism is a feature of OOP that allows objects of different classes to be treated as objects of a common base class. In C++, polymorphism is implemented using virtual functions. A virtual function is a member function that can be overridden by a derived class. For example:
class Shape {
public:
virtual int area() = 0;
};
class Rectangle: public Shape {
public:
int width, height;
int area();
};
In this example, the class “Shape” has a virtual function “area()” that is overridden by the class “Rectangle”.
Encapsulation is a feature of OOP that allows hiding the implementation details of an object from the outside world, and only exposing a public interface to interact with the object. This helps to protect the internal state of an object from accidental modification, and also allows for flexibility in changing the implementation without affecting the code that uses the object.
In C++, encapsulation is achieved through the use of access modifiers such as “public”, “private”, and “protected”. “Public” members are accessible from outside the class, “private” members are only accessible from within the class, and “protected” members are accessible from within the class and its derived classes.
For example, in the class definition we saw earlier:
class Shape {
public:
int x, y;
void move(int dx, int dy);
private:
int _x, _y;
};
The member variables “x” and “y” are public, meaning they can be accessed and modified from outside the class. However, the member variables “_x” and “_y” are private, meaning they can only be accessed and modified from within the class. This allows for more control over the internal state of the object, and helps to prevent accidental modification.
Encapsulation also allows for the implementation of an object to change without affecting the code that uses the object. For example, if we need to change the way the “move()” function works, we can do so without affecting the code that calls the function.