0% found this document useful (0 votes)
52 views

Polymorphism

This document discusses polymorphism in C++. It defines polymorphism as one name having multiple forms, and describes the two types: compile-time and run-time polymorphism. Compile-time polymorphism includes function overloading and operator overloading, while run-time polymorphism uses virtual functions and involves dynamic binding. Virtual functions allow determining which function to call at run-time based on the object type. Pure virtual functions are functions declared but not defined in a base class, making the class abstract.

Uploaded by

Arun Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Polymorphism

This document discusses polymorphism in C++. It defines polymorphism as one name having multiple forms, and describes the two types: compile-time and run-time polymorphism. Compile-time polymorphism includes function overloading and operator overloading, while run-time polymorphism uses virtual functions and involves dynamic binding. Virtual functions allow determining which function to call at run-time based on the object type. Pure virtual functions are functions declared but not defined in a base class, making the class abstract.

Uploaded by

Arun Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

UNIT-2

Polymorphism:
It means one mean having multiple forms. These are two types of polymorphism,namely,compile time and run time polymorphism.

Polymorphism

Compile time Polymorphism

Run Time Polymorphism

Function overloading

Operator overloading

Virtual Function

Compile Time Polymorphism or Early Binding or Static Binding:


The overloaded menber functions are selected for invoking by matching arguments,both type and number. The compile knows this information at the complie time and,therfore,compiler is able to select the appropriate function for a particular call at the compiler time itself. This is called early or Static Binding/Static Linking.

Runtime Polymorphism/ Late Binding/ Dynamic Binding:


An appropriate member function is selected while the program is running. C++ supports run time polymorphism with the help of virtual functons. This is called as runtime polymorphism or late binding or dynamic binding. Dynamic binding requires use of pointers to objects.

Object Pointer are useful in creating objects at runtime.It can be used to access the publc menbers of an objects,along with an arrow operator.

UNIT-2

Virtual Functions:
When a function is made virtual,c++ determines which function to use at run time based on the type of object pointed to by the base pointer,rather than the type of the pointer. Syntax: Virtual returntype fun_name(arg list) {.}

Rules for Virtual Function:


1. 2. 3. 4. 5. 6. The virtual functions must be members of some class. They cant be static members. They are accessed by using object pointers. A virtual function can be a friend of another class. A virtual function in a base class must be defined,even though it may not be used. The prototype of the base class version of a virtual function and all the derived class versions must be identical,otherwise, the Virtual function mechanism is ignored. 7. We cant have virutal constructors,but we can have virtual destructors. 8. We cant use a pointer to a derived class to access an object of the base type. 9. If a virtual function is defined in the base class, it need not be necessarily redefined in the derived class. In such cases, calls will invoke the base function. 10. When a base pointer points to a derived class, incrementing or decrementing it will not make it to point to the next object of the derived class.

Pure Virtual Function:


A virtual function, equated to zero is called a pure virtual function. It is a function declared in a base class that has no definition relative to the base class. A Class containing such pure functions is called as Abstract Class. Example: virutal void display()=0;

UNIT-2

class Base { public: void display() { cout<<\n Display Base; } virtual void show() { cout<<\nShow Base: } } class Derived : public Base { public: void display() { cout<<\n Display Derived:; } virtual void show() { cout<<\n Show Derived:; } }; int main( ) { Base B; Derived D; Base *bptr; // pointer to object cout<<\n bptr points to Base; bptr=&B; bptrdisplay(); bptrshow(); cout<<\n bptr points to Derived; bptr=&D; bptrdisplay(); bptrshow(); return 1; }

You might also like