GIK Institute of Engineering Sciences & Technology
Faculty of Computer Science and Engineering
Time: 1.5 hours CS 102 Lab Max Marks: 10
Section: C Lab No: 11 April 24, 2018
Activity #01
Define a class Weapon with virtual function feature to print a message like āloading weapon
featuresā. Make two more classes Bomb and Gun. Inherit class Weapon in both of them and make
function feature in both and print a suitable message like above mentioned.
Define another class Loader with a function load_feature(). The prototype of this method may
be like:
void loadFeatures(Weapon *weapon)
Within this method, call the function feature of object weapon that is passed to this method.
In main(), make pointer objects of class Loader and Weapon. Another two objects of class Gun
and Bomb. Assign the addresses of Bomb and Gun class objects to the object of class weapon.
Call the Loader class method with weapon class object like:
w = &b;
l->loadFeatures(w);
w = &g;
l->loadFeatures(w);
Discuss what actually happens.
Solution:
1. #include <iostream>
2. using namespace std;
3.
4. class Weapon
5. {
6. public:
7. virtual void features()
8. { cout << "Loading weapon features.\n"; }
9. };
10.
11. class Bomb : public Weapon
12. {
13. public: