Presentation is loading. Please wait.

Presentation is loading. Please wait.

정적 멤버 변수/정적 멤버 함수 - friend 함수/클래스 template

Similar presentations


Presentation on theme: "정적 멤버 변수/정적 멤버 함수 - friend 함수/클래스 template"— Presentation transcript:

1 정적 멤버 변수/정적 멤버 함수 - friend 함수/클래스 template
기타 정적 멤버 변수/정적 멤버 함수 - friend 함수/클래스 template

2 정적 멤버 변수 class Name{ public: static int counter; … };
클래스의 모든 인스턴스(객체) 사이에서 공유되는 변수 객체에 속하는 것이 아니라 클래스에 속하는 것으로 이해 전역 변수와 멤버 변수의 절충형 주로 객체의 수를 파악하기 위해 사용 class Name{ public: static int counter; }; int Name::counter = 0; int main( ) { }

3 #include <iostream> using namespace std; class Cat { public:
Cat(int age):itsAge(age){HowManyCats++; } virtual ~Cat() { HowManyCats--; } virtual int GetAge() { return itsAge; } virtual void SetAge(int age) { itsAge = age; } static int HowManyCats; private: int itsAge; }; list 15.1

4 int Cat::HowManyCats = 0;
int main() { const int MaxCats = 5; int i; Cat *CatHouse[MaxCats]; for (i = 0; i<MaxCats; i++) CatHouse[i] = new Cat(i); cout << "There are "; cout << Cat::HowManyCats; cout << " cats left!\n"; cout << "Deleting the one which is "; cout << CatHouse[i]->GetAge(); cout << " years old\n"; delete CatHouse[i]; CatHouse[i] = 0; } return 0;

5 정적 멤버 함수 정적 멤버 변수의 접근 정적 멤버 함수 public 일 때 : list 15.1에서와 같이 직접 접근
private 일 때 public 멤버 함수를 이용하여 정적 멤버 변수에 접근 객체가 반드시 존재해야만 접근 가능 정적 멤버 함수 객체내에서 존재하는 것이 아니라 클래스에 존재 정적 멤버 함수는 this라는 포인터를 갖지 않는다 정적 멤버 함수는 정적 멤버 변수에 접근 가능 정적 멤버 함수는 일반 멤버 변수에 접근 불능 const로 선언 될 수 없다

6 #include <iostream> using std::cout; class Cat { public:
Cat(int age):itsAge(age){HowManyCats++; } virtual ~Cat() { HowManyCats--; } virtual int GetAge() { return itsAge; } virtual void SetAge(int age) { itsAge = age; } virtual int GetHowMany() { return HowManyCats; } private: int itsAge; static int HowManyCats; }; list 15.3

7 int Cat::HowManyCats = 0;
int main() { const int MaxCats = 5; int i; Cat *CatHouse[MaxCats]; for (i = 0; i<MaxCats; i++) CatHouse[i] = new Cat(i); cout << "There are "; cout << CatHouse[i]->GetHowMany(); cout << " cats left!\n"; cout << "Deleting the one which is "; cout << CatHouse[i]->GetAge()+2; cout << " years old\n"; delete CatHouse[i]; CatHouse[i] = 0; } return 0;

8 #include <iostream> class Cat { public:
Cat(int age):itsAge(age){HowManyCats++; } virtual ~Cat() { HowManyCats--; } virtual int GetAge() { return itsAge; } virtual void SetAge(int age) { itsAge = age; } static int GetHowMany() { return HowManyCats; } private: int itsAge; static int HowManyCats; }; list 15.4

9 int Cat::HowManyCats = 0;
int main() { const int MaxCats = 5; Cat *CatHouse[MaxCats]; int i; for (i = 0; i<MaxCats; i++) CatHouse[i] = new Cat(i); std::cout<<"There are "<< Cat::GetHowMany() <<" cats alive!\n"; } for ( i = 0; i<MaxCats; i++) delete CatHouse[i]; return 0;

