48
Introduction to C++ Budditha Hettige Department of Computer Science IT 1033: Fundamentals of Programming 2

Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige ([email protected]) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

  • Upload
    phamdan

  • View
    223

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Introduction to C++

Budditha Hettige

Department of Computer Science

IT 1033: Fundamentals of Programming

2

Page 2: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

C++

• C++ is a middle-level programming language

• Developed by Bjarne Stroustrup

• Starting in 1979 at Bell Labs

• C++ runs on a variety of platforms

– Windows,

– Mac OS,

– various versions of UNIX

• Designed with a bias toward system programming

and embedded

7/11/2015 Budditha Hettige ([email protected]) 2

Page 3: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

C++ A Programming Language

• Artificial Language

• General-purpose programming language

• Features

– Object-oriented & generic programming features

– Low-level memory manipulation

• Consists of

– Key words

– Syntax

– Semantics

[email protected] 3

Page 4: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

C++ Vs Natural Languages

• C++

– Artificial Language

– Consist of

• Keywords

• Syntax

• Semantics

– Translate through

the Compilers

[email protected] 4

• Natural Language

– Natural

– Consist of• Words

• Syntax

• Semantics

– Translate trough the Machine Translation systems or Human

Page 5: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Key words/ words

• C++

[email protected] 5

• Natural Language

Page 6: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Syntax

• C++ Rules for construction of valid

statements, including, Order of

words, Punctuation

[email protected] 6

• Natural LanguageGrammar rules, subject, object,

verbs etc.

Page 7: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Semantics

• C++ The set of rules that determines

the meaning of instructions (what

the computer will do) written in a

programming language.

[email protected] 7

• Natural LanguageIs the study of meaning

Page 8: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Minimum C++ Program

• Do nothing

[email protected] 8

Page 9: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

C++ Block

• A block is a set of logically connected statements

that are surrounded by opening and closing braces.

[email protected] 9

Page 10: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Blocks

[email protected] 10

C

A

B

Page 11: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

C++ semicolon

• The semicolon is a statement terminator. That is,

each individual statement must be ended with a

semicolon.

[email protected] 11

Page 12: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Example

• Create a C++ program to print message on console

window

[email protected] 12

Page 13: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Example

• This is a comment line.

