© 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++

Slides:



Advertisements
Similar presentations
CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
Advertisements

CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
© 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.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Basic Elements of C++ Chapter 2.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
Input & Output: Console
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Introduction to C The Parts of a C++ Program –Anatomy of a simple C++ program.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Lecture 3: The parts of a C++ program (Cont’d) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 2 Introduction to C++ Department of Computer Science Missouri State University.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Chapter 2 Variables.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
1 17/4/1435 h Monday Lecture 3 The Parts of a C++ Program.
Introduction to C++. 2 What Is a Program Made Of?
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Chapter 2 Introduction to C++. 2 Topics 2.1 Parts of a C++ Program 2.2 The cout Object 2.3 The #include Directive 2.4 Variables and Constants 2.5 Identifiers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Lecture 2 Variables, Types, Operators Sampath Jayarathna Cal Poly Pomona Based on slides created by Bjarne Stroustrup & Tony Gaddis CS 128 Introduction.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2: Introduction to C++
Documentation Need to have documentation in all programs
Basic Elements of C++.
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Chapter 2: Introduction to C++
2.1 Parts of a C++ Program.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 2: Introduction to C++.
Lecture 2 Variables, Types, Operators
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Presentation transcript:

© 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++ 의 소개

© 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.6 식별자 2.7 정수 자료형 2.8 문자 자료형

© 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 주석

© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 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

© 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

© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide cout 객체 컴퓨터 화면에 정보를 출력한다. 스트림 삽입 연산자 << 을 사용하여 cout 에게 정보를 보낸다. cout << "Hello, there!"; Can use 1 item to cout cout << "Hello, " << "there!"; Or cout << "Hello, "; cout << "there!";

© 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";

© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide #include 지시자 프로그램 내에 다른 파일의 내용을 삽입한다. Is a preprocessor directive –Not part of the C++ language –Not seen by compiler 예 : #include No ; goes here

© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 표준 C++, 표준 이전 C++ 예전 스타일 C++ 프로그램 헤더 파일 끝에.h 사용 #include Do not use using namespace convention May not compile with a standard C++ compiler

© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 변수, 상수, 배정문 변수 (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

© 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

© 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 // 부동소수 상수

© 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;

© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 식별자 프로그래머가 정의한 이름 – 변수, 함수 등. 변수의 이름은 변수의 용도를 잘 나타낼 수 있어야 한다. 식별자로 C++ 키워드를 사용할 수 없다. Must begin with alpha character or _, followed by alpha, numeric, or _

© 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 $

© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 정수 자료형 정수 자료 표현 Can be signed or unsigned Available in different sizes (i.e., number of bytes): short, int, and long Size of short  size of int  size of long

© 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; 서로 다른 자료형의 변수는 다른 문장에 의해 정의되어야 한다.

© 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

© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 문자 자료형 오직 하나의 문자만을 저장한다. Usually 1 byte of memory A numeric value representing the character is stored in memory CODE MEMORY char letter = 'C'; letter 67

© 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

© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 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;

© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 부동소수점 자료형 실수 표현 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

© 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 Or in E notation E16.25e-5 Are double by default Can be forced to be float f or long double L

© 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

© 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

© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 자료형의 크기 결정하기 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";

© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 변수 배정에 대한 추가사항과 초기화 변수에 값 배정 – 값을 기존에 만들어진 변수에 배정한다. –A single variable name must appear on left side of the = symbol int size; size = 5; // legal 5 = size; // not legal

© 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;

© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 변수의 유효범위 변수의 유효범위 (scope) – 변수가 사용될 수 있는 프로그램 영역 – 변수는 정의되기 전에 사용될 수 없다. int a; cin >> a; // legal cin >> b; // illegal int b;

© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 산술 연산자 산술계산을 수행하기 위해 사용된다. C++ has unary, binary, and ternary operators –unary (1 operand) -5 –binary (2 operands) –ternary (3 operands) exp1 ? exp2 : exp3

© 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

© 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

© 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

© 2006 Pearson Education. All Rights Reserved Chapter 2 Starting Out with C++: Early Objects 5/e slide 주석 프로그램에 대한 문서화를 위해 사용된다. 프로그램의 소스 코드를 읽기 쉽게 할 목적으로 사용된다. – 프로그램의 목적 – 변수 사용 용도 설명 – 복잡한 코드의 설명 컴파일러에 의해 무시된다.

© 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;

© 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 */

© 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++