Object Oriented Programming C++ 6 th Sem, A Div 2018-19 Ms. Mouna M. Naravani
Object Oriented Programming (OOP) removes some of the flaws encountered in POP. In OOPs, the primary focus is on data rather than procedure. The data cannot be moved freely around the system. OOPs ties data more closely to the functions that operate on it, and protects it from accidental modification from outside functions. The functions cannot exist without data, and data need functions to give it a shape.
Organization of data and functions in OOP
OOP allows decomposition of a problem into a number of entities called objects and then builds data and functions around these objects. Data is hidden and cannot be shared by outside functions. The data of an object can be accessed only by the functions associated with that object. The objects that are created may interact with each other via functions. OOPs follows Bottom-up approach, because the objects are created first and the data and functions around them are developed later.
Some of the striking features of object-oriented programming are: Emphasis is on data rather than procedure. Programs are divided into what are known as objects. Data structures are designed such that they characterize the objects. Data is hidden and cannot be accessed by external functions. Objects may communicate with each other through functions. New data and functions can be easily added whenever necessary. Follows bottom-up approach in program design.
Basic concepts of Object Oriented Programming Classes Message Passing Objects OOP Polymorphism Inheritance Data Abstraction and Encapsulation
Objects: Objects are basic run time entities in OOP. It may represent a person, a bank account, a place, a table of data, vectors, etc. Objects should be chosen such that they match closely with real-world objects. Ex: Name, DOB, Address, Age, Mobile number. If these details are required for student, create STUDENT object. If these details are required for customer, create CUSTOMER object. If these details are required for employee, create EMPLOYEE object. Objects occupy space in the memory and have an associated address. When a program is executed, the objects interact by sending messages to one another. Ex: If CUSTOMER and ACCOUNT are two objects, then the CUSTOMER object may send a message to the ACCOUNT object requesting for bank balance.
Objects contain both data and operations that manipulate these data. Objects can interact without having to know details of each other s data or code. It is sufficient to know the type of message accepted, and the type of response returned by the objects.
Classes: A class is a collection of objects of similar type. Ex: mango, apple and orange are members of the class fruits. The entire set of data and code of an object can be made a user defined data type with the help of a class. Once a class is defined, any number of objects belonging to that class can be created.
Encapsulation: The wrapping up of data and functions into a single unit (called class) is known as Encapsulation. It hides the implementation details of an object from its users. Encapsulation prevents unauthorized access of data or functionality. The data is not accessible to the outside world, and only functions which are wrapped in the class can access it. This is called data hiding or information hiding.
Data Abstraction: Abstraction refers to the act of representing essential features without including background details or explanations. It separates unnecessary details or explanations so as to reduce complexities of understanding requirements.
Inheritance: Inheritance is the process by which objects of one class acquire the properties of objects of another class. Hierarchical Classification Reusability adding additional features to an existing class without modifying it. That is, deriving a new class from the existing one. The new class will have the combined features of both the classes. New class is also called as Derived class and existing class is called as Base Class.
Polymorphism: (having many forms) Ability to take more than one form. The polymorphism is an ability to access different implementations of a function using the same name. Ex: Function to perform addition operation Input Output Two integer numbers integer sum Two float numbers float sum Two strings constants concatenation
Addition Operation add(int, int) add(float, float) add(string, string)
Operator Overloading: The process of making an operator to exhibit different behaviours in different instances. Function Overloading: Using a single function name to perform different types of tasks.
Dynamic Binding: Dynamic binding (late binding) means that the code associated with a procedure call is not known until the time of the call at run-time. Usually associated with Polymorphism and Inheritance. At run-time, the code matching the object under current reference will be called.
Message Passing: Set of objects communicate with each other. Steps involved: 1. Creating classes that define objects and their behaviour. 2. Creating objects from class definitions. 3. Establishing communication among objects. Objects communicate with one another by sending and receiving information. A message for an object is a request for execution of a procedure, and therefore will invoke a procedure in the receiving object that generates the desired result. Message Passing involves specifying the name of the object, the name of the procedure(message) and the information to be sent.
employee.salary(name); Object Message/ Procedure Information Objects have a life cycle. They can be created and destroyed. Communication with an object is feasible as long as it is alive.
Difference between Procedure Oriented Programming and Object Oriented Programming Procedure Oriented Programming In POP, program is divided into small parts called functions. In POP, Importance is not given to data but to functions as well as sequence of actions to be done. POP follows Top Down approach. POP does not have any access specifier. In POP, Data can move freely from function to function in the system. To add new data and function in POP is not so easy. Object Oriented Programming In OOP, program is divided into parts called objects. In OOP, Importance is given to the data rather than procedures or functions because it works as a real world. OOP follows Bottom Up approach. OOP has access modifiers named Public, Private, Protected, etc. In OOP, objects can move and communicate with each other through member functions. OOP provides an easy way to add new data and function.
Procedure Oriented Programming In POP, Most function uses Global data for sharing that can be accessed freely from function to function in the system. POP does not have any proper way for hiding data so it is less secure. In POP, Overloading is not possible. Example of POP are : C, VB, FORTRAN, Pascal. Object Oriented Programming In OOP, data can not move easily from function to function, it can be kept public or private so we can control the access of data. OOP provides Data Hiding so provides more security. In OOP, overloading is possible in the form of Function Overloading and Operator Overloading. Example of OOP are : C++, JAVA, VB.NET, C#.NET.
References Sourav Sahay, Objected Oriented Programming with C++ E Balagurusamy, Objected Oriented Programming with C++ P. B. Kotur, Objected Oriented Programming with C++