32
[CSE10200] Programming Basis (프로그래밍 기초) Chapter 2 Seungkyu Lee Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture3.pdf · 2014. 3. 16. · Float constant 3.141592 Character constant ‘d’ Character constant ‘\0’ String constant “Hello word

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

  • [CSE10200] Programming Basis

    (프로그래밍 기초)

    Chapter 2

    Seungkyu Lee

    Assistant Professor, Dept. of Computer Engineering

    Kyung Hee University

  • Chapter 2

    Introduction to the C++ Language

  • Figure 2-1 Taxonomy of the C++ language

  • Structure of a C++ program

  • Pre-compiler directive

    Opening brace

    Closing brace

    Opening brace

    Closing brace

    Structure of a C++ Program

  • Preprocessor Directives

    #include “I want to use a predefined library

    called iostream”

    Always start with a ‘#’

    iostream: a library for inputs (from

    e.g., a user) and outputs (to e.g.,

    the monitor)

  • Without namespace

    Hello World!

    Namespace std contains

    all the classes, objects

    and functions of the

    standard C++ library.

    #include

    int main ()

    {

    std::cout

  • main function

    int main() • The main body of my program.

    • Compiler first tries to locate “main()” to find where to begin the program

    • This is a function

  • Comment

    • Internal program document

    • Not considered as a program code

    Start of comment

    End of comment

    Start of comment

    End of comment

  • Nested Block Comments are Invalid

  • Identifiers

    Memory

    Address of memory:

    Hard to remember

    Identifier: name of

    address

  • Identifiers

    Memory

    studentID

    studentGrade1

    studentGrade2

    Identifiers

  • Identifiers

    Memory

    studentID

    studentGrade

    studentName

    Compiler keeps track

    of [identifier-address]

    table

  • Naming Identifiers

    • Allowed characters: A-Z, a-z, 0-9, _ (underscore)

    • Not allowed to start with a digit. E.g., 3class (x), class3(o)

    • The identifier cannot duplicate a reserved word. e.g., if, case, while…

    • Good names descriptive but short

    • C++ is case sensitive; PI, Pi and pi are different.

  • Standard Data Types

  • Integer and Floating Types

    2 or 4 Bytes

    4 Bytes

    2 Bytes

    8 Bytes

    10 Bytes

    4 Bytes

    Size of value type depends on computer architecture

  • Max/Min of Integer Value Type

    Type Sign Byte Minimum value Maximum value

    short int/short signed

    2 -32,768 32,767

    unsigned 0 65,535

    int (PC) signed

    2 -32,768 32,767

    unsigned 0 65,535

    int (Mainframe) signed

    4 -2,147,483,648 2,147,483,647

    unsigned 0 4,294,967,295

    long int/long signed

    4 -2,147,483,648 2,147,483,647

    unsigned 0 4,294,967,295

  • Maximum/Minimum of C++ data types

  • In C++ the Boolean constants are true

    and false. Additionally, following

    traditional standards, any nonzero

    number is considered true, and zero is

    considered false.

    Note:

  • Variables

    • Variable = Identifier + Type

  • Variables Declaration

    = Identifier & Type assignment

  • Variable Initialization

    • Variable declaration

    ≠ Variable initialization

    • Should be initialized by a programmer before it is used

    int count; declaration (o), initialization(x)

    char grade = ‘d’; declaration (o), initialization(o)

  • Constants

    • Data values that cannot be changed during program execution

    • E.g.,

    Integer constant 4

    Float constant 3.141592

    Character constant ‘d’

    Character constant ‘\0’

    String constant “Hello word”

  • The only bool types constants are true,

    printed as 1, and false, printed as 0.

    Note:

  • So far ~~ so good ?

  • Standard streams

    • A mapping between data and input/output device

  • More about cout

    • width(int) function sets the width for

    printing a value

    • Only works until the next insertion command comes

    int x = 42;

    cout.width(5);

    cout

  • More about cout

    • fill(char) function sets the fill

    character.

    • The character remains as the fill character until set again.

    int x = 42;

    cout.width(5);

    cout.fill(‘*’);

    cout

  • More about cout • precision (int) sets the number of significant

    digits of float type numbers

    float y = 23.1415;

    cout.precision(1);

    cout

  • More about cout • Output Manipulators (not a function)

    endl - outputs a new line character, flushes output

    dec - sets int output to decimal

    hex - sets int output to hexadecimal

    oct - sets int output to octal

    #include

    int x = 42;

    cout

  • Example codes reading (Program 2-2)

    • #include

    • using namespace std;

    • int main (void)

    • {

    • int a;

    • int b;

    • int c;

    • int sum;

    • cout a >> b >> c;

    • // Numbers are now stored in a, b, and c. Add them.

    • sum = a + b + c;

    • cout