3학년 1학기 전공/객체지향 프로그래밍
                
              [객체지향프로그래밍] 3주차 정리
                시데브
                 2024. 3. 19. 01:21
              
              
            
            경희대학교 컴퓨터공학부 이대호 교수님의 객체지향프로그래밍 수업 자료를 참고한 정리본
Expressions
Operator and Operand(연산자와 피연산자)
- symbol and object of operations
 - 5 + 10 -> operand를 2개 필요로하는 binary(이항) 연산자
 - -x -> operand를 1개 필요로 하는 unary(단항) 연산자
 - 5 + 10 / 2 -> precedence: 연산자 우선순위
 - a = b = 2 -> associativity: 일반적인 산술연산은 왼쪽부터 오른쪽으로 연산, but 할당(assignment)연산의 경우에는 오른쪽에서 왼쪽으로
 
Arithmetic Operators
- +, -, *, /, % -> %연산자의 피연산자는 정수
 - +, - < *, /, %
 
std::cin
- standard input stream
 - >>: extraction operator
 - int x;
 - std::cin >> x;
 
#include <iostream>
using namespace std;
int main(void) {
    int x, y = 5, z;
    
    cin >> x >> y;
    
    cout << x * y << endl;
    
    cin >> z;
    
    cout << x * y * z << endl;
    return 0;
}
2 3 6
6
36
Program ended with exit code: 0
-> 입력을 한 번에 받아도 문제없음
#include <iostream>
using namespace std;
int main(void) {
    cout << 10 % 3 << " " << 3 % 10 << endl;
    
    char lower = 'd', upper = lower - 32;
    // char upper = lower - ('a' - 'A');
    cout << upper << endl;
    return 0;
}
1 3
D
Program ended with exit code: 0
Mixed Type Expressions
- 5. / 2, 5 / 2. -> 실수 저장 범위가 더 넓으므로, 결과로 실수를 반환
 
#include <iostream>
using namespace std;
int main(void) {
    int x = 4;
    double y = 10.2, sum;
    int sum2;
    
    sum = x + y;    //double 저장
    sum2 = sum; //type이 맞지 않아 경고 메세지 발생 -> 형변환 하고 저장 (int)sum
    //c++ 스타일 -> or static_cast<int>(sum)
    return 0;
}

Operator Precedence and Associativity
Precedence
- 다른 종류의 연산자들이 혼합된 수식에서 어떤 연산을 먼저 진행할지 결정
 
Associativity(연산자 결합성)
- 동일한 우선순위를 가진 연산자에서, 어떤 연산을 먼저 처리할지 결정
 - 이항 연산자보다 단항 연산자의 우선순위가 높음
 

Comments
Comments
- 주석
 - 설명 annotation
 - compiler나 interpreter에서 무시됨
 - //
 - /*...*/ -> 중복해서 사용 X, line commentd와 달리 수식 안에서(한 줄 안에서) 부분적으로 사용 가능
 
Formatting

-> 어떻게 쓰던 다 똑같음
Errors and Warnings
Compile-time Error
- compile error -> syntax error
 - link error -> 외부에 있는 걸 썼는데, 외부에 없는 경우
 
Run-time Error
- 실행시간에 발생
 - 2 / 0 -> division by zero
 - 메모리 누수 memory leak
 - invalid memory access
 
Logic Error
- division by zero
 - wrong formula/output -> 수식을 잘못쓴 경우
 
Compiler Warnings
- Narrowing conversion -> 넓은 범위에서 좁은 범위 데이터 타입으로 변경 -> 명시적으로 형변환하지 않고 진행
 - uninitialized object -> 변수를 초기화하지 않고 이용
 
Integers vs. Floating-point Numbers
- 컴퓨터는 모든 데이터를 내부적으로 이진적으로 저장
 - Integers -> overflow/underflow
 

char -> -128 ~ +127 -> 8비트 2진수 생각
char c = 128
cout << (int)c << endl 
->> -128
-> 제일 큰 값과 제일 작은 값은 +-1을 하면 서로가 될 수 있음
Integers vs. Floating-point Numbers
Floating-point numbers
- sign, mantissa, exponent
 - 부호, 가수, 지수
 
#include <iostream>
#include <iomanip>
using namespace std;
int main(void) {
    double d1 = 2000.5;
    double d2 = 2000.0;
    cout << setprecision(16) << (d1 - d2) << endl;
    // 0.5
    
    double d3 = 2000.58;
    double d4 = 2000.0;
    cout << setprecision(16) << (d3 - d4) << endl;
    // 0.5799999999999272 -> 2진수-10진수 변환 과정에서 아주 작은 오차 발생
    return 0;
}
0.5
0.5799999999999272
Program ended with exit code: 0
-> epsilon을 지정해서 오차가 epsilon보다 작으면 무시
More Arithmetic Operators
Incremetn/decrement
- ++x, x++, --x, x--
 
prefix, postfix
#include <iostream>
using namespace std;
int main(void) {
    int x1 = 1, y1 = 10, x2 = 100, y2 = 1000;
    cout << "x1 = " << x1 << ", y1 = " << y1 << ", x2 = " << x2 << ", y2 = " << y2 << endl;
    y1 = x1++;	//y1에는 증가되기 이전의 값으로 초기화하고, x1 증가 적용
    cout << "x1 = " << x1 << ", y1 = " << y1 << ", x2 = " << x2 << ", y2 = " << y2 << endl;
    y2 = ++x2;	//x2가 증가되고 나서 y2에도 적용
    cout << "x1 = " << x1 << ", y1 = " << y1 << ", x2 = " << x2 << ", y2 = " << y2 << endl;
    return 0;
}
x1 = 1, y1 = 10, x2 = 100, y2 = 1000
x1 = 2, y1 = 1, x2 = 100, y2 = 1000
x1 = 2, y1 = 1, x2 = 101, y2 = 101
Program ended with exit code: 0
Assignment operators
- +=, -=, *=, /=, %= -> 하나의 연산자 취급
 - x *= 4 + 6 -> *=연산자가 +연산자보다 우선순위가 낮음
 
Bitwise operators
- &
 - |
 - ^ -> exclusive or
 - ~ -> 1의 보수
 - >> -> bit 단위로 오른쪽으로 이동, x >> 2 -> 오른쪽으로 2bit만큼 이동
 - << -> bit 단위로 왼쪽으로 이동
 
#include <iostream>
using namespace std;
int main(void) {
    int x = 3;
    
    cout << (x << 1) << endl;   //왼쪽으로 1비트 이동시키니까 곱하기 2
    cout << (x << 2) << endl;   //곱하기 4
    cout << (x << 3) << endl;   //곱하기 8
    
    x = 100;
    cout << (x >> 1) << endl;   //오른쪽으로 1비트 이동시키니까 나누기 2
    cout << (x >> 2) << endl;   //나누기 4
    cout << (x >> 3) << endl;   //나누기 8
    
    return 0;
}
6
12
24
50
25
12
Program ended with exit code: 0
728x90
    
    
  반응형