Object Oriented Programming definitely took the programming world by storm when it appeared and still forms basis for programming to a large extent. In this article we would explore Object Oriented programming In C++. Following Pointers will be covered in this article,
- What Is Object-Oriented Programming?
- Features of object-oriented programming
What Is Object-Oriented Programming?
The major motivation behind the invention of the object oriented approach is to remove some of the flaws encountered in the procedural approach. In OOP data is treated as a critical element in the program development and restricts its flow around the system. It ties data more closely to the function that operate on it and protects it from accidental modification from outside functions. It allows the problem to break into a number of entities called objects and then builds data and functions around these objects. The data of an object can only be accessed by the functions associated with that particular object. However, the functions of one object can access the functions of other objects if required.
Features of object-oriented programming:
- It emphasis more on data rather than procedure.
- The programs are divided into objects thus making it easy to work with.
- Data structures are designed in such a way that they characterize the objects.
- Functions that work on the data of an object are placed together in the data structure.
- Data is hidden and cannot be accessed by external functions without permission.
- Communication between objects can take place with the help of functions.
- Adding new data and functions has become easy.
- Follows the bottom-up approach in program design.
Objects are the most basic run·time entities in OOPs.They may represent user·defined data such as vectors, time and lists or any item that program has to handle. Programming problem is analyzed on the basis of objects and the nature of communication between them. Objects should be selected such that they match closely with the real-world objects. Objects take up space in the memory and have no associated address. While execution objects interact by sending messages to one another. For example, if “Student” and *Student_roll_no” are two objects in a program, then the Student object may send a message to the Student_roll_no object requesting for its associated Marks. Each object contains code to manipulate the data. Objects can interact with each other’s data or code without having to know its
The entire set of code of an object can be made a user-defined data type with the help of a class, actually, objects are variables of the type class. Once we define a class, we can create any number of objects belonging to the class. Each object can be associated with the data of type class with which they are created. Thus a class is nothing but a collection of objects of similar type. For example, mobile, laptop, and smartwatch are all members of the class electronics. Classes are user-defined data types. The syntax used to create an object is quite simple. If electronics has been defined as a class, then the statement electronics laptop; will create an object laptop belonging to the class electronics.
This will create an object laptop belonging to the class electronics.
Encapsulation can be defined as wrapping up of data and information under a single unit. In Object-Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulate them.
Consider a real-life example of encapsulation, in a School, there are different sections like the student’s section, teachers section, accounts section, etc. The student section handles all the student’s activities and keeps records of all the data related to finance. Similarly, the teacher’s section handles all the teacher’s related activities and keeps records of all the marks and performance of students. Now there may arise a situation when for some reason a student from the student section needs all data about marks and performance of all students. In this case, he is not allowed to directly access the data of the teacher’s section. He will first have to contact some teacher in the teacher section and then request him to give all the data. This is what encapsulation is. Here the data of the student’s section and the teacher’s section that can manipulate them are wrapped under a single name of “teachers section”.
Abstraction refers to showing only the important and required features of the application and hiding the details. In C++, classes can provide data and functions to the outside world to access, keeping the variables hidden from direct access, or classes can even declare everything accessible to everyone, or maybe just to the classes inheriting it, we can change them as per our requirements.
This can be done using access specifiers. C++ has 3 access specifiers:
The word polymorphism means having many forms. We can define polymorphism as the ability of a function or data to be displayed in more than one form.A person can have different characteristic at the same time. As a boy at the same time is a student, a brother, a son. So the same person posses different behavior in different situations. This is called polymorphism.
An operation can exhibit different behaviors as per the requirement in different instances. The behavior depends upon the types of data used and its requirements in the operation.
C++ supports operator overloading and function overloading.
- Operator Overloading: It’s the process of making an operator to exhibit different behaviors in different instances.
- Function Overloading: Function overloading is using a single function name to perform different types of tasks.
Polymorphism is extensively used in implementing inheritance.
Inheritance is the process by which objects of one class can acquire the properties, functions, and data of objects of another class. It follows the concept of hierarchical classification. For example, the bird ‘sparrow’ is a part of the class ‘flying bird’ which is again a part of the class ‘bird’. With the help of this sort of division each derived class shares common characteristics and data with the class from which it is inherited. Inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying or making any changes in it. This is possible by deriving a new class from the existing class. The new class will acquire the combined features of both parent and child class.
The real use of the inheritance is that it allows the programmer to reuse a class that is almost, but not exactly, what he wants, and to alter the class and make changes in such a way that it does not introduce any undesirable side-effects into the rest of the classes. Note that each sub-class defines only those features that are unique to it and the rest of it, it derives from its parent class. Without the use of any classification, each class would have to explicitly include all of its features which would have taken a lot of time and effort.
Thus we have come to an end of this article on ‘Object Oriented Programming In C++’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.