'Virtual Method not calling derived class method (only calling base class) C++
My base class detect() and collect() methods are the only ones being called. I'm trying to get the derived class methods of these to be printed instead. I believe this is because of my vector and how it is storing/printing the miners in minermgr.cpp.
I don't know the correct way to solve this issue.
Miner.cpp
using namespace std;
miner::miner(){}
miner::miner(int miner_id, string miner_type){
this->id = miner_id;
this->type = miner_type;
}
void miner::start(){
// "Miner #ID (TYPE) is starting"
cout << "Miner " << id << " (" << type << ") is starting.\n";
};
void miner::detect() {
cout << "base class detect\n";
};
void miner::collect(){
cout << "base class collect\n";
};
void miner::stop(){
// "Miner #ID (TYPE) is stopping"
cout << "Miner " << id << " (" << type << ") is on standby.\n\n";
};
bool miner::isfull(){
if(currentCapacity > maxCapacity){
return true;
} else { return false; }
};
//additional methods
int miner::getID(){
return id;
};
string miner::getType(){
return type;
};
int miner::getCurrentCapacity(){
return currentCapacity;
}
//shared collect and detect messages
void miner::getSodiumDetectMessage(){
cout << "Steam blasting...\n"
"Salinity detector activated.\n"
"Concentration vector of NaCl detected.\n";
};
void miner::getOxygenDetectMessage(){
cout << "Releasing oxygen...\n"
"Thermal detector activated.\n"
"Flammable reaction detected.\n";
};
// getting collect message for all miners except water
void miner::getNotWaterCollectMessage(){
cout << "Impact hammer positioned.\n"
"Impact hammer activated for 15 seconds.\n"
"Debris scoop activated.\n"
"Debris elevator activated.\n"
"Debris collection complete.\n";
};
miner::~miner(){}
Miner.h
#pragma once
#include <iostream>
#include <string>
#include "helper.h"
class miner
{
private:
int id;
std::string type;
int maxCapacity;
int currentCapacity;
public:
miner();
miner(int miner_id, std::string miner_type);
~miner();
//shared methods
void start();
virtual void detect();
virtual void collect();
void stop();
bool isfull();
//additional methods
int getID();
std::string getType();
int getMaxCapacity();
int getCurrentCapacity();
//shared collect/detect message methods
void getSodiumDetectMessage();
void getOxygenDetectMessage();
void getNotWaterCollectMessage();
};
minermgr.cpp
#include "minermgr.h"
using namespace std;
minermgr::minermgr(){}
void minermgr::addMiner(int miner_id, string miner_type){
if(miner_type == "water"){
miner * waterMiner = new water(miner_id, miner_type);
minerList.push_back(*waterMiner);
} else if(miner_type == "sodium"){
miner * sodiumMiner = new sodium(miner_id, miner_type);
minerList.push_back(*sodiumMiner);
} else if(miner_type == "iron"){
miner * ironMiner = new iron(miner_id, miner_type);
minerList.push_back(*ironMiner);
} else if(miner_type == "silicon"){
miner * siliconMiner = new silicon(miner_id, miner_type);
minerList.push_back(*siliconMiner);
} else if(miner_type == "magnesium"){
miner * magnesiumMiner = new magnesium(miner_id, miner_type);
minerList.push_back(*magnesiumMiner);
} else if(miner_type == "potassium"){
miner * potassiumMiner = new potassium(miner_id, miner_type);
minerList.push_back(*potassiumMiner);
}
}
void minermgr::runMiners(){
for(auto i = minerList.begin(); i != minerList.end(); i++){
i->start();
i->detect();
i->collect();
i->stop();
}
}
minermgr::~minermgr(){}
minermgr.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
// header files
#include "miner.h"
#include "water.h"
#include "sodium.h"
#include "chloride.h"
#include "iron.h"
#include "silicon.h"
#include "magnesium.h"
#include "potassium.h"
class minermgr
{
private:
public:
minermgr();
// minermgr methods ===============
void addMiner(int miner_id, std::string miner_type);
void runMiners();
// ================================
~minermgr();
std::vector<miner>minerList;
};
Water.cpp
#include "water.h"
using namespace std;
water::water(){};
water::water(int water_id, string water_type): miner (water_id,water_type){};
void water::detect(){
didItBreak();
cout << "Initiating heat blast.\n"
"Directional humidity detector activated.\n"
"Ice located.\n";
};
void water::collect() {
didItBreak();
int x = getXFactor();
int amountCollected = (100 * x);
cout << "Water vacuums and hoses deployed.\n"
"Heater turned on. Waiting for the ice to melt.\n"
"Liquid water moved to collection chamber.\n"
"Surface pump activated.\n";
};
water::~water(){}
Water.h
#include <iostream>
#include <string>
// header files
#include "miner.h"
class water: public miner
{
private:
int maxCapacity = 10000;
// int id;
// std::string type;
public:
water();
water(int water_id, std::string water_type);
void detect();
void collect();
~water();
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|