Polymorphism in C++

Submitted by: 
Language: 

Polymorphism in C++

In this part you will learn:
1. Polymorphism
2. C syntax
3. Showing output
In this tutorial I will teach you about the concept of Polymorphism in C++.

What is a Polymorphism?
Polymorphism enables to write programs that process objects of classes (that are part of the same hierarchy) as if they are all objects of the hierarchy’s base class. In polymorphism we can access functions of the derived class object through base class pointer.
Person Class

Before we can start writing the program we need to have a little knowledge about virtual functions. To achieve this functionality we need to declare some functions virtual. We need to override the functions in the derived class and declare those functions virtual in the base class.

This is because when we call a function using base class pointer which is pointing to derive class object the compiler sees the type of pointer that is being used to call the function and executes the function of the base class. Whereas if we make the function in the base class virtual then in that case the compiler sees the type of object the pointer is pointing at and then executes that function of the derived class.
Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<iostream>
  4. using namespace std;

These are the header files that we need for coding. The main header is stdio.h which provides us with most of the functions for coding in C and the second header files is just for using a function which can pause the screen until the user hits any key.

  1. class Person
  2. {
  3. protected:
  4. char name[40];
  5.  
  6. public:
  7. void getname()
  8. {
  9. cout<<"Enter name:";
  10. cin>>name;
  11. }
  12. void putname()
  13. {
  14. cout<<endl<<"Name is:"<<name;
  15. }
  16.  
  17. virtual void getdata(){};
  18. virtual bool isOutstanding(){};
  19.  
  20. };

This is our Base class with one protected data member and four public functions. Two of the functions void getdata() and void bool isOutstanding are declared as virtual functions. They are made virtual because we need to use the function in the derived class.

  1. class Professor: public Person
  2. {
  3. private:
  4. int numpubs;
  5. public:
  6. void getdata()
  7. {
  8. Person::getname();
  9. cout<<"Enter publications:";
  10. cin>>numpubs;
  11. }
  12. bool isOutstanding()
  13. {
  14. return (numpubs>100)?true:false;
  15. }
  16. };

This is our first derived class. It only has two public functions and a private member to store the number of publications of a Person. The two functions are overriding the functions of the Person class. The function isOutstanding() checks if the number of publications of a person is greater than 100 then it returns true, otherwise false.

  1. class Student: public Person
  2. {
  3. private:
  4. float gpa;
  5. public:
  6. void getdata()
  7. {
  8. Person::getname();
  9. cout<<"Enter GPA:";
  10. cin>>gpa;
  11. }
  12. bool isOutstanding()
  13. {
  14. return(gpa>3.5)?true:false;
  15. }
  16. };

This is the second derived class. It has also one private member and two public functions. These functions also override the functions of the base class. The function isOutstanding() checks if the gpa of a student is greater than 3.5 it returns true, otherwise false.

  1. int main()
  2. {
  3. Person* persPtr[2];
  4.  
  5. Professor p;
  6. persPtr[0]=&p;
  7. persPtr[0]->getdata();
  8. Student s;
  9. persPtr[1]=&s;
  10. persPtr[1]->getdata();

Now in this piece of code we have made a pointer array of Base class which in this case is the Person class and hardcoded the size to 2. Then we have assigned the address of an object of Professor class to one of the index of the pointer and then using that index we have called the function getdata(). Now this function getdata() which is present in the Person class will be executed because the function in the Base class was declared as virtual. The same is happening with the second index of the pointer array.

  1. for(int i=0;i<2;i++)
  2. {
  3. persPtr[i]->putname();
  4. if(persPtr[i]->isOutstanding())
  5. {
  6. cout<<endl<<"This Person is outstanding"<<endl;
  7. }
  8. }
  9.  
  10.  
  11. system("pause");

In this for loop, the function isOutstanding() is called for both the Professor class and the Student class through the pointers of the base class. This function checks a simple condition and gives the corresponding output.
Execute > compile
then Execute > run
Output
output


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.