본문 바로가기

Programming/C++

(10)
C++ 기초 - Procedural language : algorithm oriented - Structured programming language : no unconditional jump, still data is separated from algorithm - Object oriented programming language : algorithm(behavior) + data(state) -> object using namespace std; 를 이용하여 cout을 이용한다면 cout이라는 객체한테 그 뒤에 나오는 string 글 변수 등을 맡기는 것 - 그리고 알아서 출력을 해준다 Linux에서 c++ compile을 돌릴려면 g++ [c++ source code file name] -o [exe_file ..
C++ 이중포인터와 동적 다차원 배열 이중 포인터의 이해를 돕기위한 코드 #include using namespace std; int main() { int *ptr = nullptr; int **ptrptr = nullptr; int value = 5; ptr = &value; ptrptr = &ptr; cout
C++ 메모리 동적 할당, 동적 할당 배열 메모리를 새로 할당하고 제거할때 이러한 코드들을 작성한다. #include using namespace std; int main() { // new int 는 int 사이즈만큼 메모리 받아오고 // 그 주소를 알려줌 그래서 포인터로 받아야 한다. int *ptr = new int(7); cout
c++ Pointer의 기초 c 언어가 시스템의 기초를 다루는데 가장 유용한 언어라는 것도 알겠다. 정적 메모리와 동적 메모리를 관리하는데도 유용하고 #include using namespace std; int main() { int x = 5; cout
C++ 난수 random number 만들기 #include #include //std::radn(), std::srand() #include using namespace std; int main() { //std::srand(5343); //seed를 설정해주는 것 //다만 이렇게 되면 seed number가 고정이 된다. //그래서 srand를 time과 연결시켜준다 std::srand(static_cast(std::time(0))); for (int i = 0; i < 100; i++) { cout
C++ 배열의 반복 #include using namespace std; int main() { const int num_students = 5; int scores[num_students] = {84, 92, 76, 81, 56}; //동적할당을 하면 이것도 자동화가 가능하다. //오히려 미리 사전에 배열의 크기를 정하는 것이 아니라 //입력된 숫자를 가지고 계산을 할 수도 있다. //역으로 5로 계산되는 것을 알 수 있다. //const int num_students = sizeof(scores) / sizeof(int); //함수 parameter로 넘어간거면 pointer 주소로 넘어간 거여서 다른 숫자가 나옴 int max_score = 0; int total_score = 0; for (int i = 0; i <..
C++ 배열의 기초 #include using namespace std; int main() { int one_student_score; // 1 variable int student_scores[5]; // 5 int // array의 각각 element는 각각의 변수로 사용할 수 있다. return 0; } struct를 이용하여 array를 사용하는 방법 #include using namespace std; struct Rectangle { int length; int width; }; int main() { cout
C++ Fundamental data Types, Integers, Float, Char type C++에서 변수를 초기화 하는 방법 int a = 123; 같이 초기에 초기화 시키는 방법 copy initialzation int a(123); 괄호로 초기화 direct initialization int b { 123 }; 중괄호로 초기화 uniform initialization 밑에 두가지는 직접 데이터 타입을 만들어서 사용할 때 유용하게 사용된다. 강제적으로 데이터 타입을 바꿀때 int i = (int)3.1415 copy initialization int a((int)3.1415) direct initialization은 타입명을 맞추어서 초기화를 시키면 되지만 uniform initialization은 어떻게든 타입을 맞추어주어야 한다. ------------- char 문자의 characte..