C++ Type issue - Slicing isse?
TourManager.h
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <typeinfo>
#include "Tour.h"
#include "GuidedTour.h"
using namespace std;
class TourManager {
private:
vector<Tour *> list;
void setupTour();
void callDisplayOnEach();
public:
TourManager();
void go();
};
TourManager.cpp
#include "TourManager.h"
TourManager::TourManager() {
setupTour();
}
void TourManager::setupTour() {
list.push_back(new Tour("BG002", "Botanical Gardens Entry Pass", 30.00));
list.push_back(new GuidedTour("SK003", "Learn to Ski Adventure Tour",
240.00, "28/07/2008", "Zail S", 25));
}
void TourManager::callDisplayOnEach() {
for(vector<Tour *>::iterator it = list.begin() ; it != list.end();
++it) {
(*(*it)).display();
}
}
Guided tour is a sub class of the Tour. It overrides display method of the
parent tour class. However when I loop through tour objects vector and
call display, it always call the tour::display even if the object is a
GuidedTour.
What am I doing wrong here?
I'm using C++98 Many thanks.
No comments:
Post a Comment