Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

70
Chapter 2: Introduction to C++

Transcript of Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Page 1: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Chapter 2:

Introduction to C++

Page 2: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Outline

• Basic “Hello World!!”• Variables• Data Types• Illustration

Page 3: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

The main function

int main ( ) heading{

statements body

return 0;}

All C++ programs must have a function named main

There are other things need to be specified before main.

The body of the main program, must be inside the braces

Page 4: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Function heading

int main ( ){

}

type of returned value name of function says no

parameters

Page 5: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 5

Parts of a C++ Program

// sample C++ program#include <iostream>using namespace std;int main() {

cout << "Hello, there!";return 0;

}

commentpreprocessor

directive

which namespaceto use

beginning offunction named main

beginning of block for main

output statement

stringliteral

send 0 tooperating system

end of blockfor main

Page 6: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 9

The cout Object

• Displays output on the computer screen

• You use the stream insertion operator << to send output to cout:

cout << "Programming is fun!";

Page 7: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 10

The cout Object

• Can be used to send more than one item to cout:

cout << "Hello " << "there!";Or:

cout << "Hello ";cout << "there!";

Page 8: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 11

The cout Object

• cout << "Programming is ";cout << "fun!";

• This produces one line of output

Page 9: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

The cout statements

SYNTAX

These examples yield the same output:

cout << “The answer is “ ;cout << 3 * 4 ;

cout << “The answer is “ << 3 * 4 ;

cout << Expression << Expression . . . ;

Page 10: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 13

The endl Manipulator

Programming isfun!

cout << "Programming is" << endl;cout << "fun!";

You can use the endl manipulator to start a new line of output. This will produce two lines of output:

a lowercase L

Page 11: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 15

The \n Escape Sequence• You can also use the \n escape sequence to start a new line

of output. This will produce two lines of output:

cout << "Programming is\n";cout << "fun!";

Notice that the \n is INSIDE the string.Programming is

fun!

Page 12: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Example 1

• What is the output from the next program?

#include <iostream>using namespace std;

