Q:

C++ Program to define an Abstract Class in Polymorphism

0

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++ ?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now