[ create a new paste ] login | about

Link: http://codepad.org/uuPlC3eO    [ raw code | output | fork ]

C++, pasted on Oct 30:
#include <iostream>
#include <string>
using namespace std;

class ClsSuper{
public:
	virtual void test(){ cout << "super" << endl;};
};

class ClsSub1 : public ClsSuper{
	virtual void test(){ cout << "sub1" << endl;}
};

class ClsSub2 : public ClsSuper{
	virtual void test(){ cout << "sub2" << endl;}
};

//これはmain()と別の翻訳単位にあるかもしれない。
//とりあえず今はそういう状態にあると考えよう。
ClsSuper *GetPointer()
{
	string wk;
	cin >> wk;
	if(wk == "1"){
		return new ClsSub1;
	}
	else{
		return new ClsSub2;
	}
}

//もし違う翻訳単位にGetPointerの定義があれば、
//内部でどんな内容の処理をしているか、main()からはまったくわからない。
//ClsSuper *GetPointer(); という関数の宣言だけがあればいいので
//返されるポインタがClsSub1,ClsSub2,ClsSuperのどのオブジェクトをさしているかは
//main()からはわからない。
int main()
{
	ClsSuper *s = GetPointer();
	s->test();
	delete s;
	return 0;
}


Output:
1
sub2


Create a new paste based on this one


Comments: