//card class header
class Card{
public:
string toString();
};
//card.cpp
string Card::toString(){
return string("Not defined in sub class");
}
//turn class header
class Turn: public Card{
public:
string toString();
};
//turn.cpp
string Turn::toString(){
string str("You turn to ");
std::stringstream ss;
ss << turnto;
str.append(ss.str());
return str;
}
Did you try it?
That is what virtual is for, it specifies that a sub class may overwrite it else it will use the default.
A pure virtual function specifies a subclass must overwrite it and it is declared like this:
1 2 3 4 5 6
//card class header
class Card{
public:
virtual string toString() = 0;
};
Notice the = 0; // <-- pure virtual
Regardless of what your class diagram tells you, this is how it is done.
And I would speculate that this is the reason you haven't been able to do it.
Else you wouldn't be here asking.
1 2 3 4 5
//turn class header
class Turn: public Card{
public:
string toString();
};
the turn class will also need a default constructor at least;
Thanks CodeGoggles. I understand it now. Using virtual, a sub class may overwrite it else it will use the default and when using pure virtual a subclass must overwrite it. I have only included some parts of the code.