int main( ){ cout << "Computers, computers everywhere\n as far as\n\nI can

C"; return 0;}

Page 13: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Example 1- Solution

Computers, computers everywhere as far as

I can C

Page 14: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Example 2

• What is the output from the next program?

#include <iostream>using namespace std;int main( ){ cout << "Computers, computers everywhere" << endl; cout << " as far as" << endl << endl; cout << "I can C"; return 0;}

Same as the results of the previous slide!

Page 15: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

The cout object

• Other escape sequences: \t for tab; \a for alarm; \b for backspace; \r for return to the beginning of the current line; \\ for backslash; \’ for single quotation mark; \” for double quotation mark

Page 16: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 21

Comments

• Used to document parts of the program• Explanatory remarks made within the program• Intended for persons reading the source code

of the program:– Indicate the purpose of the program– Describe the use of variables– Explain complex sections of code

• Are ignored by the compiler

Page 17: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 22

Single-Line Comments

Begin with // through to the end of line:int length = 12; // length in inchesint width = 15; // width in inchesint area; // calculated area

// calculate rectangle areaarea = length * width;

Page 18: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 23

Multi-Line Comments

• Begin with /*, end with */• Can span multiple lines:

/* this is a multi-line comment*/

• Can begin and end on the same line:int area; /* calculated area */

Page 19: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 24

Programming Style

• The visual organization of the source code• Includes the use of spaces, tabs, and blank

lines• Does not affect the syntax of the program• Affects the readability of the source code

Page 20: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 26

Except for strings, double quotes, identifiers, and keywords, C++ ignores all whitespace (whitespace is any combination of one or more blank spaces, tabs, or new lines). For example, this is a valid program:

// Hello World Program#include <iostream>int main( ){ cout << "Welcome to UCA!"; return 0;}

Page 21: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 27

Although the previous version of the program works, it is an example of extremely poor programming style. It is difficult to read and understand. The main( ) function should always be written in the standard form below:

// Hello World Program#include <iostream>using namespace std;

int main( ) { cout<<“Hello World!”; return 0; }

Page 22: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Outline

• Basic “Hello World!!”• Variables• Data Types• Illustration

Page 23: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 30

Variable Initialization

• To initialize a variable means to assign it a value when it is declared:

int length = 12;

• Can initialize some or all variables:int length = 12, width = 5, area;

Not initialized

Page 24: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 31

Variable Assignments and Initialization

• An assignment statement uses the = operator to store a value in a variable.

item = 12;

• This statement assigns the value 12 to the item variable.

Page 25: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Assign a value to a variableYou can assign (give) a value to a variable by using the assignment operator =

VARIABLE DECLARATIONS

string firstName ;char middleInitial ;char letter ;int ageOfDog;

VALID ASSIGNMENT STATEMENTS

firstName = “Fido” ;middleInitial = ‘X’ ;letter = middleInitial ;ageOfDog = 12 ;

Page 26: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 33

Assignment

• The variable receiving the value must appear on the left side of the = operator.

• This will NOT work:

// ERROR! 12 = item;

Page 27: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 34

Variables and Literals

• Variable: a storage location in memory– Syntax: data-type variable-name;

– Has a name and a type of data it can hold– Must be declared before it can be used:

int item;

Page 28: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 35

Variable Declaration

Page 29: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 36

Variable Declaration

Page 30: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 37

Literals

• Literal: a value that is written into a program’s code.

"hello, there" (string literal)12 (integer literal)

Page 31: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 38

20 is an integer literal

Page 32: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 39

This is a string literal

Page 33: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 40

This is also a string literal

Page 34: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

What is an identifier?

• An identifier is a programmer-defined name for some part of a program: variables, functions, etc.

• A programmer chooses identifiers as long as the rules are followed, however, using meaningful (mnemonic) identifiers is a good programming practice

• C++ is a case-sensitive language, lower case and upper case are different

Page 35: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 42

C++ Key Words

You cannot use any of the C++ key words as an identifier. These words have reserved meaning.

Page 36: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 43

C++ Key Words

You cannot use any of the C++ key words as an identifier. These words have reserved meaning.

Page 37: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 44

Variable Names

• A variable name should represent the purpose of the variable. For example:

items_ordered

The purpose of this variable is to hold the number of items ordered.

Page 38: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 45

Identifier Rules

• The first character of an identifier must be an alphabetic character or an underscore ( _ ),

• After the first character you may use alphabetic characters, numbers, or underscore characters, no special characters are allowed ( !, @, #, $, %, &, *, etc.), or commas.

• Upper- and lowercase characters are distinct (C++ is a case sensitive language).

Page 39: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 46

Valid and Invalid Identifiers – Try?

IDENTIFIER VALID? REASON IF INVALID

totalSales

total_Sales

total.Sales

4thQtrSales

totalSale$

Page 40: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 47

Valid and Invalid Identifiers

IDENTIFIER VALID? REASON IF INVALID

totalSales Yes

total_Sales Yes

total.Sales No Cannot contain .

4thQtrSales No Cannot begin with digit

totalSale$ No Cannot contain $

Page 41: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Components and Order of statements in a program #include <iostream>using namespace std;int main (){

// variable declarations..// statementsstatement 1;statement 2;..statement n;

return 0;}

where we define names and types of data to be manipulated by our program

instructions for computer operation

Page 42: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Outline

• Basic “Hello World!!”• Variables• Data Types

• int• char & string• float • bool

• Illustration

Page 43: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 50

Integer Data Types• Integer variables can hold whole numbers such as 12, 7, and -

99.

Page 44: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 51

Declaring Variables

• Variables of the same type can be declared- On separate lines:int length;int width;unsigned int area;

- On the same line:int length, width;unsigned int area;

• Variables of different types must be in different declarations

Page 45: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Declaring a variable

A declaration tells the compiler to allocate enough memory to hold a value of this data type, and to associate the identifier with this location.

int ageOfDog;float taxRate;char middleInitial;

4 bytes for ageOfDog

ageOfDog

Page 46: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 57

The char Data Type

• Used to hold characters or very small integer values• Usually 1 byte of memory• Character literals must be enclosed in single quote

marks. Example: 'A'• Numeric value of character from the character set is

stored in memory:

CODE:char letter;letter = 'C';

MEMORY:letter

67

Page 47: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

ASCII Code

Page 48: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 59

What is the output?

Page 49: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
Page 50: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 61

Character Strings

• A series of characters in consecutive memory locations:

string greeting;greeting = “Hello”;

• Stored with the null terminator, \0, at the end:

• Comprised of the characters between the “”

H e l l o \0

Page 51: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
Page 52: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 63

Floating-Point Data Types

• The floating-point data types are:floatdoublelong double

• They can hold real numbers such as:12.45 -3.8

• Stored in a form similar to scientific notation• All floating-point numbers are signed

Page 53: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Represents values that are true or false bool variables are stored as small integers false is represented by 0, true by 1:

bool allDone = true;

bool finished = false;

The bool Data Type

allDone

1 0

finished

Page 54: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
Page 55: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
Page 56: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Outline

• Basic “Hello World!!”• Variables• Data Types• Illustration

Page 57: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

#include <iostream>using namespace std;int main (){

int length1, width1 =2, area1, length2, width2 = 3, area2;length1 = 10;length2 = length1;length1 = 5 + 3;length1 = length2;area1 = length1 * width1;area2 = area1 + 10;area2 = area2 + 10;return 0;

}

