How well do you understand object oriented programming?

Tram Ho

When it comes to object-oriented programming, you will immediately think of four properties: encapsulation, inheritance, polymorphism and abstraction. In essence, these 4 properties are just like the materials for building an object-oriented program, the most important thing is how you use these materials to build the program.

So what is object oriented programming?

Object oriented programming is simply understood as a method to solve programming problems that, when applied, the code becomes easier to develop and maintain. This method breaks the program down into objects and relationships, each with properties (data) and behaviors (methods). To be able to program and design programs in this way, you definitely need to understand 4 properties: encapsulation, inheritance, polymorphism and abstraction.

1. Encapsulation

This is a technique that helps you conceal information inside the object by using private access scope for properties, to communicate or retrieve the object’s information must be through public methods. , thereby reducing errors when developing the program. This property is similar to in fact, you cannot see the real attributes of a person (personality, interests, other private information, …), everything you know is through. the actions of the person. For example, that person tells you about interests, ages, … but this information is not necessarily true of that person’s true properties (just like getter does not return the real value of the property. returns a different value). The main benefits that encapsulation brings: Limits invalid access to object properties. Makes the state of objects always correct. For example, if the gpa property of Student class is public, it will be difficult to control the value, you can change gpa to any value. Conversely, if you leave the gpa property private and provide the setGpa () function like this: void setGpa (double gpa) {if (gpa> = 0 && gpa <= 4) {this-> gpa = gpa; } else {cout << "gpa is invalid"; }}

thì lúc này giá trị của thuộc tính gpa sẽ luôn được đảm bảo là không âm và nhỏ hơn hoặc bằng 4 (do muốn thay đổi gpa thì phải thông qua hàm setGpa ()) ''

Helps hide unnecessary information about the object. Allows you to change the structure within a class without affecting another class. For the initial example, you design the Student class as follows: class Student { private: string firstName; string lastName; public: Student() { ... } string getFullName() { return firstName + lastName; } }; Later if you want to combine the two properties firstName and lastName into fullName , the Student class will look like this: class Student { private: string fullName; public: Student() { ... } string getFullName() { return fullName; } }; At this point, the Student class structure has been changed, but the objects using this class still do not need to change because these objects only care about the getFullName () method. If this method is not available, then you must correct all the places using the firstName and lastName properties of the Student class.

Note: keep in mind that the main purpose of encapsulation is to limit errors in the development of the program, not to secure or conceal information.


When programming, there are certainly cases where objects share some properties and methods. For example, when you write a program that stores information about students and teachers. For students, it is necessary to save information about the name, age, address, grade and with the teacher, it is necessary to save information about the name, age, address, salary => now the code will be duplicated a lot (from properties to setter, getter, ...) and it violates one of the most basic programming rules of DRY (Don't Repeat Yourself - never repeat code). To see more clearly, consider the following class diagram:

With inheritance, this problem will be solved, inheritance in object-oriented programming is to inherit the properties and methods of a class. This means that if class A inherits class B, then class A will have the properties and methods of class B. Therefore, from the above diagram you can separate the identical properties and methods into a new class named Person and for the Student and Teacher classes to inherit this class like this:

As can be seen with this diagram, the Student and Teacher classes will inherit the common properties from the Person class and the code will no longer be duplicated. That is the benefit of inheritance.

3. Polymorphism

As you know, object oriented programming is a method of thinking and solving programming problems in a practical direction. Therefore, its properties will also be associated with reality, so you need to first understand the polymorphism in reality. Polymorphism is understood that in each situation, each different case, the objects will play different roles. For example, the same person but when at the company, the role is an employee, when going to the supermarket, there is the role of a customer, or when at school, the role is a student, ... => Being the same person but having many different roles, this is polymorphism in reality. In programming, when an object or a method has more than one form, it is polymorphism. Polymorphism is shown in 3 forms:

3.1. Polymorphism with method overloading

For example, the addition method will take the form of adding 2 integers, adding 2 real numbers, adding 3 integers, v / v. It can be seen that the same is the method of addition but there are many different forms, so this is an expression of the polymorphism. Example of polymorphism with method overloading: #include <iostream>

using namespace std;

class Calculator {public: int add (int a, int b) {return a + b; }


int main () {Calculator c; cout << c.add (1, 2) << endl; cout << c.add (3.3, 4.2) << endl; cout << c.add (1, 2, 3) << endl; return 0; } Kết quả khi chạy chương trình: 3 7.5 6

3.2. Polymorphism with method override

For example, the getSalary() used to calculate the salary will have the form of calculating the salary for the manager, the salary for the employee:

Employee class {private: string name; int salary;

public: Employee (string name, int salary) {this-> name = name; this-> salary = salary; }


class Manager: Employee {private: int bonus; public: Manager (string name, int salary, int bonus): Employee (name, salary) {this-> bonus = bonus; }


All is salary calculation, but for each object there is a different way of calculating, that is polymorphism.

3.3 Polymorphic objects through polymorphic objects

The parent class variable can refer to the objects of the subclasses, so the parent class variable also has many forms, so this is also polymorphic. For example:

#include <iostream>

using namespace std;

class Animal {public: virtual void sound () {cout << “some sound” << endl; }};

class Dog: public Animal {public: void sound () {cout << “bow wow” << endl; }};

class Cat: public Animal {public: void sound () {cout << “meow meow” << endl; }};

class Duck: public Animal {public: void sound () {cout << “quack quack” << endl; }};

int main () {Animal * animal = new Animal (); animal-> sound (); animal = new Dog (); animal-> sound (); animal = new Duck (); animal-> sound (); animal = new Cat (); animal-> sound (); return 0; } Kết quả khi chạy chương trình: some sound bow wow quack quack meow meow `

4 Abstraction

Abstraction is a property that simplifies information within the object, it allows us to communicate with the object’s components without having to know how these components are constructed (more precisely. no matter how these elements are coded, just know what these elements are used for). First of all, let’s look at a practical example of abstraction: When you go to withdraw money at an ATM, you don’t care about how the ATM works or the components in the ATM, which you care about. That only mind is the withdrawal feature. In this case, unnecessary information of ATM such as counting money, subtracting money from account, sending data to the server is hidden. What you see about the ATM object is the withdrawal => ATM has hidden unnecessary details and that is the abstraction. Similarly in programming, when calling the methods of an object, you only need to care about what that method is used for, no matter how the method is coded. This feature is very useful when working in groups, you only need to care about the functionality of the methods your colleagues code, not how it is implemented. To implement abstraction, you can use abstract classes and interfaces because it only contains declarations and not implementations (in some languages ​​there is no concept of interfaces, so if you do not know about the interface, then Can understand the interface is abstract class with methods are abstract). In fact, when you go to work, you will use the interface a lot, with each class you usually create a separate interface to represent the features of that class and use this interface to communicate with the object. For example, the Customer class will have an ICustomer interface, other objects that want to communicate with the Customer class must all go through the above interface ..


Object-oriented programming not only encapsulated in the above 4 properties, in order to write a good program, you also have to know a lot of other materials such as OOP design, Software Architecture, … in this article, I just summarized about object oriented programming and 4 main properties, if you want to learn more details, you can refer to C ++ OOP and Java OOP on the system. As for other topics in OOP, I will introduce in the next articles.


Share the news now

Source : Viblo