새소식

🏫 School/[23-2] 객체지향프로그래밍

[C++] Variables, Constants etc...

  • -

Identifier (variabes, constant etc... 이름 붙이기)

- C++언어에서 변수나 상수의 이름을 지을 때는 아래의 규칙들을 따라야 함

규칙1) 최소 한개의 character 요소 포함
규칙2) 첫번째 character는 alphabetic letter (대/소문자 무관) 또는 underscore(_)
규칙3) 나머지 character는 alphabetic letter (대/소문자 무관), underscore(_) 또는 숫자
규칙4) 다른 character는 허용되지 않으며, reserved words는 이름이 될 수 없음

reserved word 리스트

Variables

[Integer]

- 기본적으로 정수형 상수 변수를 의미
- 선언할 때, 앞에 붙이는 수식어에 따라 사용할 수 있는 숫자의 범위가 달라짐 (b/c 해당변수가 사용하는 저장용량이 달라짐)

32 bit computer에서 integer 표현에 따른 저장용량 및 숫자 차이

/* integer 사용 예시 코드 */
#include <iostream>

int main () {
	int x;
    x = 10;
}

[Floating Point]

- 유리수 (소수)까지 표현 할 수 있는 비정수형 변수를 의미 (= non-integer type)
- 해당 변수형 역시 표현에 따라 나타낼 수 있는 범위가 달라짐 (나타낼수 있는 소숫점 아래 숫자 개수 역시 변화)

32 bit computer에서 floating point 표현에 따른 저장용량 및 숫자 차이

/* floating point 사용 예시 코드 */
#include <iostream>

int main (){
	double pi = 3.141592;
}

[Character]

- 'char' data type은 하나의 character를 나타내기 위해 사용되는 변수형
- 대소문자 알파벳, digit , punctuation, control characters가  character로 나타내짐
- 대부분의 character 기호들은 ASCII code로 나타낼 수 있음

ASCII Code &nbsp;Number - ASCII Code
Extra ASCII Code

#include <iostream>

int main () { 
	char ch1, ch2;
    
    /*ASCII code Number 이용*/
    ch1 = 65;
    
    /*ASCII code 이용*/
    ch2 = 'A';
}

[Named Constant]

- 위의 변수형들과 다르게 바꿀 수 없는 상수를 의미
- 처음에 저장했던 상수를 바꾸려고 하면 compile error가 남

#include <iostream>

int main () {
	const double pi = 3.14159;
    double temp = 0;
    
    temp = pi;
    /*temp = 3.14159*/
}

[Output Stream]

cout << "Please enter two integer values : ";

- 사용자가 정보를 입력하기를 요청하는 정보를 출력

[Input Stream]

cin>> value1;

- 사용자가 입력 하는 정보를 value1에 할당
- 여러개의 정보를 순차적으로 받기 위해서는 순차척으로 진행하면 됨

Operators

[Arithmetic Operators]

[Compound Assignments]

[Increase & Decrease Operator]

- '++ x ' : x에 1을 먼저 더한 후 연산에 이용
- ' x ++' : x를 연산에 사용한 후 +1
- 'x+=1' : x=x+1

- '-- x ' : x에 1을 먼저 뺀 후 연산에 이용
- ' x --' : x를 연산에 사용한 후 -1
- 'x-=1' : x=x-1


[Prefix or Suffix Operation]

x=3;
y= ++x;
// x=4, y=4

a=3;
b= x++;
//x=4, y=3

[Relational Operator]

Relational Operator

- Conditional Ternary Operator "?" : ? 기호 앞이 참 / 거짓 여부에 따라 다른 값을 내보냄

/* Conditional Ternary Operator */
7==5 ? 4:3 		// 7!=5이므로 3이 나옴
7==5+1 ? 4:3 	// 7=5+2이므로 4가 나옴
5>3 ? a:b 		// 5>3이므로 a가 나옴
a>b ? a:b		// 둘 중에 큰 값을 나타냄

[Examples]

//assignment operator
#include <iostream>
using namespace std;

int main () {
	int a,b;
    a=10;
    b=4
    a=b
    b=7
    // 결과적으로 a=4, b=7
}
//compound assignment operator
#include <iostream>
using namespace std;

int main () {
	int a, b=3;
    a=b;
    a+=2;
    //결과적으로 a=5, b=3
}
//conditional operator
#include <iostream>
using namespace std;

int main () {
	int a, b, c;
    a=2;
    b=7;
    c=(a>b) ? a:b;
    // 결과적으로 a=2, b=7,  c=7
}

Comments (주석)

[Enhance Readability]

- Compiler가 무시하고 지나감
- Programmer들의 편의를 위해 존재

// 한 줄 짜리 주석

/*
* 여러줄 짜리
* 주석
*/

참고 내용 )

- 변수의 이름을 설정할때 띄어쓰기를 표현하고 싶으면 대문자를 이용한다. ex) BookShelf
- 아래 표는 operator들의 우선 순위와 방향에 대한 표이다

Precedence Priority

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.