분류 전체보기(109)
-
static
c에서의 static 전역변수에 선언된 static 선언된 파일 내에서만 참조를 허용 함수 내에 선언된 static 한번만 초기화 되고, 지역변수와 달리 함수를 빠져나가도 소멸되지 않는다. #include using namespace std; void Counter() { static int cnt; cnt++; cout
2022.04.10 -
friend
private 로 선언된 멤버변수와 멤버함수는 클래스 내에서만 접근가능하다. class Boy { private: int height; friend class Girl; //Girl 클래스를 friend로 선언함 public: Boy(int len) : height(len) {} }; class Girl { private: char phNum[20];//전화번호 public: Girl(char* num) { strcpy(phNum, num); } void ShowYoutFriendInfo(Boy &frn) { cout
2022.04.10 -
연산자 오버로딩
#include using namespace std; class Vector2{ public: Vector2(); Vector2(float x, float y); float GetX() const; float GetY() const; private: float x; float y; }; int main(){ } Vector2::Vector2() :x(0), y(0){ } Vector2::Vector2(float x, float y): x(x), y(y) { } float Vector2::GetX() const {return x;} float Vector2::GetY() const {return y;} 위와 같은 코드가 있을때 아래의 코드는 오류를 발생한다. int main(void) { Vector2 a..
2022.04.10 -
멤버 메서드의 선언, 정의 분리
//멤버 메서드의 선언, 정의 분리하기 #include using namespace std; class Vector2{ public: Vector2(); Vector2(float x, float y); float GetX() const; float GetY() const; private: float x; float y; }; int main(){ } Vector2::Vector2() :x(0), y(0){ } Vector2::Vector2(float x, float y): x(x), y(y) { } float Vector2::GetX() const {return x;} float Vector2::GetY() const {return y;}
2022.04.10 -
예외처리
int fact(int n){ if(n == 0) return 1; return n * fact(n-1); } 위와 같이 팩토리얼을 반환하는 함수가 있다고 하자. 아래와 같이 사용자에게 n값을 입력받는다고 할때 음수 값을 받지 않도록 예외 처리를 해주어야 한다. int main(void) { int n; cin>>n; if (n < 0){ cout
2022.04.10 -
범위지정 연산자
지역 변수의 이름이 전역변수의 이름과 같을 경우, 전역변수는 지역변수에 의해 가려진다는 특징이 있다. int val = 20; int SimpleFunc(void) { int val = 100; val += 20; //지역변수 val의 값 20증가 } 범위지정 연산자를 사용하면 전역변수에 접근 가능하다. int val = 20; int SimpleFunc(void) { int val = 100; val += 20; //지역변수 val의 값 20증가 ::val -=10; // 전역변수 val의 값 10 감소 }
2022.04.09