티스토리 뷰
c++을 계속 써왔고 이제 제법 편하게 쓰고 있지만,
처음에 책을 보고 공부했을때 많은 부분을 제대로 이해 하지 않고 넘어간것 같기도 하고
프로그래밍에 대해 조금이나마 더 이해하게 된 지금 다시 책을 읽어 보고 싶다는 생각이 계속 있었다.
또한 이 책은 단순히 c++의 문법 말고도 객체 지향이라는 프로그래밍 방법에 대해서도 꽤 상세히 적혀있기에 너무 오래 전에 배워서 까먹어가는 c언어의 복습도 하면서 다시 한번 책을 정독해볼까 한다.
문제 2-1 [참조자 기반의 Call-by-reference 구현]
문제 1
#include <iostream>
using namespace std;
void func1(int &num)
{
num++;
}
void func2(int &num)
{
num *= -1;
}
int main()
{
int num = 1;
func1(num);
cout << num << endl;
func2(num);
cout << num << endl;
}
문제 2
참조자 선언은 변수를 대상으로만 가능.
문제 3
#include <iostream>
using namespace std;
void SwapPointer(int *(&ptr1), int *(&ptr2))
{
int *temp = ptr1;
ptr1 = ptr2;
ptr2 = temp;
}
int main()
{
int num1 = 5;
int *ptr1 = &num1;
int num2 = 10;
int *ptr2 = & num2;
cout << num1 << ' ' << num2 << endl;
cout << *ptr1 << ' ' << *ptr2 << endl;
SwapPointer(ptr1, ptr2);
cout << *ptr1 << ' ' << *ptr2 << endl;
}
문제 2-2
const 포인터와 const 참조자
#include <iostream>
using namespace std;
int main() {
const int num = 12; // 상수화된 변수
// int *ptr = # // 컴파일 에러
const int *ptr = # // 상수화된 변수를 카리키는 포인터또한 상수화해줘야함
const int *(&ref) = ptr; // 상수화된 변수를 카리키는 포인터의 참조자
cout << *ptr << endl;
cout << *ref << endl;
}
참조자의 선언은 상수가 아닌 변수에만 가능하다.
하지만 다음과 같은 선언은 가능하다
const int &ref = 10;
c++은 const 참조자를 이용해 상수를 참조할때 임시변수 라는것을 만듦.
이 임시변수에 10을 저장하고 참조자가 이를 참조하도록 함.
int Adder(const int &num1, const int &num2)
{
return num1 + num2;
}
int main() {
// const 참조자의 상수참조를 허용함으로서 이와같은 코드가 동작가능.
cout << Adder(3,4) << endl;
}
구조체 포인터 변수
struct point pos = {1,2};
struct point * pptr = &pos; // 구조체 포인터
(*pptr).xpos = 10; // 구조체 포인터의 접근
(*pptr).xpos = 20;
pptr->xpos = 10; // 이런식으로도 접근가능
pptr->ypos = 20;
문제 2-3 [구조체에 대한 new & delete 연산]
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
struct Point
{
int xpos;
int ypos;
};
Point& PntAdder(const Point &p1, const Point &p2)
{
Point *tmp = new Point;
tmp->xpos = p1.xpos + p2.xpos;
tmp->ypos = p2.xpos + p2.ypos;
return *tmp;
}
int main()
{
Point *p1 = new Point;
p1->xpos = 10;
p1->ypos = 20;
Point *p2 = new Point;
p2->xpos = 10;
p2->ypos = 30;
Point &res = PntAdder(*p1, *p2);
cout << res.xpos << ' ' << res.ypos;
delete p1;
delete p2;
delete &res;
}
문제 2-4 [C++의 표준함수 호출]
문제 1
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
int main()
{
char *str1 = "abcde";
char *str2 = "ABCD";
char str3[100];
cout << strlen(str1) << endl;
cout << strlen(str2) << endl;
strcpy(str3, str1);
cout << str3 << endl;
strcat(str3, str1);
cout << str3 << endl;
if(strcmp(str1, str2) == 0) cout << "same" << endl;
else cout << "not same" << endl;
}
문제 2
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
// time을 기반으로 시드 설정
srand(time(NULL));
for(int i = 0; i < 5; i++)
{
// 100 나머지 연산으로 100 미만의 값
cout << rand() % 100 << endl;
}
}
'윤성우의 열헐 C++' 카테고리의 다른 글
윤성우의 열혈 c++) OOP 단계별 프로젝트 04단계 (0) | 2022.03.19 |
---|---|
윤성우의 열혈 c++) OOP 단계별 프로젝트 03단계 (0) | 2022.03.16 |
윤성우의 열혈 c++) OOP 단계별 프로젝트 02단계 (0) | 2022.03.16 |
윤성우의 열혈 c++) Chapter 04. 클래스의 완성 (0) | 2022.03.14 |
윤성우의 열혈 c++) Chapter 03. 클래스의 기본 (0) | 2022.03.14 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 재귀
- 자료구조
- greedy
- MVC
- BFS
- Dijkstra
- permutation
- Brute Force
- 조합
- priority queue
- binary search
- DP
- graph
- recursion
- db
- Stack
- back tracking
- dfs
- C++
- C
- Implementation
- Tree
- 이분탐색
- Python
- CSS
- Unity
- two pointer
- Spring
- Kruskal
- floyd warshall
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함