10 프렌드 함수 객체에 대한 접근 통제 프렌드라는 또 다른 형식의 접근을 제공 class ABC{
공용 멤버 함수(변수)가 유일한 접근 방법 프렌드라는 또 다른 형식의 접근을 제공 프렌드 함수 프렌드 클래스 프렌드 함수는 멤버 함수가 가진 것과 같은 접근 권한을 가짐 연산자 중복에 주로 이용 =, (), [], -> 연산자는 중복 할 수 없음 프렌드 함수는 this 포인터를 가지지 않음 class ABC{ friend void func( void ); }; class ABC{ friend class DEF; };

11 #include <iostream> using namespace std; class Complex;
Complex Add( Complex c1, Complex c2 ); Complex Sub( Complex c1, Complex c2 ); class Complex{ public: Complex(): real(0), imag(0) { } Complex( double a, double b ): real(a), imag(b) { } friend Complex Add( Complex c1, Complex c2 ); friend Complex Sub( Complex c1, Complex c2 ); void Show(void); private: double real, imag; }; list friend.1

12 Complex Add( Complex c1, Complex c2 )
{ Complex temp; temp.real = c1.real + c2.real; temp.imag = c1.imag + c2.imag; return temp; } Complex Sub( Complex c1, Complex c2 ) temp.real = c1.real - c2.real; temp.imag = c1.imag - c2.imag; void Complex::Show() cout << real; if(imag>=0) cout << "+i" << imag << "\n"; else cout << "-i" << -imag << "\n";

13 int main() { Complex a(8.7, 2.4), b(2.6, 3.2), sum, sub; sum = Add(a,b); sub = Sub(a,b); cout << "Result of Summation: "; sum.Show(); cout << "Result of Subtraction: "; sub.Show(); return 0; }

14 #include <iostream> using namespace std; class Complex{ public:
Complex(): real(0), imag(0) { } Complex( double a, double b ): real(a), imag(b) { } Complex operator+( Complex c2 ); Complex operator-( Complex c2 ); void Show(void); private: double real, imag; }; list friend.2

15 Complex Complex::operator+( Complex c2 )
{ Complex temp; temp.real = real + c2.real; temp.imag = imag + c2.imag; return temp; } Complex Complex::operator-( Complex c2 ) temp.real = real - c2.real; temp.imag = imag - c2.imag; void Complex::Show() cout << real; if(imag>=0) cout << "+i" << imag << "\n"; else cout << "-i" << -imag << "\n";

16 int main() { Complex a(8.7, 2.4), b(2.6, 3.2), sum, sub; sum = a + b; sub = a - b; cout << "Result of Summation: "; sum.Show(); cout << "Result of Subtraction: "; sub.Show(); return 0; }

17 #include <iostream> using namespace std; class Complex;
Complex operator+( Complex c1, Complex c2 ); Complex operator-( Complex c1, Complex c2 ); class Complex{ public: Complex(): real(0), imag(0) { } Complex( double a, double b ): real(a), imag(b) { } friend Complex operator+( Complex c1, Complex c2 ); friend Complex operator-( Complex c1, Complex c2 ); void operator() (); private: double real, imag; }; list friend.3

18 Complex operator+( Complex c1, Complex c2 )
{ Complex temp; temp.real = c1.real + c2.real; temp.imag = c1.imag + c2.imag; return temp; } Complex operator-( Complex c1, Complex c2 ) temp.real = c1.real - c2.real; temp.imag = c1.imag - c2.imag; void Complex::operator() () cout << real; if(imag>=0) cout << "+i" << imag << "\n"; else cout << "-i" << -imag << "\n";

19 int main() { Complex a(8.7, 2.4), b(2.6, 3.2), sum, sub; sum = a + b; sub = a - b; cout << "Result of Summation: "; sum(); cout << "Result of Subtraction: "; sub(); return 0; }

20 #include <iostream> using namespace std; class Complex;
Complex operator+( Complex c1, Complex c2 ); Complex operator-( Complex c1, Complex c2 ); class Complex{ public: Complex(): real(0), imag(0) { } Complex( double a, double b ): real(a), imag(b) { } friend Complex operator+( Complex c1, Complex c2 ); friend Complex operator-( Complex c1, Complex c2 ); void operator() (); private: double real, imag; }; list friend.3

21 Complex operator+( Complex c1, Complex c2 )
{ Complex temp; temp.real = c1.real + c2.real; temp.imag = c1.imag + c2.imag; return temp; } Complex operator-( Complex c1, Complex c2 ) temp.real = c1.real - c2.real; temp.imag = c1.imag - c2.imag; void Complex::operator() () cout << real; if(imag>=0) cout << "+i" << imag << "\n"; else cout << "-i" << -imag << "\n";

