Presentation is loading. Please wait.

Presentation is loading. Please wait.

Static과 const 선언 조 병 규 한 국 교 통 대 학 교 SQ Lab..

Similar presentations


Presentation on theme: "Static과 const 선언 조 병 규 한 국 교 통 대 학 교 SQ Lab.."— Presentation transcript:

1 static과 const 선언 조 병 규 한 국 교 통 대 학 교 SQ Lab.

2 static 멤버 자료 (static member data)
static 자료형 memberDataName1 [,memberDattName2] . . .; 해당 클래스의 모든 객체에서 멤버 자료(기억 장소)를 공동으로 사용하고자 할 경우 static을 선언한다. 클래스 설정 후 다음과 같은 형식으로 반드시 전역적으로 초기 값을 수록시켜 사용하여야 한다. 자료형 className::staticMemberDataName = value; SQ Lab.

3 (예) static 멤버 자료 /*001*/ // staticMemberData01cNcpp /*002*/
/*003*/ #include "iostream" /*004*/ using namespace std; /*005*/ class classX /*006*/ { /*007*/ private : /*008*/ int Total; /*009*/ static int staticTotal; /*010*/ /*011*/ public : /*012*/ classX(int initialValue=0) /*013*/ { /*014*/ Total = initialValue; /*015*/ } /*016*/ void adder(int x) /*017*/ { /*018*/ Total = Total + x; /*019*/ staticTotal = staticTotal + x; /*020*/ }; SQ Lab.

4 /*021*/ void display(char *object) /*022*/ {
/*022*/ { /*023*/ cout << "\n " << object << " : " /*024*/ << "Total=>" << Total << " " /*025*/ << "staticTotal=>" << staticTotal; /*026*/ } /*027*/ }; /*028*/ /*029*/ int classX::staticTotal = 10; /*030*/ /*031*/ void main() /*032*/ { /*033*/ classX x1, x2; /*034*/ /*035*/ x1.display("x1"); /*036*/ x2.display("x2"); /*037*/ cout << "\n"; /*038*/ /*039*/ x1.adder(1); /*040*/ x1.display("x1"); /*041*/ x2.display("x2"); /*042*/ cout << "\n"; /*043*/ SQ Lab.

5 /*048*/ cout << "\n\n Type any character : "; /*049*/ cin.get();
/*044*/ x2.adder(3); /*045*/ x1.display("x1"); /*046*/ x2.display("x2"); /*047*/ /*048*/ cout << "\n\n Type any character : "; /*049*/ cin.get(); /*050*/ } SQ Lab.

6 /*007*/ private : /*008*/ int Total; /*009*/ static int staticTotal; static이 선언되지 않은 멤버 자료 Total과 static을 선언한 멤버 자료 staticTotal을 설정하였다. /*016*/ void adder(int x) /*017*/ { /*018*/ Total = Total + x; /*019*/ staticTotal = staticTotal + x; /*020*/ }; Total과 staticTotal에 전달된 x값을 누적하는 adder() 멤버 함수를 설정하였다. /*029*/ int classX::staticTotal = 10; classX 클래스의 staticTotal에 10을 수록한다. static 멤버 자료는 반드시 전역으로 초기 값을 설정해 주어야 한다. /*033*/ classX x1, x2; classX형의 객체 x1과 x2를 생성한다. x1의 Total에 0이 수록되고, x2의 Total에 0이 수록되며 staticTotal은 x1과 xt2에서 공동으로 사용하여 값(10)이 같다. /*039*/ x1.adder(1); 실인자를 1로 하여 x1의 adder() 멤버 함수를 수행시킨다. x1의 Total에 1이 누적되고, staticTotal에 1이 누적된다. 따라서 x1의 Total은 1이고, x2의 Total은 0이며, x1과 x2의 staticTotal은 11이 수록되어 있다. SQ Lab.

7 /*044*/ x2.adder(3); 실인자를 3로 하여 x2의 adder() 멤버 함수를 수행시킨다. x2의 Total에 3가 누적되고, staticTotal에 3이 누적된다. 따라서 x1의 Total은 1이고, x2의 Total은 3이며, x1과 x2의 staticTotal은 14가 수록되어 있다. SQ Lab.

8 [결과] SQ Lab.

9 static 멤버 함수 (static member function)
static 함수형 memberFunctionName([parameterList]); static으로 선언한 멤버 함수는 해당 클래스의 static 멤버 자료만 사용할 수 있다. SQ Lab.

10 (예) static 멤버 함수 /*001*/ // staticMemberFunction01cNcpp /*002*/
/*003*/ #include "iostream" /*004*/ using namespace std; /*005*/ class classX /*006*/ { /*007*/ private : /*008*/ int Total; /*009*/ static int staticTotal; /*010*/ /*011*/ public : /*012*/ classX(int initialValue=0) /*013*/ { /*014*/ Total = initialValue; /*015*/ } /*016*/ static void adder(int x) /*017*/ { /*018*/ staticTotal = staticTotal + x; /*019*/ // Total = Total + x; //error : illegal reference /*020*/ }; SQ Lab.

