Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming INTRODUCTION : The object oriented programming paradigm is the latest in the software development and the most adopted one.

Similar presentations

Presentation on theme: "Object Oriented Programming INTRODUCTION : The object oriented programming paradigm is the latest in the software development and the most adopted one."— Presentation transcript:

1 Object Oriented Programming INTRODUCTION : The object oriented programming paradigm is the latest in the software development and the most adopted one in the developing projects of today. Paradigm means organizing principle of a program. It is an approach to programming.

2 Procedural V/s Object Based Programming In procedural programming, whenever the definition of type changes, the functions referring to this type must also be changed to reflect the change. Procedural programming leads to increased time and cost overheads during design changes. In object based programming, whenever there is any change in the definition of type,users interface remains unaffected generally. Object based programming is subset of OOP. Because it implements some of the features of OOP.

3 OOP -Concepts 1.Data Abstraction – it is the act of representing the essential features without including the background details. Data Hiding- it is a related concept of data abstraction. Unessential features are hidden from the world. 2.Data Encapsulation – it is the wrapping of data and associated functions in one single unit. 3.Inheritance – it is the capability of one class to inherit properties from other class. 4.Polymorphism – it is the ability for data to be processed in more than one form. 5.Modularity – it is the concept of breaking down the program into several module. E.g. : Classes, Structure, etc.

4 Implementing OOP concepts in C++ We know that a class represents a group of objects that share common properties and relationship. While an object is a distinct instance of a given class that has some characteristics and behavior. While implementing encapsulation the following things are taken care of – (i) Anything that an object does not know or cannot do is excluded from the object. (ii) Encapsulation is used to hide unimportant implementation details from other objects. (iii) Packaging an objects variables within the protective custody is accomplished through classes i.e.the data and associated functions are wrapped up in one unit.

5 Implementing OOP concepts in C++ A class groups its members into three sections: private, protected and public. The private and protected members remain hidden from outside world. Thus through private and protected members, a class implements data-hiding. The outside world is given only the essential and necessary information through public members, rest of the things remain hidden, which is nothing but abstraction. As abstraction means representation of essential features without including the background details.

6 Implementing OOP concepts in C++ Inheritance is implemented in C++ by specifying the name of the (base) class from which the class being defined (the derived class) has to inherit from.The class which is inherited is called the base class or super class. And class which inherits from other class is called derived class or sub class. In C++ an Abstract Class is the one, which defines an interface, but does not necessarily provide implementations for all its member functions. An abstract class is meant to be used as the base class from which other classes are derived. The derived class is expected to provide implements for the member functions that are not implemented in the base class. A derived class that implements all the missing functionality is called a Concrete Class. A concrete class derives from its abstract class.

7 Implementing OOP concepts in C++ Polymorphism is implemented through virtual functions,overloaded functions and overloaded operators. A virtual function is used to specify the interface in abstract class. The term overloaded function' refers to a function having more than one distinct meanings. The term overloaded operator is used when more than one meaning are defined for an operator.

8 Function Overloading A function name having several definitions that are differentiable by the number or types of their arguments, is known as an overloaded function and this process is known as Function Overloading. Function overloading is needed in order to simulate real world objects in programming. Function overloading not only implements polymorphism but also reduces number of comparisons in a program and thereby makes the program run faster. The key to function overloading is a functions argument list which is also known as the functions signature. If two functions are having same number and types of arguments in the same order, they are said to have the same signature.

9 Function Overloading Let us take an example to understand the process of function overloading- The following code overloads a function area to compute areas of circle,rectangle and triangle. float area (float radius) { return (3.14*radius*radius); } float area (float length, float breadth ) { return (length*breadth); } float area (float side1,float side2, float side3) { float s = (side1+side2+side3) /2; float ar = sqrt (s*(s-side1) * (s-side2) * (s-side3) ); return ar ; }

10 Restrictions on Overloaded Functions Any two functions in a set of overloaded functions must have different argument lists. Overloading functions with argument lists of the same types, based on return type alone, is an error. Member functions cannot be overloaded solely on the basis of one being static and the other non static. Typedef declarations do not define new types; they introduce synonyms for existing types. They do not affect the overloading mechanism.

11 Calling overloaded functions Overloaded functions are called just like other functions. The number and type of arguments determine which functions should be invoked. A function call first matches the prototypes available with the number and type of arguments provided with the function call and then calls the appropriate function for execution. But sometimes there might be ambiguity between float and double values or say int or long values. For instance if we want to invoke the function with the following declaration – void prnsqr (double d); with the value This value may also be assumed to be float as well as double. Now to avoid such ambiguity, we can use constant suffixes (F,L,U, etc.) to distinguish between such values. For instance the suffix F (312.32F) makes it a float.

12 Steps involved in finding the best match A call to an overloaded function is resolved to a particular instance of the function through a process known as argument matching, which can be termed as a process of disambiguation. Argument matching involves comparing the actual arguments of the call with the formal arguments of each declared instance of the function. There are three possible cases, a function call may result in: (a) A match – a match is found for the function call. (b) No match – No match is found for the function call. (c) Ambiguous Match – More than one defined instance match for the function call.

13 Steps involved in finding the best match-2 Search for an Exact Match– if the type of the actual argument exactly matches the type of one defined instance, the compiler invokes that particular instance. For example – void afunc (int); // overloaded functions void afunc (double); afunc (0) ; // exact match. Matches afunc (int) 0 (Zero) is of type int, thus the exactly matches afunc (int) A match through promotion or conversion rules – if no exact match is found, an attempt is made to achieve a match through promotion of the actual argument. i.e. by conversion of data types. As char,short can be converted into int data type and float can be converted into double.

14 Advantage of OOP 1.Re –Use of code- In OOP objects allows related objects to share code.Encapsulation allows class definitions to be re-used in other applications. 2.Ease of Comprehension – The classes can be set up to closely represent the generic application concepts and processes. OOP codes are more near to real-world models than other programming methodologist's codes. 3.Ease of fabrication and maintenance – The concepts such as encapsulation data abstraction allow for very clean designs. 4.Easy redesign and extension – The same concepts facilitate easy redesign and extension.

15 Assignments 1.How are classes and objects implemented? 2.Discuss the relation between abstract and concrete classes. 3.What is polymorphism ? How does function overloading implement polymorphism ? 4.What do you understand by Data Encapsulation and Data Hiding? Also give an example to illustrate both.

Download ppt "Object Oriented Programming INTRODUCTION : The object oriented programming paradigm is the latest in the software development and the most adopted one."

Similar presentations

Ads by Google