22 int main() { Complex a(8.7, 2.4), b(2.6, 3.2), sum, sub; sum = a + b; sub = a - b; cout << "Result of Summation: "; sum(); cout << "Result of Subtraction: "; sub(); return 0; }

23 템플릿 (template) 템플릿 함수나 클래스를 생성하는데 사용될 수 있는 함수 또는 클래스의 원형(틀)
컴파일 동안에 실제 함수와 클래스를 생성하기 위해 템플릿 사용 템플릿을 통해 하나 또는 그 이상의 실제적인 함수나 클래스 제작 가능

24 함수 템플릿 함수 템플릿 int max (int x, int y) { return ( x>y ) ? x : y ; }
일반적인 함수 작성 int max (int x, int y) { return ( x>y ) ? x : y ; } float max (float x, float y) { return ( x>y ) ? x : y ; } long max (long x, long y) { return ( x>y ) ? x : y ; } double max (double x, double y) { return ( x>y ) ? x : y ; } 함수가 생성될 때 교체될 포괄적인 형 template <class TYPE> TYPE max (TYPE x, TYPE y) { return ( x>y ) ? x : y ; }

25 #include <iostream> using namespace std;
template <class TYPE> TYPE max (TYPE x, TYPE y) { return (x>y) ? x : y; } int main() int i1, i2; cout << "\nFirst Number:" ; cin >> i1; cout << "\nSecond Number:" ; cin >> i2; cout << "\nMax Number :" << max(i1, i2) << endl; float f1, f2; cin >> f1; cin >> f2; cout << "\nMax Number :" << max(f1, f2) << endl; return 0; list template.1

26 #include <iostream> using namespace std;
template <class T> T smallest (T arr[], int size) { T smallestValue = arr[0]; for( int index=0 ; index<size ; index++ ) if( arr[index] < smallestValue) smallestValue = arr[index]; } return smallestValue; int main() int arr1[4] = {8, 2, 45, 67}; cout << "Smallest integer value is: "; cout << smallest(arr1, 4) << endl; double arr2[5] = {3.5, 5.6, 1.22, 78.4, 6.0}; cout << "Smallest double value is: "; cout << smallest(arr2, 5) << endl; return 0; list template.2

27 클래스 템플릿 클래스 템플릿 template <class TYPE> class Array { public: …
다른 형의 속성들을 가지는 여러 개의 클래스를 필요로 하는 경우 발생 예) 일반화된 배열 클래스 template <class TYPE> class Array { public: protected: TYPE * array; int capacity; int next UnusedIndex; }; int main( ) { Array <int> intarray; }

28 #include <iostream> using namespace std;
template <class TYPE> class Array { protected: TYPE *ary; int capacity; int nextUnusedIndex; public: Array(int size); int getCapacity() const; bool isFull() const; void append (TYPE data); int find (TYPE data) const; TYPE retrieve (int index) const; void change (int index, TYPE data); }; list template.3

29 template <class TYPE>
Array<TYPE>::Array(int size) { capacity = size; nextUnusedIndex = 0; ary = new TYPE[capacity]; } int Array<TYPE>::getCapacity() const return capacity; bool Array<TYPE>::isFull() const return (nextUnusedIndex==capacity);

30 template <class TYPE>
void Array<TYPE>::append(TYPE data) { if(capacity > nextUnusedIndex) { ary[nextUnusedIndex] = data; nextUnusedIndex++; } else{ cout << "Error 100 : Array Overflow"; exit(100); int Array<TYPE>::find(TYPE data) const int index = 0; bool found = false; while(!found && index<nextUnusedIndex){ if( ary[index]==data ) found = true; else index++; if(found) return index; else return -1;

31 template <class TYPE>
TYPE Array<TYPE>::retrieve(int index) const { if( index<0 || index>=nextUnusedIndex ) cout << "Error 101: Index out of range"; exit(101); } return ary[index]; void Array<TYPE>::change( int index, TYPE data) if(index<0 || index>=nextUnusedIndex) cout << "Error 102: index out of range"; exit(102); ary[index] =data

32 int main() { Array<int> intary(30); Array<float> floatary(20); for( int i=0 ; i<20 ; i++ ) intary.append(i*100); floatary.append(i*3.14); } return 0;


Download ppt "정적 멤버 변수/정적 멤버 함수 - friend 함수/클래스 template"

Similar presentations


Ads by Google