5월26일 C++(템플릿함수,inline,클래스밖 함수구현템플릿,virtual 객체기반)API(비트맵뷰어)
2010. 5. 26. 17:57ㆍ2010년/5월
그래서 다음 보기처럼 num1>num2?num1:num2으로 참이면 앞에꺼 거짓이면 뒤에 것을 리턴한다.
그래서 템플릿함수를 다음과 같이 다른 자료형으로 사용이 가능하다
첫번째는 int형으로 받고
두번째는 float형으로 받고
템플릿이 없다면 함수를 2개를 구현해야한다.
그래서 장점이 있다.
클래스안에 함수를 템플릿을 통해서 밖으로 빼냈을때
스코프연산을 사용한다 하지만 템플리 Z를 사용하기 때문에 그에대한 정의를 내려줘야한다.
기본적으로 밖으로 냈기 때문에 그에 따른
템플릿을 따로 정의해야하고 그리고 스코프 연산앞에다가 <>이름을 적어주게 된다. 여기서 큰특징은 만약 inline인경우 어떻게 될까?
그경우는 그냥 함수앞에서 inline을 사용하게 되면 간단하다 inline은 함수 안의 코드가 작을때 간단하게 사용할수 있다.
virtual는 객체기반함수.
int main()
{
Musician *pm;
pm=new Musician;
pm->greet();
pm->play();
delete pm;
pm=new Trumpeter; //지금은 pm은 트럼펫을 가르키고 있다 그래서
pm->greet();
pm->play(); //지금 pm을 트럼펫을 가르키기때문에 객체기반 함수다 virtual
delete pm;
return 0;
}
class에서 이미 부모는 virtual play()로 되어있다.
================================================================
================================================================
#include <iostream>
using namespace std;
template <class Z>
class Array
{
public:
Array()
{
Elems = 0;
NumElems = 0;
}
~Array()
{
if (0 != NumElems)
{
delete [] Elems;
}
}
void SetSize(size_t value)
{
delete [] Elems;
NumElems = value;
Elems = new Z[value];
if (0 == Elems)
{
NumElems = 0;
}
}
size_t GetSize()
{
return NumElems;
}
void SetElem(size_t index, Z value)
{
if(NumElems <= index)
{
return;
}
Elems[index] = value;
}
Z GetElem(size_t index)
{
if(NumElems <= index)
{
return Z();
}
return Elems[index];
}
private:
Z *Elems;
size_t NumElems;
};
class Musician
{
public:
virtual void greet();
virtual void play();
};
void Musician::greet()
{
cout << "뮤지션 인사 : 안녕~\n";
}
void Musician::play()
{
cout << "뮤지션 연주 : 딴따~\n";
}
class Trumpeter : public Musician
{
public:
void greet()
{
cout << "트럼펫 인사 : 하이~\n";
}
void play()
{
cout << "트럼펫 연주 : 빰파~\n";
}
}; // 세미콜론 빼먹다가 걸리면 주금....
class Flautist : public Musician
{
public:
void greet()
{
cout << "플룻터 인사 : 방가~\n";
}
void play()
{
cout << "플룻터 연주 : 필릴~\n";
}
};
void WarmUp(Musician *pm)
{
cout << "오케스라 단원 입니다...\n";
pm->greet();
cout << "소리 내봐...\n";
pm->play();
}
#define MEMBER 10
int main()
{
Array<Musician *> orchestra;
int iCnt;
orchestra.SetSize(MEMBER);
orchestra.SetElem(0, new Trumpeter);
orchestra.SetElem(1, new Flautist);
orchestra.SetElem(2, new Trumpeter);
orchestra.SetElem(3, new Trumpeter);
orchestra.SetElem(4, new Flautist);
orchestra.SetElem(5, new Trumpeter);
orchestra.SetElem(6, new Flautist);
orchestra.SetElem(7, new Flautist);
orchestra.SetElem(8, new Trumpeter);
orchestra.SetElem(9, new Flautist);
cout << "오케스트라 소개 및 웜업이 있겠습니다.\n";
for(iCnt = 0; MEMBER > iCnt ; ++iCnt)
{
WarmUp(orchestra.GetElem(iCnt));
}
for(iCnt = 0; MEMBER > iCnt ; ++iCnt)
{
delete orchestra.GetElem(iCnt);
}
return 0;
}
===============================================================
===============================================================
API bitmapViewer
'2010년 > 5월' 카테고리의 다른 글
5월28일(비트맵뷰어회전)C++(hexaviewer c++로 변환 (0) | 2010.05.28 |
---|---|
5월27일 C++(순수가상함수,sort,setf(ios_base::boolalpha,showpos,hex,width)API(비트맵뷰어) (0) | 2010.05.27 |
5월25일 C++(복사생성자,대입연산자,종속대입연산자.종속복사생성자,템플릿)API(simplepaint2) (0) | 2010.05.25 |
5월24일 C++(SmartPointer,namespace,연산자오버로딩 API(대화상자 후반부) (0) | 2010.05.24 |
5월20일 C++(예외처리 try,catch,throw) API(멍때림) (0) | 2010.05.20 |