Polymorphism using Abstract Class and Virtual Functions || Polymorphism || C++



In this program, we are going to see polymorphism using Abstract class and virtual functions in C++ Programming Language. 
 


The Code given below can be used in gcc/g++ Compilers: -


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

// An abstract class
class Card 
{
    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("HelpForCoders");
    hol.greeting();
    Birthday bd("HelpForCoders", 20);
    bd.greeting();
    Holi holi("Sunny", 7);
    holi.greeting();
    return 0;
}

//.......Coded by RISHAB NAIR


The Code given below can be used in Turbo C++ Compilers: -

#include <iostream.h>
#include <string.h>
#include <conio.h>

// An abstract class
class Card 
{
    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";
    }
};

void main()
{
    clrscr();
    Holiday hol("HelpForCoders");
    hol.greeting();
    Birthday bd("HelpForCoders", 20);
    bd.greeting();
    Holi holi("Sunny", 7);
    holi.greeting();
    getch();
}

//.......Coded by RISHAB NAIR


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post