본문 바로가기

Programing/C++

template 예제

#include <iostream>
#include <fstream>

using namespace std;
/*
template <class CoordType> class MyClass {
 CoordType x, y;
public:
 MyClass(CoordType i, CoordType j) {
 x = i;
 y = j;
 }
 void show() {
  cout << x << ", " << y << endl;
 }
};

int main() {
 MyClass<int> object1(1,2), object2(3, 4);
 object1.show();
 object2.show();
 MyClass<double> object3(0.0, 0.23), object4(10.19, 3.098);
 object3.show();
 object4.show();
 
 return 0;
}
*/
/*
template <class T> class myclass {
 T a;
public:
 myclass(T i) {
  a = i;
 }
};

int main() {
 myclass<int> o1(10), o2(9);
 myclass<double> o3(7.2);
 cout << "type of o1 is ";
 cout << typeid(o1).name() << endl;
 cout << "type of o2 is ";
 cout << typeid(o2).name() << endl;
 cout << "type of o3 is ";
 cout << typeid(o3).name() << endl;
 cout << endl;
 if(typeid(o1) == typeid(o2))
  cout << "o1 and o2 are the same type" << endl;
 else
  cout << "o1 and o3 are the different type" << endl;
 return 0;
}
*/
/*
template <typename Tp>

Tp Add(Tp m, Tp n) {
 return m+n;
}

int main(void) {
 cout << Add(10, 20) << endl;
 cout << Add(5.5, 7.7) << endl;
 return 0;
}
*/
/*
template <typename Tp1, typename Tp2>

void ShowData(Tp1 m, Tp2 n) {
 cout << m << " " << n << endl;
}

int main(void) {
 ShowData(5, 7);
 ShowData(5, 7.7);
 return 0;
}
*/
/*
template <typename Tp>

void ShowData(Tp val) {
 cout << "일반 함수 템플릿 : " << val << endl;
}

template <>

void ShowData(int val) {
 cout << "특수화 함수 템플릿(int  타입) : " << val << endl;
}

int main(void) {
 ShowData(5);
 ShowData(5.5);
 return 0;
}
*/
/*
class KeepData {
 int data;
public:
 KeepData(int i) {
  data = i;
 }
 void PutData(int i) {
  data = i;
 }
 int GetData() {
  return data;
 }
};

int main() {
 KeepData k_d1(0);
 k_d1.PutData(10);
 KeepData k_d2(20);

 cout << k_d1.GetData() << endl;
 cout << k_d2.GetData() << endl;
 return 0;
}
*/
/*
template <typename Tp>

class KeepData {
 Tp data;
public:
 KeepData(Tp i) {
  data = i;
 }
 void PutData(Tp i) {
  data = i;
 }
 Tp GetData() {
  return data;
 }
};

int main() {
 KeepData<int> k_d1(0);
 k_d1.PutData(10);
 KeepData<double> k_d2(12.3);

 cout << k_d1.GetData() << endl;
 cout << k_d2.GetData() << endl;
 return 0;
}
*/
/*
template <typename Tp>

class KeepData {
 Tp data;
public:
 KeepData(Tp i);
 void PutData(Tp i);
 Tp GetData();
};

template <typename Tp>
KeepData<Tp>::KeepData(Tp i) {
 data = i;
}

template <typename Tp>
void KeepData<Tp>::PutData(Tp i) {
 data = i;
}

template <typename Tp>
Tp KeepData<Tp>::GetData() {
 return data;
}


int main() {
 KeepData<int> k_d1(0);
 k_d1.PutData(10);
 KeepData<double> k_d2(12.3);

 cout << k_d1.GetData() << endl;
 cout << k_d2.GetData() << endl;
 return 0;
}
*/
/*
template <typename Tp>
void Swap(Tp& m, Tp& n) {
 Tp t = m;
 m = n;
 n = t;
}

class Data {
 int a, b;
public:
 Data(int _a = 0, int _b = 0) : a(_a), b(_b) {
 
 }

 void ShowData() {
  cout << "출력 : " << a << " " << b << endl;
 }
};

int main () {
 Data dt1(10, 20);
 Data dt2(20, 30);

 Swap(dt1, dt2);
 dt1.ShowData();
 dt2.ShowData();

 int m = 50, n = 70;
 Swap(m, n);
 cout << "출력 : " << m << " " << n << endl;
 return 0 ;
}
*/
/*
template <typename Tp>
int Sizeof(Tp val) {
 return sizeof(val);
}

template <>
int Sizeof(char* val) {
 return strlen(val);
}

int main(void) {
 int i = 5;
 double d = 3.14;
 char* ch="Hi C++ Programing";

 cout << Sizeof(i) << endl;
 cout << Sizeof(d) << endl;
 cout << Sizeof(ch) << endl;
 
 return 0;
}
*/