The perfect place for easy learning...

OOPs using C++

×

List of Programs


C++ Practical Programs


Aim


Write a C++ program to use pointer for both base and derived classes and call the member function. Use Virtual keyword.

Procedure


Implementation


Example
#include <iostream>
using namespace std;

class Weapon
{
    public:
    virtual void features()
    {
        cout << "Loading weapon features.\n";
    }
};
class Bomb : public Weapon
{
    public:
    void features()
    {
        this->Weapon::features();
        cout << "Loading bomb features.\n";
    }
};
class Gun : public Weapon
{
    public:
    void features()
    {
        this->Weapon::features();
        cout << "Loading gun features.\n";
    }
};
class Loader
{
    public:
    void loadFeatures(Weapon *weapon)
    {
        weapon->features();
    }
};
int main()
{
    Loader *l = new Loader;
    Weapon *w;
    Bomb b;
    Gun g;
    w = &b;
    l->loadFeatures(w);
    w = &g;
    l->loadFeatures(w);
    return 0;
}

Result


C++ Programming Tutorial

   Download Source Code


Place your ad here
Place your ad here