// 28_28-1.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <typeinfo>

using namespace std;

// 基底クラス
class ClsA {
protected:
	int a;
public:
	ClsA(int n = 0) { a = n; }
	virtual void disp() { cout << "ClsA a=" << a << '\n'; }
};

// 派生クラス
class ClsB : public ClsA {
private:
	int b;
public:
	ClsB(int n1 = 0, int n2 = 0) : ClsA(n1) { b = n2; }
	void disp() { cout << "ClsB a=" << a << " b=" << b << '\n'; }
};

int _tmain(int argc, _TCHAR* argv[])
{
	int idt = 0, idt2 = 0, *pi;
	double ddt = 0.0;
	ClsA aa(10), aa2, *pa;
	ClsB bb(20, 30);

	cout << "-----(1) クラス内容\n";
	aa.disp();
	bb.disp();

	cout << "-----(2) name()を使う\n";
	cout << "int    :" << typeid(int).name() << '\n';
	cout << "double :" << typeid(double).name() << '\n';
	cout << "idt    :" << typeid(idt).name() << '\n';
	cout << "ddt    :" << typeid(ddt).name() << '\n';
	cout << "aa     :" << typeid(aa).name() << '\n';
	cout << "bb     :" << typeid(bb).name() << '\n';
	
	cout << "-----(3) pi = &idt; を実行\n" << '\n';
	pi = &idt;
	cout << "pi :" << typeid(pi).name() << '\n';
	cout << "*pa:" << typeid(*pi).name() << '\n';
	
	cout << "-----(4) pa = &aa; を実行\n" << '\n';
	pa = &aa;
	cout << "pa :" << typeid(pa).name() << '\n';
	cout << "*pa:" << typeid(*pa).name() << '\n';

	cout << "-----(5) pa = &bb; を実行\n" << '\n';
	pa = &aa;
	cout << "pa :" << typeid(pa).name() << '\n';
	cout << "*pa:" << typeid(*pa).name() << '\n';

	cout << "-----(6) idt vs. int\n" << '\n';
	if (typeid(idt) == typeid(int)) {
		cout << "idtはint型である\n";
	}
	else {
		cout << "idtはint型ではない\n";
	}

	cout << "-----(7) idt vs. idt2\n" << '\n';
	if (typeid(idt) == typeid(idt2)) {
		cout << "idtはidt2と同じ型である\n";
	}
	else {
		cout << "idtはidtと同じ型ではない\n";
	}

	cout << "-----(8) idt vs. double\n" << '\n';
	if (typeid(idt) == typeid(double)) {
		cout << "idtはdouble型である\n";
	}
	else {
		cout << "idtはdouble型ではない\n";
	}

	cout << "-----(9) pi vs. int*\n" << '\n';
	if (typeid(pi) == typeid(int*)) {
		cout << "piはint*型である\n";
	}
	if (typeid(*pi) == typeid(int)) {
		cout << "*piはint*型である\n";
	}

	cout << "-----(10) aa vs. aa2\n" << '\n';
	if (typeid(aa) == typeid(aa2)) {
		cout << "aaはaa2と同じ型である\n";
	}
	if (typeid(aa) == typeid(ClsA)) {
		cout << "*piはClsAと同じ型である\n";
	}

	cout << "-----(11) pa = &aaを実行\n" << '\n';
	pa = &aa;
	if (typeid(*pa) == typeid(ClsA)) {
		cout << "*paはClsAと同じ型である\n";
	}
	else if (typeid(*pa) == typeid(ClsB)) {
		cout << "*paはClsBと同じ型である\n";
	}
	else {
		cout << "*paはClsAともClsBとも異なる型\n";
	}

	cout << "-----(12) 参考 現在のpa->disp()はClsAのdispを実行する\n";
	pa->disp();

	cout << "-----(13) pa = &bb; を実行\n";
	pa = & bb;
	if (typeid(*pa) == typeid(ClsA)) {
		cout << "*paはClsAと同じ型\n";
	}
	else if (typeid(*pa) == typeid(ClsB)) {
		cout << "*paはClsBと同じ型\n";
	}
	else {
		cout << "*paはClsAともClsBとも異なる型\n";
	}

	cout << "-----(14) 参考 現在のpa->disp()はClsBのdispを実行する\n";
	pa->disp();

	MessageBox(NULL, _T("Check Console Window"), L"MessageBox", MB_OK);

	return 0;
}

