I'm reasonably new to C++ so I'd like to apologize if the level of this question is a little below the usual standards here -
I'm trying to get several classes to inherit from a base class which has a virtual function definition, then I'd like to create an array of MainClass* which can include all of the derived classes in order to output the derived + defined virtual function.
I receive the Error "R6025: pure virtual function call" - I don't know why, I was assuming that - when called - the updated definition of that function from the child classes was used.
Here's my code:
Base:
class mitarbeiter
{
public:
mitarbeiter(string name, int nummer);
~mitarbeiter(void);
virtual void einkommen() = 0;
protected:
string name;
int nummer;
};
Derived1:
#include "mitarbeiter.h"
class lohnempfaenger : public mitarbeiter
{
public:
lohnempfaenger(int stundenlohn, int anzahlStunden, string name, int nummer);
~lohnempfaenger(void);
void einkommen();
private:
int stundenlohn;
int anzahlStunden;
};
Derived 2:
#include "mitarbeiter.h"
#include <string>
class angestellter : public mitarbeiter
{
public:
angestellter(int gehalt, string name, int nummer);
~angestellter(void);
void einkommen();
private:
int gehalt;
};
Implementation of "einkommen()" in Derived 1:
void lohnempfaenger::einkommen()
{
cout << "Lohnempfaenger, Name: " << this->name << ", Gesamtlohn: " << this->stundenlohn*this->anzahlStunden << "\n";
}
Implementation of "einkommen()" in Derived 2:
void angestellter::einkommen()
{
cout << "Angestellter, Name: " << this->name << ", Einkommen: " << this->gehalt << "\n";
}
Array-Declaration in main method:
mitarbeiter* mitPtr[5];
mitPtr[0] = &angestellter(/*values...*/);
//error
mitPtr[0]->einkommen();
Constructors:
mitarbeiter::mitarbeiter(string name, int nummer)
{
this->name = name;
this->nummer = nummer;
}
angestellter::angestellter(int gehalt, string name, int nummer):mitarbeiter(name, nummer)
{
this->gehalt = gehalt;
}
lohnempfaenger::lohnempfaenger(int stundenlohn, int anzahlStunden, string name, int nummer):mitarbeiter(name, nummer)
{
this->stundenlohn = stundenlohn;
this->anzahlStunden = anzahlStunden;
}
Thanks a lot!