Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Introduction to C++ Programming

Shaobai Kan
chapter 2

Introduction to C++ Programming – p. 1/2


Outline

Introduction to C++ Programming – p. 2/2


Outline
• Introduction to C++ and its development
environment

Introduction to C++ Programming – p. 2/2


Outline
• Introduction to C++ and its development
environment

• An example of C++ program

Introduction to C++ Programming – p. 2/2


Outline
• Introduction to C++ and its development
environment

• An example of C++ program

• Lab and Exercises

Introduction to C++ Programming – p. 2/2


Introduction to C++ and its development
environment

Introduction to C++ Programming – p. 3/2


C++ & Operating System

• C++ programming language.

———– High - level language.

• Windows, Unix, Linux and Mac OS X.

———– Operating systems.

Introduction to C++ Programming – p. 4/2


Standard C++

ANSI / ISO C++. the internationally agreed-upon version


that is portable to any platform and any development
environment.

Introduction to C++ Programming – p. 5/2


Standard C++

ANSI / ISO C++. the internationally agreed-upon version


that is portable to any platform and any development
environment.

Question: What is ANSI? ISO?

Introduction to C++ Programming – p. 5/2


Standard C++

ANSI / ISO C++. the internationally agreed-upon version


that is portable to any platform and any development
environment.

Question: What is ANSI? ISO?

• ANSI - American National Standards Institute

• ISO - International Organization for


Standardization

Introduction to C++ Programming – p. 5/2


Integrated Development Environment (IDE)

IDE. a software application that provides comprehensive


facilities to computer programmers for software
development.

Introduction to C++ Programming – p. 6/2


Integrated Development Environment (IDE)

IDE. a software application that provides comprehensive


facilities to computer programmers for software
development.

An IDE consists of
- a code editor
- a compiler
- a debugger
- a graphic user interface (GUI) builder

Introduction to C++ Programming – p. 6/2


Integrated Development Environment (IDE)

• Computer Lab
—Visual C++ 2005 & Visual C++ 2008

• Home
—Visual C++ 2008 Express Edition
http://www.microsoft.com/express/download/

Introduction to C++ Programming – p. 7/2


C++ Library

All C++ compilers come with a library of useful


functions and classes that you can include in your
program.

Introduction to C++ Programming – p. 8/2


C++ Library

All C++ compilers come with a library of useful


functions and classes that you can include in your
program.

• function. a block of code that performs one or more


tasks, such as adding two numbers or printing to the
screen.

Introduction to C++ Programming – p. 8/2


C++ Library

All C++ compilers come with a library of useful


functions and classes that you can include in your
program.

• function. a block of code that performs one or more


tasks, such as adding two numbers or printing to the
screen.

• class. the definition of a new type.

Introduction to C++ Programming – p. 8/2


An example of C++ program

Introduction to C++ Programming – p. 9/2


1st Example: Hello World!

hello.cpp

// Shaobai Kan, 9/1/08


Comments
// Text-Printing Program
Preprocessor Directive
# include <iostream>

“main” function int main( )


{
Executable statement 1
std::cout << “Hello World!\n” ;

Executable statement 2 return 0 ;

Introduction to C++ Programming – p. 10/2


1st Example: Result

Output.

Hello World!

Introduction to C++ Programming – p. 11/2


Line-by-Line Analysis (Comments)

Lines 1 & 2.
" //Shaobai Kan, 9/1/08
//Text-Printing Programming "

Introduction to C++ Programming – p. 12/2


Line-by-Line Analysis (Comments)

Lines 1 & 2.
" //Shaobai Kan, 9/1/08
//Text-Printing Programming "

Comments.
"//................... "

- Do not affect the operation of the program; Ignored by


the compiler
- Enhance the readability of the source code

Introduction to C++ Programming – p. 12/2


Line-by-Line Analysis (Preprocessor)

Line 3.
" # include <iostream> "
—— Preprocessor directive.

Introduction to C++ Programming – p. 13/2


Line-by-Line Analysis (Preprocessor)

Line 3.
" # include <iostream> "
—— Preprocessor directive.

Question: What does the preprocessor do here?

Introduction to C++ Programming – p. 13/2


Line-by-Line Analysis (Preprocessor)

hello.cpp hello.cpp
iostream.h

# include <iostream>

Source Code
Source Code
B B

Source code Preprocessor


A

Source Code
A

(a) (b)

Introduction to C++ Programming – p. 14/2


Line-by-Line Analysis ("main" function)

Line 4. main function

Definition

function header int main ( )


openning brace {
………..
function body

………..
closing brace }

Introduction to C++ Programming – p. 15/2


Line-by-Line Analysis ("main" function)

main function.

• Every C++ program has a main() function.

• The function main() is called by operating system


automatically when program starts.

• The function main() always return int, e.g. 0 (signal for


success).

Introduction to C++ Programming – p. 16/2


Line-by-Line Analysis (Output statement)

Line 6.
" std::cout « "Hello World!\n"; "

Introduction to C++ Programming – p. 17/2


Line-by-Line Analysis (Output statement)

Line 6.
" std::cout « "Hello World!\n"; "

—— Output statement.

• cout — output stream object.

• std — namespace.
std::— use the standard input/output library.
• white space — blank lines, space characters and tab
characters.

Introduction to C++ Programming – p. 17/2


Line-by-Line Analysis (Output statement)

"\n" — tell cout to output a new line.


Compare the following statements:

1 std::cout « "Hello World!\n";


std::cout « "Hello World!";

2 std::cout « "Hello World!Hello World!";

3 std::cout « "Hello World!";


std::cout « "Hello World!";

Introduction to C++ Programming – p. 18/2


Line-by-Line Analysis (Remark)

Remark.

• Every C++ statement must be ended with a semicolon


;.

• main function must be ended with return 0;

Introduction to C++ Programming – p. 19/2


Lab and Exercise

Introduction to C++ Programming – p. 20/2


In-Class Exercises:

Exercise 1. Write a program that prints:

***********
*********
*******
*****
***
*

Introduction to C++ Programming – p. 21/2


In-Class Exercises:

Exercise 2. Write a program that prints:

*
* *
* * * *
* *
* *
*

Introduction to C++ Programming – p. 22/2


Homework:
• Read Sec. 2.1-2.3
• Exercises 2.21, 2.22, 2.26

Introduction to C++ Programming – p. 23/2

You might also like