C++ Program to define an Abstract Class in Polymorphism

By | 03.01.2017

Abstract Class in Polymorphism


Write a C++ Program  to define an Abstract Class in Polymorphism. Here’s a Simple C++ Program  to define an Abstract Class in Polymorphism in C++ Programming Language.


What is Polymorphism in C++ ?


The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.

C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.


Virtual Function : :


A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don’t want static linkage for this function.


Below is the source code for C++ Program  to define an Abstract Class in Polymorphism which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Program to define an Abstract Class in Polymorphism  */

#include<iostream>
#include <string.h>
using namespace std;

class Card // An abstract class
{
        protected:
                char recipient[20];
        public:
                virtual void greeting()=0; // A pure virtual function
} ;
class Holiday : public Card
{
        public:
                Holiday( char r[ ] )
                {
                        strcpy(recipient,  r) ;
                }
                void greeting() // implementation of function in sub class
                {
                        cout <<  "Dear "  <<  recipient  << endl  ;                           cout <<  "Season's Greetings!\n\n" ;
                }
} ;

class Birthday : public Card
{
        private:
                int age;
        public:
                Birthday ( char r[ ], int years )
                {
                        strcpy(recipient, r);
                        age = years;
                }
                void greeting()  // Implementation of function in sub class
                {
                        cout << "Dear " << recipient << ",\n" ;
                        cout << "Happy "  << age  << "th" << " Birthday\n\n";
                }
} ;

class Holi : public Card
{
        private:
                int colors;
        public:
                Holi( char r[ ], int c )
                {
                        strcpy(recipient, r) ;
                        colors    = c;
                 }
                void greeting()  // Implementation of function in subclass
                {
                        cout << "Dear "  << recipient << ",\n";
                        cout << " Happy holi and lots of colors for you\n";
                        for ( int j=0; j < colors; j++ )
                                cout << "*";
                        cout << "\n\n" ;
                }
} ;


int main ()
{
                Holiday   hol ( "Codez" );
                hol.greeting();
                Birthday  bd( "CodezClub", 21 );
                bd.greeting();
                Holi holi( "Max", 7 );
                holi.greeting();
return 0;
}

OUTPUT : :


/*  C++ Program to define an Abstract Class in Polymorphism  */

Dear Codez
Season's Greetings!

Dear CodezClub,
Happy 21th Birthday

Dear Max,
 Happy holi and lots of colors for you
*******

Process returned 0

Above is the source code and output for C++ Program to define an Abstract Class in Polymorphism which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments