Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. In this assignment, using C++ you will be using inheritance and polymorphism

ID: 3590181 • Letter: 1

Question

1. In this assignment, using C++ you will be using inheritance and polymorphism to implement a similar structure to the drawing/shape example in the lecture. For this, you will need to design and write a base class and three derived classes that inherit from the base (similar to the shape class and the derived shape classes). You will also need a “manager” object that will hold a (fixed) array of pointers to your base type (similar to the drawing class).

2. The base class may or may not have any member variables itself, depending on your design. The derived classes should have at least one member each to describe the structure of that class.

3. There should be at least one virtual function in the base class that each derived class will override. The base class’ virtual function should be pure virtual (a.k.a. abstract) unless the base does indeed have an implementation. Each derived type’s override of the virtual function should cause some output to std::cout to indicate that it was invoked.

4. Each class should include a full set of constructors to initialize their member(s) as well as the base class. Each derived class should call the base class’ constructor in their initialization lists if needed.

5. Your “manager” class will have an array of pointers to the base type that it manages and a method to add objects (by pointer-to-base) into the array and track how many items have been added.

6. Include a function in the manager class that will loop through all of the objects in the array and invoke the virtual function for each element (just like drawing::draw looped through all of its shapes).

7. In your main function, instantiate a “manager” object.

8. Add several objects of your types to the manager object. These objects will have to be created dynamically (with new) and passed into the function you made in (5) that will add the objects to the array.

9. Call the manager object’s function from (6) that will loop through all objects you just added.

10. Make sure that the manager’s constructor cleans up all of its objects when the manager object goes out of scope.

11. Place all classes in their own headers (protecting against multiple inclusion) and in a namespace of your own choosing.  

Explanation / Answer

Please save the below code in 7 different files as asked in your question. Then compile and run.

You can modify, add, any member of any class as per your requirement. I have taken care all of your questions, mainly inheritance and polymorphysm.

Note: Please like the answer. Put comments in comment section if you have any query.

/***************************************/

/*
* main.cpp
*
* Created on: 11-Oct-2017
*      Author:
*/

#include <iostream>
#include "manager.h"
#include "Derived.h"
using namespace std;
int main()
{
   manager mng; //create instance of manager q7

   // add objects of different types q8
   Derived1* d11=new Derived1;
   mng.addObj(d11);
   Derived1* d12=new Derived1;
   mng.addObj(d12);
   Derived2* d21=new Derived2;
   mng.addObj(d21);
   Derived2* d22=new Derived2;
   mng.addObj(d22);
   Derived3* d31=new Derived3;
   mng.addObj(d31);
   Derived3* d32=new Derived3;
   mng.addObj(d32);
   Derived1* d13=new Derived1;
   mng.addObj(d13);

   cout<<"You have added "<<mng.getObjCount()<<" Objects"<<endl;
   //call function to view objects q9
   mng.viewObjects();
   return 0;
}

/****************************/

/*
* manager.h
*
* Created on: 11-Oct-2017
*      Author:
*/
#include <iostream>
#include "Base.h"
#ifndef MANAGER_H_
#define MANAGER_H_

class manager {

   int count;
public:
   Base* obj[];
   manager();
   void addObj(Base* );
   int getObjCount();
   void viewObjects();
   virtual ~manager();
};

#endif /* MANAGER_H_ */


/****************************/

/*
* manager.cpp
*
* Created on: 11-Oct-2017
*      Author:
*/

#include "manager.h"

manager::manager() {
   // TODO Auto-generated constructor stub
   count=0;
}

void manager::addObj(Base* bptr) {
   if(bptr!=NULL)
   {
       obj[count]=bptr;
       count++;
   }
}

int manager::getObjCount() {
   return count;
}

void manager::viewObjects() {
   for(int i=0;i<count;i++)
       {
           obj[i]->fun();
       }
}
manager::~manager() {
   // TODO Auto-generated destructor stub

   delete[] *obj; // deleting object when out of scope q10

}


/**********************************/

/*
* Derived.h
*
* Created on: 11-Oct-2017
*      Author:
*/
#include "Base.h"
#ifndef DERIVED_H_
#define DERIVED_H_

class Derived1:public Base {
public:
   Derived1();
   void fun();

};

class Derived2:public Base {
public:
   Derived2();
   void fun();

};

class Derived3:public Base {
public:
   Derived3();
   void fun();

};

#endif /* DERIVED_H_ */


/************************/

/*
* Derived.cpp
*
* Created on: 11-Oct-2017
*      Author:
*/
#include <iostream>
#include "Derived.h"

using namespace std;
Derived1::Derived1() {
   // TODO Auto-generated constructor stub

}

void Derived1::fun() {
   cout<<"fun() called from Derived1"<<endl; //you can implement your own code here
}

Derived2::Derived2() {
   // TODO Auto-generated constructor stub

}

void Derived2::fun() {
   cout<<"fun() called from Derived2"<<endl; //you can implement your own code here
}

Derived3::Derived3() {
   // TODO Auto-generated constructor stub

}

void Derived3::fun() {
   cout<<"fun() called from Derived3"<<endl; //you can implement your own code here
}


/***********************/

/*
* Base.h
*
* Created on: 11-Oct-2017
*      Author:
*/

#ifndef BASE_H_
#define BASE_H_

class Base {
public: // you can add any members you required as your need
   Base();
   virtual void fun()=0; //pure virtual function, Base class cannot be instantiated
                           // this function is overridden in all derived class (polimorphysm)
   virtual ~Base();
};

#endif /* BASE_H_ */


/***********************/

/*
* Base.cpp
*
* Created on: 11-Oct-2017
*      Author:
*/

#include "Base.h"

Base::Base() {
   // TODO Auto-generated constructor stub

}

Base::~Base() {
   // TODO Auto-generated destructor stub
}


/***************************/