w2 c++ 배우기 - OOP

hongregii·2023년 3월 10일
0

OOP basics!

목록 보기
1/1
#include <iostream>
using namespace std;

네임스페이스 <- 헷갈리지 말라고

int main()
{
	int numberOfLanguages;
    
}

메인 함수

  • variables : memory location to store data for a program. must be declared before being used.
// syntax
typeName VariableName = 10;
  • c++ identifiers : caseSensitive. 숫자로 시작하면 안됨. letters, digits, _

declare 만 하고 initialize 하지 않으면 -> undefined 나옴
int count = 0 ; 하라고

Data Assignment Rules

  • Type mismatch 하지 마씨오
    타입이란 것은 한번 선언되면 다시 못바꿈
  • intVar = 2.99; 이래 하면 2가 들어간다.

casting / explicit type conversion
따로 타입 바꿔주면 됨

  • 그냥 literal로 선언 ex. 2 <- constant로 간주, 바꿀 수 없음.
    named constant를 쓰는 게 더 좋다. (= declared constant)
const int NUMBER_OF_STUDENTS = 24

escape sequences

  • \
  • \, \ㅅ, \\, \", \'

Arithmetic Precision

  • 타입을 잘 봐라~ 계산할때 제대로 안나온다
  • ex. 17 / 5 = 3임. <- integer division

Type Casting

  • 계산 지대로 하려면 ".0" 더하면 된다. 그러나 이게 끝이 아니고

  • Implicit conversion : 자동으로 됨

  • Explicit conversion : 개발자가 함

Shorthand Operators

  • ++
  • --
  • someVar++ : post-increment : 계산 후 원래 변수에 (+1) reassign
  • ++someVar : pre-increment : (+1) reassgin 후 계산

console output / input

하려면
#include <iostream>, using namespace std; 해야 함.

  • 줄바꿈은 \n. <<endl; 해도 됨

String type

  • #include <string> 해야 선언 가능 ㅋㅋ
  • cin >> str 하면 공백문자 까지만 들어감. 하면 그대로 그 전까지 입력됨

Formatting Numbers

"Magic Formula" to force decimal sizes:

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

중략

Program style

  • comments : //, /* */
  • constants : ALL_CAPS
  • variables : lowerToUpper <- snakeCase 말인듯?

libraries

  • #include <Library_Name>
    library 파일을 program file에 복사함. 컴파일러 작동 전에 실행됨.

Namespace

  • 여러개가 있지만, 일단은 std 사용
profile
잡식성 누렁이 개발자

0개의 댓글