• All lines beginning with two slash signs (//) or

(/*) are considered comments and do not have

any effect on the behavior of the program.

• // Single line comment

• /* */ block comment

• Block comments cannot be nested

[email protected] 13

Page 14: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Example

• Lines beginning with a hash sign (#) are directives for the preprocessor.

• They are not regular code lines with expressions but indications for the compiler's preprocessor.

• In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file.

• This specific file (iostream) includes the declarations of

the basic standard input-output library in C++, and it is

included because its functionality is going to be used

later in the program.

[email protected] 14

Page 15: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

What is preprocessor.• Is a program that processes its input data to

produce output that is used as input to another

program.

• The preprocessor provides the ability for the

inclusion of header files.

• Is a separate program invoked by the compiler

as the first part of translation

• Example:

[email protected] 15

Page 16: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Preprocessor in a Program

7/11/2015 Budditha Hettige ([email protected]) 16

Page 17: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Example

• All the elements of the standard C++ library are

declared within what is called a namespace, the

namespace with the name std

• Use Only for GCC compilers

[email protected] 17

Page 18: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Example

• Beginning of the definition of the main function.

• The main function is the point by where all C++

programs start their execution, independently of its

location within the source code.

• All C++ programs have a main function

[email protected] 18

Page 19: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Example

• Is a C++ statement.

• This statement performs the only action that

generates a visible effect in our first program.

• Command

[email protected] 19

Page 20: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Example

• The return statement causes the main function to

finish.

[email protected] 20

Page 21: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Cording, Editing, compiling and run a

program

• Create new Code:blocks project

• Add following code

• Compile and run

7/11/2015 Budditha Hettige ([email protected]) 21

Page 22: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Code::Blocks

[email protected] 22

Output

Source CodeProject

Source file

Code::Blocks IDE

Page 23: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Example 2

• Create a C++ program to display your name and

address

[email protected] 23

Page 24: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Cording Styles

7/11/2015 Budditha Hettige ([email protected]) 24

Page 25: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

New Line

[email protected] 25

Page 26: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Example 2

• What is output of the following program

[email protected] 26

Output

Page 27: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Escape sequences

• Escape sequences are used to represent certain special

characters within string literals (“ ”)

[email protected] 27

Escape sequence Description

\' single quote

\" double quote

\? question mark

\\ backslash

\a audible bell

\b backspace

\f form feed - new page

\n line feed - new line

\r carriage return

\t horizontal tab

Page 28: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

ASCII art with C++ASCII art is a graphic design technique that uses computers

for presentation and consists of pictures pieced together

from the 95 printable (from a total of 128) characters defined

by the ASCII Standard from

28

cout<<" @ @ @ \n“;

cout<<" @ @ @ @ \n“;

cout<<" @ @ @ \n“;

cout<<" \\|/ \n“;

cout<<" | | | \n“;

cout<<" | | | \n“;

cout<<" | | | \n“;

cout<<" | | | \n“;

cout<<" ( ) \n“;

cout<<" ^^^^^ \n“;

[email protected]

Page 29: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Example

• Write a C++ program to display your name using

Ascii Art

7/11/2015 Budditha Hettige ([email protected]) 29

Page 30: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Exercise

1. Write a C++ Program to Display the Following output

30

-------------------------------

C1033

Fundamentals of Programming

-------------------------------

[email protected]

Page 31: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Different ways to create a

C++ program

7/11/2015 Budditha Hettige ([email protected]) 31

Page 32: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Different ways to create a

C++program

7/11/2015 Budditha Hettige ([email protected]) 32

Page 33: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Different ways to create a

C++program

7/11/2015 Budditha Hettige ([email protected]) 33

Page 34: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Different ways to create a

C++program

7/11/2015 Budditha Hettige ([email protected]) 34

Page 35: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Template for a C++ Program

7/11/2015 Budditha Hettige ([email protected]) 35

Page 36: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

C++ Programming

36

C++ Source File(.cpp)

Executable program(.exe)

C++ Compiler

Operating system can

directly execute

[email protected]

Page 37: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Preparing a C++ program for

running

[email protected] 37

Page 38: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Compiling

[email protected] 38

Page 39: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Compile errors

Page 40: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Compilation Errors

• Compiler fails to compile a piece of computer

program source code.

• Error message is given

[email protected] 40

Page 41: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Common C++ compilation errors

• Undeclared identifier

• Common function undeclared

• = expected

• Internal compiler error

• Unexpected closing brace

[email protected] 41

Page 42: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Example

• Write the following C++ program and identify

Compilation errors

[email protected] 42

Page 43: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Example

• Correct errors and rewrite the program

[email protected] 43

Page 44: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Clear the console screen

7/11/2015 Budditha Hettige ([email protected]) 44

Command

Header

Page 45: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Change console Text and background

color

• Sets the default console foreground and background

colours.

• Syntax

– COLOR [background][foreground]

– system("Color FA");

– system("Color F0");

7/11/2015 Budditha Hettige ([email protected]) 45

Page 46: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Color Code

• 0 = Black

• 1 = Blue

• 2 = Green

• 3 = Aqua

• 4 = Red

• 5 = Purple

• 6 = Yellow

• 7 = White

7/11/2015 Budditha Hettige ([email protected]) 46

• 8 = Gray

• 9 = Light Blue

• A = Light Green

• B = Light Aqua

• C = Light Red

• D = Light Purple

• E = Light Yellow

• F = Bright White

Page 47: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Example

• Write a C++ program to display following screen

7/11/2015 Budditha Hettige ([email protected]) 47

USER INFORMATION

-------------------------------

NAME : B. HETTIGE

ADDRESS: No23, Panadura

AGE : 19

SALARY : 23500

GENDER : M

-------------------------------

Page 48: Introduction to C++ - · • Write a C++ program to display following screen 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47 USER INFORMATION-----NAME : B. HETTIGE ADDRESS: No23,

Summary

• C++ Programming Language?

• C++ Vs Natural Languages

• C++ Syntax

• Create a C++ program using code:blocks

• Cording styles

• ASCII Art

• Handle compile errors

• Customize Console screen

[email protected] 48