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

In this exercise you will be working with typeid(). Download the file SomeHeader

ID: 3688619 • Letter: I

Question

In this exercise you will be working with typeid(). Download the file SomeHeader.h, which defines a class B that inherits from the base class A. The initVec() method creates a vector of type A* and pushes several objects into it. For this exercise, write a main() function that will do the following. Declare a vector of type A*, and use the initVec(vector & myVec) to populate it. Now, iterate over all vector elements and print the name of each element, one item per line.

#ifndef LA8_SomeHeader_h
#define LA8_SomeHeader_h
#include <iostream>
#include <vector>

using namespace std;
class A {
  
public:
    A(){
      
    }
  
    virtual void doSomething(){
        cout << "Hello" << endl;
    }
  
    ~A(){
      
    }
};

class B : public A {
  
public:
    B(){
      
    }
  
    void doSomething(){
        cout << "Hi" << endl;
    }

    ~B(){
      
    }
};


void initVec(vector<A*> & myVec){
    myVec.clear();
    myVec.push_back(new A());
    myVec.push_back(new A());
    myVec.push_back(new B());
    myVec.push_back(new A());
    myVec.push_back(new B());
    myVec.push_back(new B());
}


#endif

Explanation / Answer

// SomeHeader.h

#ifndef LA8_SomeHeader_h
#define LA8_SomeHeader_h
#include <iostream>
#include <vector>
using namespace std;
class A {
  
public:
   A(){
      
   }
  
   virtual void doSomething(){
       cout << "Hello" << endl;
   }
  
   ~A(){
      
   }
};
class B : public A {
  
public:
   B(){
      
   }
  
   void doSomething(){
       cout << "Hi" << endl;
   }
   ~B(){
      
   }
};

void initVec(vector<A*> & myVec){
   myVec.clear();
   myVec.push_back(new A());
   myVec.push_back(new A());
   myVec.push_back(new B());
   myVec.push_back(new A());
   myVec.push_back(new B());
   myVec.push_back(new B());
}

#endif

// driver

#include <iostream>
#include "SomeHeader.h"
#include <vector>
using namespace std;

int main(){
   vector<A*> myVec;
   initVec(myVec);
   while (!myVec.empty()){
       cout << myVec.front() << " ";
       myVec.pop_back();
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote