C++ 프로그래밍은 C문법 + OOP 개념(문법) 으로 구성되어 있다.
C문법은 보통 소단위 작업을 할 때 쓰이고, C++은 대단위 작업을 할 때 쓰인다.
cout : 객체
<< : inserter(연산자 오버로딩) - cout 클래스의 멤버 함수 중 하나
"C++ 프로그래밍" : 전달인자 (출력대상)
cout : 객체
operator<< : 연산자 overloading 함수명
cin : 객체
>> : extractor(연산자 오버로딩) - cin 클래스의 멤버 함수 중 하나
num : 전달인자
cin : 객체
operator>> : 연산자 overloading 함수명
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
32
33
34
35
36
37
38
39
40
41
|
#include <iostream> // cin, cout, endl 객체 사용을 위해
using namespace std;
void my_flush(); // cin buffer(내부에서 stdin 사용)를 모두 비우는 함수
int main(void) {
int intNumber;
double doubleNumber;
char ch;
char str[100];
cout << "정수값 : ";
cin >> intNumber;
cout << "실수값 : ";
cin >> doubleNumber;
cout << "문자 : "; // 구분자는 불가
cin >> ch;
cout << "문자열 : "; // 여백없는 문자열만 가능
cin >> str;
cout << "intNumber = " << intNumber << endl;
cout << "doubleNumber = " << doubleNumber << endl;
cout << "ch = " << ch << endl;
cout << "str = " << str << endl;
my_flush();
cout << "여백있는 문자열 : ";
cin.getline(str, sizeof(str)); // 지정문자 생략 시 지정문자는 '\n'. 지정문자는 저장X
cout << "입력 받은 문자열 : " << str << endl;
cout << "여백문자을 입력(space, tab, enter) : ";
ch = cin.get();
cout << "입력한 여백문자의 아스키코드 값 : " << (int)ch << endl;
return 0; // 생략 가능
}
void my_flush() {
while (cin.get() != '\n');
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include <iostream>
using namespace std;
void my_flush();
int main(void) {
int num;
cin >> num;
while (cin.fail()) {
my_flush();
cin >> num;
}
cout << "num = " << num << endl;
return 0;
}
void my_flush() {
cin.clear();
while (cin.get() != '\n');
}
|
cs |
< C++에서 주소 출력 >
1
2
3
4
5
6
7
8
9
10
|
#include <iostream>
using namespace std;
int main(void) {
int intArray[5] = { 1, 3, 5, 7, 9 }
char charArray[10] = "banana";
cout << intArray << endl; // 주소출력
cout << charArray << endl; // 문자열출력
cout << (void*)charArray << endl; // 주소출력
return 0;
}
|
cs |
조정자 | default 값 | 의 미 |
dec, hex, oct | dec(10진수) | 정수값을 10진수, 8진수, 16진수로 출력한다. |
showbase, noshowbase | noshowbase | 16진수, 8진수 진법접두어(0x, 0) 출력 설정, 취소 |
fixed | 소수점 형태로 출력 | |
scientific | 지수 형태로 출력 | |
setprecision(n) | 일반 출력모드에서는 유효숫자 n자리 출력 fixed와 scientific 모드에서는 소수점 이하 n자리 출력 |
|
setw(n) | field 폭 지정 ( 1회성 ) | |
left, right | right | 왼쪽, 오른쪽 정렬 |
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
|
#include <iostream>
#include <iomanip> // setprecision과 setw를 사용하기 위함
using namespace std;
int main(void) {
int number = 12;
cout << showbase; // 지금부터 진법접두어 출력 설정
cout << "10진수로 출력 : " << number << endl;
cout << "16진수로 출력 : " << hex << number << endl;
cout << "8진수로 출력 : " << oct << number << endl;
double dnumber = 71.2345;
cout << setprecision(3); // 3자리 출력
cout << dnumber << endl; // (일반모드) 유효숫자 3자리 출력
cout << fixed; // fixed 모드로 변경
cout << dnumber << endl; // (fixed 모드) 소수점 형태로 출력
cout << scientific; // scientific 모드로 변경
cout << dnumber << endl; // (scientific 모드) 지수 형태로 출력
cout << "|" << setw(10) << number << "|" << setw(15) << dnumber << "|" << endl;
cout << left;
cout << "|" << setw(10) << number << "|" << setw(15) << dnumber << "|" << endl;
cout << right;
cout << "|" << setw(10) << number << "|" << setw(15) << dnumber << "|" << endl;
return 0;
}
|
cs |
상수명 | 의 미 |
ios::showbase | 출력에서 C++의 기준 접두어(0, 0x)를 붙인다. (showbase 기능과 같음) |
ios::showpoint | 소수점과 소수점 이하의 0을 생략하지 않는다. 일반 출력 모드에서는 유효숫자 6자리로 출력 fixed나 scientific 모두에서는 소수점 이하 6자리까지 출력 |
ios::uppercase | 16진수 출력을 위해 대문자를 사용 |
ios::showpos | 양수 앞에 +부호 붙임 |
setf( ) 멤버함수를 통해 지정된 각각의 기능들은 unsetf( )함수에 의해 기능 해제 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#include <iostream>
using namespace std;
int main(void) {
int a = 10, b = 365;
float f = 3.14, g = 5.0;
cout.setf(ios::showpos);
cout << a << endl;
cout << f << endl;
cout << hex << a << endl; // 앞으로의 정수는 16진수로 출력
cout.setf(ios::showbase);
cout << a << endl;
cout << b << endl;
cout.setf(ios::uppercase);
cout << a << endl;
cout << b << endl;
cout.setf(ios::showpoint);
cout << f << endl;
cout << g << endl;
return 0;
}
|
cs |
첫 번째 전달인자 | 두 번째 전달인자 | 의 미 |
ios::fixed | ios::floatfield | 고정 소수점 표기 사용 |
ios::scientifc | 과학적 표기(지수표기) 사용 | |
ios::right | ios::adjustfield | 우측정렬 사용 |
ios::left | 좌측정렬 사용 | |
ios::internal | 부호는 좌측정렬, 값은 우측정렬 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include <iostream>
using namespace std;
int main(void) {
float f = 3.14, g = 150.0;
cout.setf(ios::showpos);
cout.setf(ios::showpoint);
cout.precision(3);
cout << f << "\n";
cout << g << "\n\n";
cout.setf(ios::scientific, ios::floatfield);
cout << f << "\n";
cout << g << "\n\n";
return 0;
}
|
cs |
▶ C언어의 구조체
public : 모든 함수에서 접근 가능한 member
구조체 형틀을 선언할 당시에는 메모리가 할당되지 않으나,
구조체 변수를 선언하면 메모리가 할당된다.
▶ C++의 객체
접근지정자
▷ private : 사적 member (비공개된 정보)
▷ public : 공적 member (공개된 정보)
data member를 private로 선언하는 이유는 데이터의 무결성을 보장하기 위함.
ob는 Person class의 객체로 data member만 메모리가 할당된다.
[TIL] 1 - 6. 객체지향 기본 문법 - 참조 (0) | 2022.02.09 |
---|---|
[TIL] 1 - 5. 객체지향 기본 문법 - const의 이해 (0) | 2022.02.08 |
[TIL] 1 - 4. 객체지향 기본 문법 - C++에서 확장된 함수의 기능 (0) | 2022.02.08 |
[TIL] 1 - 3. 객체지향 기본 문법 - 네임스페이스 (0) | 2022.02.08 |
[TIL] 1 - 2. 객체지향 기본 문법 - C++에 추가된 데이터 타입 및 향상된 for문 (0) | 2022.02.07 |
댓글 영역