Load features weapon program example using Virtual Function in C++


Example Program : 1

The program defines a base class called Weapon which has a constructor and a member function features(). Two derived classes, Bomb and Gun, are defined, which also have their own implementation of the features() function. A class called Loader is defined which has a member function called loadFeatures() that takes a pointer to a Weapon object and calls the features() function of that object.

In the main function, an instance of the Loader class is created, and two instances of the derived classes Bomb and Gun are created. A pointer to a Weapon object is created, and it is assigned the address of the Bomb object. The loadFeatures() function is then called with this pointer as an argument. The features() function of the Bomb object is called, which in turn calls the features() function of the Weapon class and then displays the message "Loading bomb features".

Next, the same thing is done with the Gun object. The features() function of the Gun object is called, which calls the features() function of the Weapon class and then displays the message "Loading gun features". Overall, this program demonstrates how polymorphism and inheritance work in C++, allowing derived classes to override and extend the functionality of the base class.

Source Code

#include<iostream>
using namespace std;
class Weapon
{
  public:
    Weapon()
    {
       cout<<"Loading weapon features.\n";
    }
    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;
}
To download raw file Click Here

Output

Loading weapon features.
Loading weapon features.
Loading weapon features.
Loading weapon features.

Example Program : 2

The program demonstrates the concept of inheritance in C++. It defines a base class Weapon with a function loadFeatures(), which is overridden by its derived classes Bomb and Gun. In the main() function, objects of all three classes are created: w of type Weapon, b of type Bomb, and g of type Gun.

The loadFeatures() function is called on each of these objects, which invokes the appropriate function based on the object's type. This demonstrates the concept of polymorphism, where the same function loadFeatures() behaves differently for objects of different classes. The base class pointer w is also used to call the loadFeatures() function, which demonstrates the concept of dynamic polymorphism achieved through virtual functions in C++.

Source Code

#include<iostream>
using namespace std;
class Weapon
{
  public:
  void loadFeatures()
  {
    cout<<"Loading weapon features.\n";
  }
};

class Bomb : public Weapon
{
  public:
  void loadFeatures()
  {
    cout<<"\n Loading bomb features.\n";
  }
};

class Gun : public Weapon
{
  public:
  void loadFeatures()
  {
    cout<<"\n Loading gun features.\n";
  }
};
int main()
{
  Weapon *w = new Weapon;
  Bomb *b = new Bomb;
  Gun *g = new Gun;
  w->loadFeatures();
  b->loadFeatures();
  g->loadFeatures();
  return 0;
}
To download raw file Click Here

Output

Loading weapon features.
Loading bomb features.
Loading gun features.


Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples