- C++ Programming Basics
- C++ Tutorial
- C++ Environment Setup
- C++ Character Set
- C++ Keywords
- C++ Identifiers
- C++ Constants
- C++ Punctuators
- C++ Program Structure
- C++ Basic Syntax
- C++ Comments
- C++ Basic Programs
- C++ Input Output Operator
- C++ Input Output Stream
- C++ Type & Variable
- C++ Data Types
- C++ Data Type Modifiers
- C++ Variables
- C++ Variable Types
- C++ Variable Scope
- C++ Storage Classes
- C++ Formatting Output
- C++ Operators
- C++ Operators
- C++ Type Conversion
- C++ Numbers
- C++ Assignment Operator
- C++ Shorthand
- C++ Flow of Control
- C++ Statements
- C++ Flow Control
- C++ Decision Making
- C++ if if-else if-else-if switch
- C++ Loops
- C++ for while do-while Loop
- C++ break continue goto
- C++ Standard Library
- C++ Standard Library
- C++ Header Files
- C++ Character String
- C++ Mathematical Functions
- C++ Functions
- C++ Functions
- C++ Function Types
- C++ Function Prototype
- C++ Function Call
- C++ Function Return
- C++ Friend Function
- C++ Scope Rules
- C++ Arrays & Strings
- C++ Arrays
- C++ One Dimensional Arrays
- C++ Two Dimensional Arrays
- C++ Strings
- C++ Data Structure
- C++ Data Structure
- C++ Access Structure Member
- C++ Nested Data Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ typedef
- C++ #define
- C++ Programming Pointers
- C++ Pointers
- C++ Memory Map
- C++ Free Store
- C++ Declare Initialize Pointers
- C++ Dynamic Memory Allocation
- C++ Pointers & Arrays
- C++ Pointers & Const
- C++ Pointers & Functions
- C++ Pointers & Structures
- C++ Objects as Function Arguments
- C++ Pointers & Objects
- C++ References
- C++ File Handling
- C++ File Handling
- C++ File Streams
- C++ Data Files
- C++ Opening & Closing Files
- C++ Steps to Process Files
- C++ Change Stream Behaviour
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Object Oriented
- C++ Object Oriented
- C++ Function Overloading
- C++ Classes & Objects
- C++ Constructors & Destructors
- C++ Inheritance
- C++ Encapsulation
- C++ Polymorphism
- C++ Data Abstraction
- C++ Interfaces
- C++ Programming Advance
- C++ Linked Lists
- C++ Stacks
- C++ Queues
- C++ Date Time
- C++ Preprocessors
- C++ Exception Handling
- C++ Namespaces
- C++ Dynamic Memory
- C++ Multithreading
- C++ Templates
- C++ Signal Handling
- C++ Web Programming
- C++ Programming Examples
- C++ Programming Examples
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Polymorphism
In C++, polymorphism means having many forms. Generally, polymorphism occurs when there is a hierarchy of the classes and they are related by the inheritance.
C++ Polymorphism Example
Here is an example demonstrating polymorphism in C++. Here the base class is derived by the other two classes:
/* C++ Polymorphism - Example Program */ #include<iostream.h> #include<conio.h> class JAVAPOLYMORPHISMEXAMPLE { protected: int wid, hei; public: JAVAPOLYMORPHISMEXAMPLE(int a=0, int b=0) { wid=a; hei=b; } int area() { cout<<"Parent class area\n"; return 0; } }; class RECTANGLE:public JAVAPOLYMORPHISMEXAMPLE { public: RECTANGLE(int a=0, int b=0):JAVAPOLYMORPHISMEXAMPLE(a, b) { } int area() { cout<<"Rectangle class area\n"; return(wid*hei); } }; class TRIANGLE:public JAVAPOLYMORPHISMEXAMPLE { public: TRIANGLE(int a=0, int b=0):JAVAPOLYMORPHISMEXAMPLE(a, b) { } int area() { cout<<"Triangle class area\n"; return(wid*hei/2); } }; void main() { clrscr(); JAVAPOLYMORPHISMEXAMPLE *sh; RECTANGLE rec(10, 7); TRIANGLE tri(10, 5); sh = &rec; sh->area(); sh = &tri; sh->area(); getch(); }
Here is the sample output of the above C++ program:

