C
cerenoc
I am fairly new to polymorphism with c++ and am having trouble
figuring out an error message. I narrowed it down to the following
simple example:
------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
class Base {
int temp1;
//string temp2;
public:
virtual void whoami() = 0;
};
class Derived1 : public Base {
public:
virtual void whoami() {
cout << "== Derived1 ==" << endl;
}
};
class Derived2 : public Base {
public:
virtual void whoami() {
cout << "== Derived2 ==" << endl;
}
};
int main(int argc, char *argv[]) {
int t = 0;
Base * var;
if (t == 1) {
Derived1 v;
var = &v;
var->whoami();
} else if (t == 0) {
Derived2 v;
var = &v;
var->whoami();
}
var->whoami();
return 0;
}
----------------------------------------------------------------------
This code, as shown, compiles, runs and produces the expected result.
However, if I uncomment the 'string temp2;' line, the code runs to
produce the following error message:
==============
== Derived2 ==
pure virtual method called
terminate called without an active exception
Abort
==============
Could someone please explain this behavior and clue me in on how to
correct it? Thanks a lot.
J
figuring out an error message. I narrowed it down to the following
simple example:
------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
class Base {
int temp1;
//string temp2;
public:
virtual void whoami() = 0;
};
class Derived1 : public Base {
public:
virtual void whoami() {
cout << "== Derived1 ==" << endl;
}
};
class Derived2 : public Base {
public:
virtual void whoami() {
cout << "== Derived2 ==" << endl;
}
};
int main(int argc, char *argv[]) {
int t = 0;
Base * var;
if (t == 1) {
Derived1 v;
var = &v;
var->whoami();
} else if (t == 0) {
Derived2 v;
var = &v;
var->whoami();
}
var->whoami();
return 0;
}
----------------------------------------------------------------------
This code, as shown, compiles, runs and produces the expected result.
However, if I uncomment the 'string temp2;' line, the code runs to
produce the following error message:
==============
== Derived2 ==
pure virtual method called
terminate called without an active exception
Abort
==============
Could someone please explain this behavior and clue me in on how to
correct it? Thanks a lot.
J