Chapter 6 Polymorphism

Share Embed Donate


Short Description

Polymorphism...

Description

Prof.Manoj S.Kavedia (9860174297)([email protected])

66

Polymorphism

Syllabus Concepts of polymorphism, types of polymorphism, Overloading & overriding, Virtual function, Static & dynamic binding.

Polymorphism Q1.Define Polymorphism? Ans. The word ‘poly’ originated from a Greek word meaning many and ‘morphism’ from a Greek word meaning form, and thus ‘polymorphism’ means many forms. In object oriented programming, polymorphism refers to identically named methods (member functions) that have different behavior depending on the type of object they refer. Polymorphism is the process of defining a number of objects of different classes into a group and call the methods to carry out the operation of the objects using different function calls. Polymorphism means ‘to carry out different processing steps by functions having same messages’ - It treats objects of related classes in a generic manner. The keyword virtual is used to perform the polymorphism concept in C++. Polymorphism refers to the run time binding to a pointer to a method. It is one of the important of object-oriented programming. It needs one name and multiple Forms. The concept of polymorphism is implemented using function and operator overloading. The overloaded member functions are selected for invoking the function calls depending on the type and the number of arguments. When the function name and Prototype is same in the base class and derived class which is not an function overloading. But it is called a function overloading. Polymorphism is the ability of an object or reference to take many different forms at different instances. These are of two types one is the "compile time polymorphism" and other one is the "run-time polymorphism". Compile time polymorphism: In this method object is bound to the function call at the compile time itself. Run time polymorphism: In this method object is bound to the function call only at the run time.

1

Prof.Manoj S.Kavedia (9860174297)([email protected])

PolyMorphism PolyMorphism

Compile Time Compile Time Polymorphism Polymorphism

Function Function OverLoading OverLoading

Operator Operator OverLoading OverLoading

Rum Time Rum Time Polymorphism Polymorphism

Virtual Virtual Function Function

Q2.Describe what is polymorphism? State its use?State features of polymorphism Ans. Polymorphism refers to the ability to call different functions by using only one type of function call. Suppose a programmer wants to code vehicles of different shapes such as circles, squares, rectangles, etc. One way to define each of these classes is to have a member function for each that makes vehicles of each shape. Another convenient approach the programmer can take is to define a base class named Shape and then create an instance of that class. The programmer can have array that hold pointers to all different objects of the vehicle followed by a simple loop structure to make the vehicle, as per the shape desired, by inserting pointers into the defined array. This approach leads to different functions executed by the same function call. Polymorphism is used to give different meanings to the same concept. This is the basis for Virtual function implementation. In polymorphism, a single function or an operator functioning in many ways depends upon the usage to function properly. In order for this to occur, the following conditions must apply: • All different classes must be derived from a single base class. In the above example, the shapes of vehicles (circle, triangle, rectangle) are from the single base class called Shape. • The member function must be declared virtual in the base class. In the above example, the member function for making the vehicle should be made as virtual to the base class. Features and Advantages of the concept of Polymorphism Applications are Easily Extendable Once an application is written using the concept of polymorphism, it can easily be extended, providing new objects that conform to the original interface. It is unnecessary to recompile original programs by adding new types. Only re-linking is necessary to exhibit the new changes along with the old application.0020By utilizing the concept of polymorphism, time and work effort is reduced in addition to making future maintenance easier. • Helps in reusability of code. 2

Prof.Manoj S.Kavedia (9860174297)([email protected]) • •

Provides easier maintenance of applications. Helps in achieving robustness in applications.

Q3.List different types of polymorphism Ans. C++ provides three different types of polymorphism. Virtual functions Function name overloading Operator overloading In addition to the above three types of polymorphism, there exist other kinds of polymorphism: • • •

• •

run-time compile-time

Q4.What is Binding? Ans.Binding refers to the act of associating an object or a class with its member. If we can call a method fn() on an object obj of a class CC, we say that the object obj is binded with the method fn(). This happens at compile time and is known as static or compile time binding. The calls to the virtual member functions are resolved during run-time. This mechanism is known as dynamic binding. The most prominent reason why a virtual function will be used is to have a different functionality in the derived class. The difference between a non-virtual member function and a virtual member function is, the non-virtual member functions are resolved at compile time. Q5.What is Early Binding or compile time or static Binding polymorphism Ans.Selecting a function in normal way, during compilation time is called as early binding or static binding or static linkage. During compilation time, the C++ compiler determines which function is used based on the parameters passed to the function or the function’s return type. The compiler then substitutes the correct function for each invocation. Such compiler based substitutions are called static linkage. By default, C++ follows early binding. With early binding, one can achieve greater efficiency. Function calls are faster in this case because all the information necessary to call the function are hard coded. The purest object oriented programming language like Small talk permits only run time binding of the methods, whereas C++ allows both compile time binding and run time binding. Q6.Write program to demonstrate compile time polymorphism Ans. Compile time Polymorphism #include #include using namespace std; int Add(int nX, int nY) { return nX + nY; 3

Prof.Manoj S.Kavedia (9860174297)([email protected]) } int Subtract(int nX, int nY) { return nX - nY; } int Multiply(int nX, int nY) { return nX * nY; } int main() { int nX; cout > nX; int nY; cout > nY; int nOperation; do { cout > nOperation; } while (nOperation < 0 || nOperation > 2); int nResult = 0; switch (nOperation) { case 0: nResult = Add(nX, nY); break; case 1: nResult = Subtract(nX, nY); break; case 2: nResult = Multiply(nX, nY); break; } cout
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF