Data Structures and Programming with C++ By Dr. Atul Kumar Dwivedi ETC, BIT, Durg UNIT-I B. E., V Semester
Outline Basic concepts of Object oriented Programming (OOPs) 1. Objects 2. Classes 3. Data encapsulation 4. Data abstraction 5. Inheritance 6. Polymorphism 7. Dynamic binding 8. Message passing Benefits of OOPs Applications of OOPs Quiz Lab exercise 2
1. Objects Objects are run time entities related to a person, place or thing that have some specific features. Objects take space in the system memory and have some address associated with the memory. The data and functions of an object are stored in the class. Class 3
2. Classes Class specifies the data and functions related to objects. The entire set of data and code of an object can be made a userdefined data type with the help of class In fact, objects are variables of the type class. Once a class has been defined, we can create any number of objects belonging to that class. Each object is associated with the data of type class with which they are created. A class is thus a collection of objects similar types. 4
2. Classes (Example) For examples, Mango, Apple and orange members of class fruit. Classes are user-defined that types and behave like the built-in types of a programming language. The syntax used to create an object is not different then the syntax used to create an integer object in C. If fruit has been defines as a class, then the statement Fruit Mango; Will create an object mango belonging to the class fruit. 5
3. Data Encapsulation The wrapping up of data and functions into a single unit (called class) is known as encapsulation. Encapsulation helps in data hiding and protection. Data protection help the programmer as well user by providing data security Functions Data Class 6
4. Data Abstraction Data abstraction refers to, providing only needed information to the outside world and hiding implementation details. For example, consider a class Complex with public functions as getreal() and getimag(). We may implement the class as an array of size 2 or as two variables. The advantage of abstractions is, we can change implementation at any point, users of Complex class wont t be affected as out method interface remains same. Had our implementation be public, we would not have been able to change it. 7
5. Inheritance inheritance is the process by which objects of one class acquire the properties of objects of another class. It supports the concept of hierarchical classification. Inheritance provides re usability. This means that we can add additional features to an existing class without modifying it. 8
5. Inheritance(Example) 9
6.Polymorphism polymorphism means ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends upon the types of data used in the operation. C++ supports operator overloading and function overloading. Operator overloading is the process of making an operator to exhibit different behaviors in different instances is known as operator overloading. Function overloading is using a single function name to perform different types of tasks. polymorphism is extensively used in implementing inheritance. 10
7. Dynamic binding In dynamic binding, the code to be executed in response to function call is decided at runtime. C++ has virtual functions to support this. 11
Dynamic binding 12
8. Message Passing Objects communicate with one another by sending and receiving information to each other. A message for an object is a request for execution of a procedure and therefore will invoke a function in the receiving object that generates the desired results. Message passing involves specifying the name of the object, the name of the function and the information to be sent. 13
Benefits of OOP Benefits to the programmer 1. 2. 3 Benefits to the user 1. 2. 3. 14
Applications of OOP Applications of OOP are beginning to gain importance in many areas. The most popular application of object-oriented programming, up to now, has been in the area of user interface design such as window. Hundreds of windowing systems have been developed, using the OOP techniques. Real-business system are often much more complex and contain many more objects with complicated attributes and method. OOP is useful in these types of application because it can simplify a complex problem. The promising areas of application of OOP include: Real-time system simulation and modeling Object-oriented data bases Hypertext, Hypermedia, and expertext AI and expert systems Neural networks and parallel programming Decision support and office automation systems CIM/CAM/CAD systems 15
Quiz What is the difference between data encapsulation and data abstraction What are the benefits to the user by using object oriented programming approach 16
Practice problems Write a class to store details of students under following heads Name Roll no Marks (a) Calculate average marks of the class and (b) find topper of the class Write a program to calculate volume of a box using class. (a) Calculate volume in main function (b) Calculate volume in the class using member function definition within the class (c) Calculate volume in the class using volume function definition outside the class and outside the main function (d) Now increase the number of boxes by taking input from the user 17
Dr. Atul Dwivedi, BIT Durg, (India) email : atuldwivedi@live.in 8/23/2017 18