C++ And Object Oriented Programming OOPS Concepts

C++ And Object Oriented Programming OOPS Concepts

Learn Object Oriented Programming OOPs concepts in C++. Object Oriented Programming (OOP) is a programming approach being implemented by almost all the modern programming languages and professional programmers. 


Object Oriented Programming (OOP) is a programming approach being implemented by almost all the modern programming languages and professional programmers. This approach is based on concepts like ‘class’ and ‘objects’. In OOP, every entity is considered to be a class. A class should have some attributes and/or behaviors.

For example, we can define a class ‘Person’ having attributes like name, age, address, cell number etc. and behaviors like eat, speak, walk and so on. Objects are real representation of a class. For example Michael is an object of class Person. We can have as many objects of a class as we need.

The attributes are represented in the form of variables and behaviors are represented in the form of function (sometimes called as methods as well). We will learn about variables and functions later in this tutorial. But at the moment, you can say variables represent attributes of a class.

For example name of a person, age, cell number, weight or height are attributes and thus can be represented as variables in a class. Similarly the actions performed are called functions. In our example, speak, eat, write and walk etc. are some of the actions being performed by a person, so can be represented as functions in a class.

C++ supports the object oriented programming approach. However, we cannot say that C++ is a pure OOP based language but is fair enough to write computer programs having OOP concepts. For OOP based programming, every program must have at least one class and its associated code.

If we do not write a class then the program is neither compiled nor executed. In C++, we can write computer programs without defining a class and that’s why C++ is not considered as pure OOP based language. On the other hand, if you are using Java for writing computer programs, then you must write at least one class otherwise the program will not be compiled and hence is considered as a pure OOP based language.

Pillars of OOP

There are a number of concepts in OOP but the basic pillars of OOP are as below:

  1. Abstraction
  2. Inheritance
  3. Encapsulation
  4. Polymorphism

Abstraction

Abstraction is an important concept of OOP. Abstraction is used to focus on the necessary code and hide the details from outside world. It is also used in situations when the function to be performed is known but the details are left for a later stage. Sometimes abstraction is used to highlight the basic activities/functions to be performed and fill the details later.

Inheritance

Without inheritance we cannot take the full advantage of OOP. One aspect of OOP is the code reusability i.e. resuse the existing code again and again without writing it more than once in different programs. Inheritance allows us the code reusability.

In inheritance parent-child relationship is created between two or more classes such that one class inherits all the code of another class. The inherited class is called parent and the class which inherits the parent class is called the child class. Once parent-child relationship is created then child can access all the variables and functions of parent class unless encapsulated.

Encapsulation

Sometimes, a class wants to hide some variables or member functions from the outside world. This feature is achieved through encapsulation. Encapsulation hides the internal details of a class and binds the variables and member functions together to work as a unit.

Polymorphism

‘Poly’ means many and ‘morph’ means faces. Hence polymorphism is a feature of OOP where the same function, object or an operator behave differently at different places. For example the same object can behave as a teacher at one place and as a student at another place.

More Related Articles For You

    C++ Tutorial

    C++ Quizzes