11 /*021*/ void display(char *object) /*022*/ {
/*022*/ { /*023*/ cout << "\n " << object << " : " /*024*/ << "Total=>" << Total << " " /*025*/ << "staticTotal=>" << staticTotal; /*026*/ } /*027*/ }; SQ Lab.

12 /*007*/ private : /*008*/ int Total; /*009*/ static I nt staticTotal; static이 선언되지 않은 멤버 자료 Total과 static을 선언한 멤버 자료 staticTotal을 설정하였다. /*016*/ static void adder(int x) /*017*/ { /*018*/ staticTotal = staticTotal + x; /*019*/ // Total = Total + x; //error : illegal reference /*020*/ }; adder() 멤버 함수를 static 함수로 선언하였다. 이 함수에서 사용할 수 있는 멤버 자료는 반드시 static 으로 선언된 것이다. Total은 static 멤버 자료가 아니므로 이 static 함수에서는 사용할 수 없다. SQ Lab.

13 const 멤버 함수 함수형 memberFunctionName([parameterList]) const {
멤버 자료의 값을 참조만 할 수 있고, 새로운 값을 수록할 수는 없음 } const로 선언된 함수에서는 멤버 자료의 값만 참조할 수 있지 새로운 값을 수록하지는 못한다. - 멤버 자료의 값을 보호하여야 할 필요가 있을 경우 선언한다. - const 단어는 반드시 블록({}) 바로 앞에 기술한다. SQ Lab.

14 (예) const 멤버 함수 /*001*/ // constMemberFunction01cNcpp /*002*/
/*003*/ #include "iostream" /*004*/ using namespace std; /*005*/ class classX /*006*/ { /*007*/ private : /*008*/ int x, y, total; /*009*/ /*010*/ public : /*011*/ classX(int initialValue=0) /*012*/ { /*013*/ x = 10; /*014*/ y = 30; /*015*/ total = initialValue; /*016*/ } /*017*/ void display(char *object) const /*018*/ { /*019*/ cout << "\n " << object << "::x=====>" << x /*020*/ << "\n " << object << "::y=====>" << y /*021*/ << "\n " << object << "::total=>" << total; /*022*/ } SQ Lab.

15 /*029*/ // void constAdder(int x) const /*030*/ // {
/*023*/ void Adder(int p) /*024*/ { /*025*/ x = x + 1; /*026*/ y = y + 2; /*027*/ total = total + p; /*028*/ } /*029*/ // void constAdder(int x) const /*030*/ // { /*031*/ // x = x + 3; /*032*/ // y = y + 4; /*033*/ // total = total + x; /*034*/ // } /*035*/ }; SQ Lab.

16 /*007*/ private : /*008*/ int x, y, total; 정수형 멤버 자료 x, y, total을 설정하였다. /*017*/ void display(char *object) const /*018*/ { /*019*/ cout << "\n " << object << "::x=====>" << x /*020*/ << "\n " << object << "::y=====>" << y /*021*/ << "\n " << object << "::total=>" << total; /*022*/ } display()를 const 멤버 함수로 선언하였다. 이 함수에서는 모든 멤버 자료(x, y, total)를 cout 문장에서 참조만 하였으므로 정상적이다. /*029*/ // void constAdder(int x) const /*030*/ // { /*031*/ // x = x + 3; /*032*/ // y = y + 4; /*033*/ // total = total + x; /*034*/ // } constAdder를 const 멤버 함수로 선언하였다. 이 함수에서는 모든 멤버 자료(x, y, total)를 참조도 하고 새로운 값을 수록도 하였다. 따라서 새로운 값을 수록하는 부분은 모두 잘 못(error)이다. SQ Lab.

17 mutable 멤버 자료 mutable 자료형 memberDataName;
const 멤버 함수에서도 새로운 값을 수록할 수 있다. - mutable은 memberDataName 앞 어느 위치에 기술해도 된다. SQ Lab.

18 (예) mutable 멤버 자료 /*001*/ // mutableMemberData01cNcpp /*002*/
/*003*/ #include "iostream" /*004*/ using namespace std; /*005*/ class classX /*006*/ { /*007*/ private : /*008*/ mutable int x; /*009*/ mutable int y; /*010*/ int mutable total; /*011*/ /*012*/ public : /*013*/ classX(int initialValue=0) /*014*/ { /*015*/ x = 10; /*016*/ y = 30; /*017*/ total = initialValue; /*018*/ } SQ Lab.

19 /*019*/ void display(char *object) const /*020*/ {
/*020*/ { /*021*/ cout << "\n " << object << "::x=====>" << x /*022*/ << "\n " << object << "::y=====>" << y /*023*/ << "\n " << object << "::total=>" << total; /*024*/ } /*025*/ void Adder(int p) /*026*/ { /*027*/ x = x + 1; /*028*/ y = y + 2; /*029*/ total = total + p; /*030*/ } /*031*/ void constAdder(int p) const /*032*/ { /*033*/ x = x + 3; /*034*/ y = y + 4; /*035*/ total = total + p; /*036*/ } /*037*/ }; SQ Lab.

20 /*052*/ cout << "\n\n Type any character : "; /*053*/ cin.get();
/*038*/ void main() /*039*/ { /*040*/ classX x1; /*041*/ /*042*/ x1.display("x1"); /*043*/ cout << "\n"; /*044*/ /*045*/ x1.Adder(3); /*046*/ x1.display("x1"); /*047*/ cout << "\n"; /*048*/ /*049*/ x1.constAdder(5); /*050*/ x1.display("x1"); /*051*/ /*052*/ cout << "\n\n Type any character : "; /*053*/ cin.get(); /*054*/ } SQ Lab.

21 /*007*/ private : /*008*/ mutable int x; /*009*/ mutable int y; /*010*/ int mutable t otal; 정수형 멤버 자료 x, y, total을 모두 mutable 선언을 하였으므로 const 멤버 함수에서 참조뿐만 아니라 새로운 값을 수록할 수도 있다. /*031*/ void constAdder(int p) const /*032*/ { /*033*/ x = x + 3; /*034*/ y = y + 4; /*035*/ total = total + p; /*036*/ } constAdder를 const 멤버 함수로 선언하였다. 이 함수에서는 모든 멤버 자료(x, y, total)에 원칙적으로 새로운 값을 수록시킬 수 없으나, 각 멤버 자료를 mutable로 선언하였으므로 새로운 값을 수록시킬 수 있다. SQ Lab.

22 const 객체 const className objectName(parameterList);
- const는 objectName 앞 어느 위치에든 기술할 수 있다. SQ Lab.

23 (예) const 객체 /*001*/ // constObject01cNcpp /*002*/
/*003*/ #include "iostream" /*004*/ using namespace std; /*005*/ class classX /*006*/ { /*007*/ private : /*008*/ mutable int x; /*009*/ mutable int y; /*010*/ mutable int total; /*011*/ /*012*/ public : /*013*/ classX(int initialValue=0) /*014*/ { /*015*/ x = 10; /*016*/ y = 30; /*017*/ total = initialValue; /*018*/ }; /*019*/ SQ Lab.

24 /*020*/ void display(char *x1) const /*021*/ {
/*021*/ { /*022*/ cout << "\n " << x1 << "::x=====>" << x /*023*/ << "\n " << x1 << "::y=====>" << y /*024*/ << "\n " << x1 << "::total=>" << total; /*025*/ } /*026*/ /*027*/ void constAdder(int p) const /*028*/ { /*029*/ x = x + 1; /*030*/ y = y + 3; /*031*/ total = total + p; /*032*/ }; /*033*/ /*034*/ void Adder(int p) /*035*/ { /*036*/ x = x + 2; /*037*/ y = y + 4; /*038*/ total = total + p; /*039*/ }; /*040*/ }; SQ Lab.

25 /*052*/ // x1.Adder(3); //error /*053*/
/*041*/ void main() /*042*/ { /*043*/ const classX x1; /*044*/ /*045*/ x1.display("x1"); /*046*/ cout << "\n"; /*047*/ /*048*/ x1.constAdder(3); /*049*/ x1.display("x1"); /*050*/ cout << "\n"; /*051*/ /*052*/ // x1.Adder(3); //error /*053*/ /*054*/ cout << "\n\n Type any character : "; /*055*/ cin.get(); /*056*/ } SQ Lab.

26 /*007*/ private : /*008*/ mutable int x; /*009*/ mutable int y; /*010*/ int mutable total; 정수형 멤버 자료 x, y, total을 모두 mutable 선언을 하였으므로 const 멤버 함수에서 참조뿐만 아니라 새로운 값을 수록할 수도 있다. /*020*/ void display(char *x1) const /*021*/ { /*022*/ cout << "\n " << x1 << "::x=====>" << x /*023*/ << "\n " << x1 << "::y=====>" << y /*024*/ << "\n " << x1 << "::total=>" << total; /*025*/ } display()를 const 멤버 함수로 설정하였다. /*027*/ void constAdder(int p) const /*028*/ { /*029*/ x = x + 1; /*030*/ y = y + 3; /*031*/ total = total + p; /*032*/ }; constAdder()를 const 멤버 함수로 설정하였다. SQ Lab.

27 /*034*/ void Adder(int p) /*035*/ { /*036*/ x = x + 2; /*037*/ y = y + 4; /*038*/ total = total + p; /*039*/ }; Adder()는 const를 선언하지 않은 멤버 함수로 설정하였다. /*043*/ const classX x1; classX형태의 객체 x1을 const 선언하여 생성하였다. x1의 const 멤버 함수인 display()와 constAdder()만을 호출(사용)할 수 있다. /*045*/ x1.display("x1"); /*048*/ x1.constAdder(3); /*049*/ x1.display("x1"); /*052*/ // x1.Adder(3); //error Adder()는 const 멤버 함수가 아니기 때문에 호출(사용)할 수 없다. SQ Lab.


Download ppt "Static과 const 선언 조 병 규 한 국 교 통 대 학 교 SQ Lab.."

Similar presentations


Ads by Google