As you can see, the output produced is incorrect. Now here we are going to make small changes in JAVAPOLYMORPHISMEXAMPLE class
class JAVAPOLYMORPHISMEXAMPLE { protected: int wid, hei; public: JAVAPOLYMORPHISMEXAMPLE(int a=0, int b=0) { wid=a; hei=b; } virtual int area() { cout<<"Parent class area\n"; return 0; } };
Here is the complete version of the above program after modifying the code:
/* C++ Polymorphism - Example Program */ #include<iostream.h> #include<conio.h> class JAVAPOLYMORPHISMEXAMPLE { protected: int wid, hei; public: JAVAPOLYMORPHISMEXAMPLE(int a=0, int b=0) { wid=a; hei=b; } virtual int area() { cout<<"Parent class area\n"; return 0; } }; class RECTANGLE:public JAVAPOLYMORPHISMEXAMPLE { public: RECTANGLE(int a=0, int b=0):JAVAPOLYMORPHISMEXAMPLE(a, b) { } int area() { cout<<"Rectangle class area\n"; return(wid*hei); } }; class TRIANGLE:public JAVAPOLYMORPHISMEXAMPLE { public: TRIANGLE(int a=0, int b=0):JAVAPOLYMORPHISMEXAMPLE(a, b) { } int area() { cout<<"Triangle class area\n"; return(wid*hei/2); } }; void main() { clrscr(); JAVAPOLYMORPHISMEXAMPLE *sh; RECTANGLE rec(10, 7); TRIANGLE tri(10, 5); sh = &rec; sh->area(); sh = &tri; sh->area(); getch(); }
Here is the sample output of the above C++ program:

Virtual Function
You are free to declare virtual functions in C++ just by using the keyword virtual. Here is a small code fragment, showing the example of pure virtual function in C++:
class JAVAPOLYMORPHISMEXAMPLE { protected: int wid, hei; public: JAVAPOLYMORPHISMEXAMPLE(int a=0, int b=0) { wid=a; hei=b; } // this is the pure virtual function virtual int area() = 0; };
Here = 0 tells the C++ compiler that this function has no body and the above virtual function will be called as pure virtual function in C++. Here is the complete version of the above C++ program:
/* C++ Polymorphism - Example Program */ #include<iostream.h> #include<conio.h> class JAVAPOLYMORPHISMEXAMPLE { protected: int wid, hei; public: JAVAPOLYMORPHISMEXAMPLE(int a=0, int b=0) { wid=a; hei=b; } virtual int area() = 0; }; class RECTANGLE:public JAVAPOLYMORPHISMEXAMPLE { public: RECTANGLE(int a=0, int b=0):JAVAPOLYMORPHISMEXAMPLE(a, b) { } int area() { cout<<"Rectangle class area\n"; return(wid*hei); } }; class TRIANGLE:public JAVAPOLYMORPHISMEXAMPLE { public: TRIANGLE(int a=0, int b=0):JAVAPOLYMORPHISMEXAMPLE(a, b) { } int area() { cout<<"Triangle class area\n"; return(wid*hei/2); } }; void main() { clrscr(); JAVAPOLYMORPHISMEXAMPLE *sh; RECTANGLE rec(10, 7); TRIANGLE tri(10, 5); sh = &rec; sh->area(); sh = &tri; sh->area(); getch(); }
Here is the sample run of this C++ program:

More Example
Here is one more example program, demonstrating polymorphism in C++
/* C++ Polymorphism - Example Program */ #include<iostream.h> #include<conio.h> class BASE { protected: int a; int b; int res; public: void setvar(int x, int y) { a=x; b=y; } virtual void op() = 0; int getresult() { return res; } }; class ADD:public BASE { public: void op() { res=a+b; } }; class SUB:public BASE { public: void op() { res=a-b; } }; void main() { clrscr(); int m, n; BASE *bo; ADD ao; SUB so; cout<<"Enter two number: "; cin>>m>>n; bo=&ao; bo->setvar(m, n); bo->op(); cout<<"\nResult of Summation: "<<bo->getresult(); bo=&so; bo->setvar(m, n); bo->op(); cout<<"\nResult of Subtraction: "<<bo->getresult(); getch(); }
Below is the sample run of the above C++ program:

« Previous Tutorial Next Tutorial »