Test Program Illustrating Assignment

length1

width1

area1

length2

width2

area2

Page 58: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

#include <iostream>using namespace std;int main (){

int length1, width1 =2, area1, length2, width2 = 3, area2;length1 = 10;length2 = length1;length1 = 5 + 3;length1 = length2;area1 = length1 * width1;area2 = area1 + 10;area2 = area2 + 10;return 0;

}

Test Program Illustrating Assignment

length1

width1

area1

length2

width2

area2

2

3

Page 59: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

#include <iostream>using namespace std;int main (){

int length1, width1 =2, area1, length2, width2 = 3, area2;length1 = 10;length2 = length1;length1 = 5 + 3;length1 = length2;area1 = length1 * width1;area2 = area1 + 10;area2 = area2 + 10;return 0;

}

Test Program Illustrating Assignment

length1

width1

area1

length2

width2

area2

10

2

3

Page 60: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

#include <iostream>using namespace std;int main (){

int length1, width1 =2, area1, length2, width2 = 3, area2;length1 = 10;length2 = length1;length1 = 5 + 3;length1 = length2;area1 = length1 * width1;area2 = area1 + 10;area2 = area2 + 10;return 0;

}

Test Program Illustrating Assignment

length1

width1

area1

length2

width2

area2

10

2

10

3

Page 61: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

#include <iostream>using namespace std;int main (){

int length1, width1 =2, area1, length2, width2 = 3, area2;length1 = 10;length2 = length1;length1 = 5 + 3;length1 = length2;area1 = length1 * width1;area2 = area1 + 10;area2 = area2 + 10;return 0;

}

Test Program Illustrating Assignment

length1

width1

area1

length2

width2

area2

8

2

10

3

Page 62: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

#include <iostream>using namespace std;int main (){

int length1, width1 =2, area1, length2, width2 = 3, area2;length1 = 10;length2 = length1;length1 = 5 + 3;length1 = length2;area1 = length1 * width1;area2 = area1 + 10;area2 = area2 + 10;return 0;

}

Test Program Illustrating Assignment

length1

width1

area1

length2

width2

area2

10

2

10

3

Page 63: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

#include <iostream>using namespace std;int main (){

int length1, width1 =2, area1, length2, width2 = 3, area2;length1 = 10;length2 = length1;length1 = 5 + 3;length1 = length2;area1 = length1 * width1;area2 = area1 + 10;area2 = area2 + 10;return 0;

}

Test Program Illustrating Assignment

length1

width1

area1

length2

width2

area2

10

2

20

10

3

Page 64: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

#include <iostream>using namespace std;int main (){

int length1, width1 =2, area1, length2, width2 = 3, area2;length1 = 10;length2 = length1;length1 = 5 + 3;length1 = length2;area1 = length1 * width1;area2 = area1 + 10;area2 = area2 + 10;return 0;

}

Test Program Illustrating Assignment

length1

width1

area1

length2

width2

area2

10

2

20

10

3

30

Page 65: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

#include <iostream>using namespace std;int main (){

int length1, width1 =2, area1, length2, width2 = 3, area2;length1 = 10;length2 = length1;length1 = 5 + 3;length1 = length2;area1 = length1 * width1;area2 = area1 + 10;area2 = area2 + 10;return 0;

}

Test Program Illustrating Assignment

length1

width1

area1

length2

width2

area2

10

2

20

10

3

40

Page 66: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 80

Binary Arithmetic Operators

SYMBOL OPERATION EXAMPLE VALUE OF 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

Page 67: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 81

A Closer Look at the / Operator

• / (division) operator performs integer division if both operands are integerscout << 13 / 5; // displays 2cout << 91 / 7; // displays 13

• If either operand is floating point, the result is floating pointcout << 13 / 5.0; // displays 2.6cout << 91.0 / 7; // displays 13.0

Page 68: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

Slide 2- 82

A Closer Look at the % Operator

• % (modulus) operator computes the remainder resulting from integer divisioncout << 13 % 5; // displays 3

• % requires integers for both operandscout << 13 % 5.0; // error

Page 69: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.

What’s the output?

Page 70: Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.