Download presentation
Presentation is loading. Please wait.
Published byDwight Lang Modified over 9 years ago
1
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 C++ 의 소개
2
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 2 Topics 2.1 C++ 프로그램의 구성요소들 2.2 cout 객체 2.3 #include 지시자 2.4 표준 C++, 표준 이전 C++ 2.5 변수, 상수, 배정문 2.6 식별자 2.7 정수 자료형 2.8 문자 자료형
3
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 3 Topics ( 계속 ) 2.9 C++ string 클래스 2.10 부동소수점 자료형 2.11 bool 자료형 2.12 자료형의 크기 결정하기 2.13 변수 배정에 대한 추가사항과 초기화 2.14 변수의 유효범위 2.15 산술 연산자 2.16 주석
4
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 4 2.1 C++ 프로그램의 구성요소들 // sample C++ program #include using namespace std; int main() { cout << "Hello, there!"; return 0; } comment preprocessor directive which namespace to use beginning of function named main beginning of block for main output statement send 0 back to operating system end of block for main
5
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 5 특수문자 문자이름설명 // Double SlashBegins a comment # Pound SignBegins preprocessor directive Open, Close BracketsEncloses filename with #include directive ( ) Open, Close ParenthesesUsed when naming function { } Open, Close BracesEncloses a group of statements " Open, Close Quote MarksEncloses string of characters ; SemicolonEnds a programming statement
6
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 6 2.2 cout 객체 컴퓨터 화면에 정보를 출력한다. 스트림 삽입 연산자 << 을 사용하여 cout 에게 정보를 보낸다. cout << "Hello, there!"; Can use 1 item to cout cout << "Hello, " << "there!"; Or cout << "Hello, "; cout << "there!";
7
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 7 개행방법 To get multiple lines of output on screen - Use endl cout << "Hello, there!" << endl; -Use \n in an output string cout << "Hello, there!\n";
8
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 8 2.3 #include 지시자 프로그램 내에 다른 파일의 내용을 삽입한다. Is a preprocessor directive –Not part of the C++ language –Not seen by compiler 예 : #include No ; goes here
9
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 9 2.4 표준 C++, 표준 이전 C++ 예전 스타일 C++ 프로그램 헤더 파일 끝에.h 사용 #include Do not use using namespace convention May not compile with a standard C++ compiler
10
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 10 2.5 변수, 상수, 배정문 변수 (Variable) –Has a name and a type of data it can hold char letter; –Is used to reference a location in memory where a value can be stored –This value can be changed (i.e. can “vary”) variable name data type
11
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 11 변수 – 새로운 값이 변수에 저장되면 이전 값은 사라진다. int age; age = 17; // age is 17 cout << age; // Displays 17 age = 18; // Now age is 18 cout << age; // Displays 18
12
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 12 상수 상수 (Constant) – 프로그램 실행 동안 값이 변경될 수 없는 자료 요소 –Constants are also called literals( 리트럴 ) 'A' // 문자 상수 "Hello" // 문자열 상수 12 // 정수 상수 3.14 // 부동소수 상수
13
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 13 배정문 배정 연산자 = 를 사용한다. Has a single variable on the left side and a value on the right side Copies the value on the right into the variable on the left item = 12;
14
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 14 2.6 식별자 프로그래머가 정의한 이름 – 변수, 함수 등. 변수의 이름은 변수의 용도를 잘 나타낼 수 있어야 한다. 식별자로 C++ 키워드를 사용할 수 없다. Must begin with alpha character or _, followed by alpha, numeric, or _
15
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 15 유효 식별자 / 비유효 식별자 식별자명유효 ? REASON IF INVALID totalSales Yes total_Sales Yes total.Sales NoCannot contain period 4thQtrSales NoCannot begin with digit totalSale$ NoCannot contain $
16
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 16 2.7 정수 자료형 정수 자료 표현 Can be signed or unsigned 12 -6 +3 Available in different sizes (i.e., number of bytes): short, int, and long Size of short size of int size of long
17
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 17 변수 정의 같은 자료형의 변수 정의 - 분리된 문장에서 int length; int width; - Or 같은 문장에서 int length, width; 서로 다른 자료형의 변수는 다른 문장에 의해 정의되어야 한다.
18
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 18 정수 상수 To store an integer constant in a long memory location, put ‘ L ’ at the end of the number: 1234L Constants that begin with ‘ 0 ’ (zero) are base 8: 075 Constants that begin with ‘ 0x ’ are base 16: 0x75A
19
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 19 2.8 문자 자료형 오직 하나의 문자만을 저장한다. Usually 1 byte of memory A numeric value representing the character is stored in memory CODE MEMORY char letter = 'C'; letter 67
20
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 20 문자열 상수 Can store a series of characters in consecutive memory locations "Hello" Stored with the null terminator, \0, at end Comprised of characters between the " " Hello\0
21
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 21 2.9 C++ string 클래스 Must #include to create and use string objects Can define string variables in programs string name; Can assign values to string variables with the assignment operator name = "George"; Can display them with cout cout << name;
22
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 22 2.10 부동소수점 자료형 실수 표현 12.45 -3.8 Stored in a form similar to scientific notation All numbers are signed Available in different sizes (number of bytes): float, double, and long double Size of float size of double size of long double
23
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 23 부동소수점 상수 Can be represented in -Fixed point (decimal) notation 31.41590.0000625 -Or in E notation 3.14159E16.25e-5 Are double by default Can be forced to be float 3.14159f or long double 0.0000625L
24
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 24 실수 정수 변수에 배정 If a floating-point value is assigned to an integer variable –The fractional part will be truncated (i.e., “chopped off” and discarded) –The value is not rounded int rainfall = 3.88; cout << rainfall; // Displays 3
25
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 25 참 또는 거짓 값을 표현 bool variables are stored as short integers false is represented by 0, true by 1 bool allDone = true; bool finished = false; 2.11 bool 자료형 allDone finished 10
26
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 26 2.12 자료형의 크기 결정하기 The sizeof operator gives the size of any data type or variable double amount; cout << "A float is stored in " << sizeof(float) << "bytes\n"; cout << "Variable amount is stored in " << sizeof(amount) << "bytes\n";
27
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 27 2.13 변수 배정에 대한 추가사항과 초기화 변수에 값 배정 – 값을 기존에 만들어진 변수에 배정한다. –A single variable name must appear on left side of the = symbol int size; size = 5; // legal 5 = size; // not legal
28
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 28 변수 배정 vs. 초기화 변수의 초기화 – 변수가 만들어질 때 변수의 초기값을 배정한다. –Can initialize some or all variables int length = 12; int width = 7, height = 5, area;
29
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 29 2.14 변수의 유효범위 변수의 유효범위 (scope) – 변수가 사용될 수 있는 프로그램 영역 – 변수는 정의되기 전에 사용될 수 없다. int a; cin >> a; // legal cin >> b; // illegal int b;
30
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 30 2.15 산술 연산자 산술계산을 수행하기 위해 사용된다. C++ has unary, binary, and ternary operators –unary (1 operand) -5 –binary (2 operands) 13 - 7 –ternary (3 operands) exp1 ? exp2 : exp3
31
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 31 이항 산술 연산자 SYMBOLOPERATIONEXAMPLE ans + addition ans = 7 + 3;10 - subtraction ans = 7 - 3;4 * multiplication ans = 7 * 3;21 / division ans = 7 / 3;2 % modulus ans = 7 % 3;1
32
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 32 / 연산자 C++ division operator ( /) performs integer division if both operands are integers cout << 13 / 5; // displays 2 cout << 2 / 4; // displays 0 If either operand is floating-point, the result is floating-point cout << 13 / 5.0; // displays 2.6 cout << 2.0 / 4; // displays 0.5
33
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 33 % 연산자 C++ modulus operator ( % ) computes the remainder resulting from integer division cout << 9 % 2; // displays 1 % requires integers for both operands cout << 9 % 2.0; // error
34
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 34 2.16 주석 프로그램에 대한 문서화를 위해 사용된다. 프로그램의 소스 코드를 읽기 쉽게 할 목적으로 사용된다. – 프로그램의 목적 – 변수 사용 용도 설명 – 복잡한 코드의 설명 컴파일러에 의해 무시된다.
35
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 35 C++ 스타일 주석 Begin with // through to the end of line int length = 12; // length in inches int width = 15; // width in inches int area; // calculated area // Calculate rectangle area area = length * width;
36
© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 36 C- 스타일 주석 Begin with /* and end with */ Can span multiple lines /*---------------------------- Multi-line C-style comment ----------------------------*/ Can be used like C++ style comments int area; /* Calculated area */
37
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction to C++
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.