How to start object-oriented programming in C++?
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which may contain data, in the form of attributes; and instructions to do things, in the form of methods.
For example, a person is an object which has certain properties such as height, gender, age, etc. It also has certain methods such as move, talk, and so on.
This is the basic unit of object oriented programming. That is both data and function that operate on data are bundled as a unit called as object.
When you define a class, you define a blueprint for an object. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.
OOP has four basic concepts on which it is totally based. Let's have a look at them individually −
- Abstraction − Abstraction means providing only essential information to the outside world and hiding their background details. For example, a web server hides how it processes data it recieves, the end user just hits the end points and gets the data back.
- Encapsulation − Encapsulation is a process of binding data members (variables, properties) and member functions (methods) into a single unit. It is also a way of restricting access to certain properties.
- Inheritence − The ability of creating a new class from an existing class is called Inheritence. Using inheritence, we can create a Child class from a Parent class such that it inherits the properties and methods of the parent class and can have its own additional properties and methods. For example, if we have a class Vehicle that has properties like Color, Price, etc, we can create 2 classes like Bike and Car from it that have those 2 properties and additional properties that are specialized for them like a car has numberOfWindows while a bike cannot. Same is applicable to methods.
- Polymorphism − The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance. C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function. For example, if we have a class called Shape and a method called getArea in it, then depending on what the shape is, we'll be getting the area processed in a different way(Circle vs square).
You can head over to the excellent tutorial on C++ Object oriented programming on tutorials
point(https://www.tutorialspoint.com/cplusplus/cpp_object_oriented.htm) to start learning OOP.
Published on 28-Feb-2018 11:55:10