[kossa] c++ programming - 14th study - template
TRANSCRIPT
![Page 1: [KOSSA] C++ Programming - 14th Study - template](https://reader034.vdocuments.net/reader034/viewer/2022042716/55b6e4a8bb61eb5a268b48ea/html5/thumbnails/1.jpg)
![Page 2: [KOSSA] C++ Programming - 14th Study - template](https://reader034.vdocuments.net/reader034/viewer/2022042716/55b6e4a8bb61eb5a268b48ea/html5/thumbnails/2.jpg)
![Page 4: [KOSSA] C++ Programming - 14th Study - template](https://reader034.vdocuments.net/reader034/viewer/2022042716/55b6e4a8bb61eb5a268b48ea/html5/thumbnails/4.jpg)
4
http://ko.wikipedia.org/wiki/표준_템플릿_라이브러리
![Page 5: [KOSSA] C++ Programming - 14th Study - template](https://reader034.vdocuments.net/reader034/viewer/2022042716/55b6e4a8bb61eb5a268b48ea/html5/thumbnails/5.jpg)
![Page 6: [KOSSA] C++ Programming - 14th Study - template](https://reader034.vdocuments.net/reader034/viewer/2022042716/55b6e4a8bb61eb5a268b48ea/html5/thumbnails/6.jpg)
6
#include <iostream>
int Max(int a, int b){
return a > b ? a : b;}
void main(){
int nA = 300;int nB = 400;int nC = Max(nA, nB);std::cout << "Max (300 , 400) = " << nC << std::endl;
float fD = 15.1f;float fE = 15.3f;float fF = Max(fD, fE);std::cout << "Max (15.1 , 15.3) = " << fF << std::endl;
}
![Page 7: [KOSSA] C++ Programming - 14th Study - template](https://reader034.vdocuments.net/reader034/viewer/2022042716/55b6e4a8bb61eb5a268b48ea/html5/thumbnails/7.jpg)
7
float Max(float a, float b){
return a > b ? a : b;}
![Page 8: [KOSSA] C++ Programming - 14th Study - template](https://reader034.vdocuments.net/reader034/viewer/2022042716/55b6e4a8bb61eb5a268b48ea/html5/thumbnails/8.jpg)
8
![Page 9: [KOSSA] C++ Programming - 14th Study - template](https://reader034.vdocuments.net/reader034/viewer/2022042716/55b6e4a8bb61eb5a268b48ea/html5/thumbnails/9.jpg)
9
template <typename T>T Max(T a, T b){
return a > b ? a : b;}
int 를 넣으면
모든 T 가 다 int
int Max(int a, int b){
return a > b ? a : b;}
![Page 10: [KOSSA] C++ Programming - 14th Study - template](https://reader034.vdocuments.net/reader034/viewer/2022042716/55b6e4a8bb61eb5a268b48ea/html5/thumbnails/10.jpg)
10
template <typename T>T Max(T a, T b){
return a > b ? a : b;}
void main(){
int nA = 300;int nB = 400;int nC = Max(nA, nB);std::cout << "Max (300 , 400) = " << nC << std::endl;
float fD = 15.1f;float fE = 15.3f;float fF = Max(fD, fE);std::cout << "Max (15.1 , 15.3) = " << fF << std::endl;
}
![Page 11: [KOSSA] C++ Programming - 14th Study - template](https://reader034.vdocuments.net/reader034/viewer/2022042716/55b6e4a8bb61eb5a268b48ea/html5/thumbnails/11.jpg)
11
template <typename T1, typename T2 >T1 Max(T1 a, T2 b){
return a > b ? a : b;}
![Page 12: [KOSSA] C++ Programming - 14th Study - template](https://reader034.vdocuments.net/reader034/viewer/2022042716/55b6e4a8bb61eb5a268b48ea/html5/thumbnails/12.jpg)
12
template <typename T, int VAL>T AddValue(T& value){
return value + VAL;}
여기에 500을 넣으면
![Page 13: [KOSSA] C++ Programming - 14th Study - template](https://reader034.vdocuments.net/reader034/viewer/2022042716/55b6e4a8bb61eb5a268b48ea/html5/thumbnails/13.jpg)
13
typename T
template <typename T>T Max(T a, T b){
return a > b ? a : b;}
float Max(float a, float b){
return a > b ? a : b;}
int Max(int a, int b){
return a > b ? a : b;}
![Page 14: [KOSSA] C++ Programming - 14th Study - template](https://reader034.vdocuments.net/reader034/viewer/2022042716/55b6e4a8bb61eb5a268b48ea/html5/thumbnails/14.jpg)
![Page 15: [KOSSA] C++ Programming - 14th Study - template](https://reader034.vdocuments.net/reader034/viewer/2022042716/55b6e4a8bb61eb5a268b48ea/html5/thumbnails/15.jpg)
15
class Data{
int data;public:
Data(int d) { data = d; }
void SetData(int d) { data = d; }
int GetData() { return data; }};
![Page 16: [KOSSA] C++ Programming - 14th Study - template](https://reader034.vdocuments.net/reader034/viewer/2022042716/55b6e4a8bb61eb5a268b48ea/html5/thumbnails/16.jpg)
16
template <typename T>class Data{
T data;public:
Data(T d) { data = d; }
void SetData(T d) { data = d; }
T GetData() { return data; }};