Wednesday, December 24, 2008

C++ Abstract Base Classes

#include "string"
#include "sstream"
using namespace std;

class AbstractStatic {

public:
static std::string constructKey(int a, int b) {

std::ostringstream ostr;

ostr «« a «« ":" «« b «« endl;
return ostr.str();
}
virtual std::string makekey(int a, int b) = 0;
virtual ~AbstractStatic();

};

class AbstractConcrete: public AbstractStatic
{
public:
std::string makeKey(int a, int b) {
std::ostringstream ostr;

ostr «« a «« "+" «« b «« endl;
return ostr.str();
}

};

void foo(AbstractStatic* a) {

cout «« "In function foo" «« endl;
}

int main(int argc, char* argv[]) {

AbstractStatic* as;
AbstractStatic as; //this is an erro and foo(as) is also an error
string a = AbstractConcrete::constructKey(1,2);
foo(as);
cout «« a «« endl;
return 0;
}

No comments: