You are on page 1of 9

Outline

CHAPTER 2
Introduction to the C++ Language
MNAZ / Department of Computer Science, KICT / Sem 1 2008/2009

Objectives
2.1 Background 2.2 C++ Programs 2.3 Identifiers 2.4 Data Types 2.5 Variables 2.6 Constant 2.7 Coding Constant 2.8 Reading and Writing Data 2.9 Programming Examples
7/14/2008 2

OBJECTIVES After studying this chapter you will be able to:


q Identify the basic components of a C++ function. q Identify and use the standard C++ data types. q Identify and use the four kinds of C++ constants. q Differentiate between literal, as defined, and memory constants. q Define and use variables. q Read data from the keyboard and output data to the console. q Add comments to a program as a form of inline documentation. q Create intelligent names to make programs easier to read and understand.

2.1

Background

7/14/2008

7/14/2008

Figure 2-1

Taxonomy of the C++ language

2.2

C++ Programs

7/14/2008

7/14/2008

Figure 2-2

Structure of a C++ program

Figure 2-3

The greeting program

7/14/2008

7/14/2008

Figure 2-4

Examples of comments

Figure 2-5

Nested block comments are invalid

7/14/2008

7/14/2008

10

2.3

2.4

Identifiers
The first character must be alphabetic character or underscore Must consists only alphabetical characters, digits or underscore The identifier cannot duplicate a reserved words.

Data Types

7/14/2008

11

7/14/2008

12

Figure 2-6

Standard data types

Figure 2-7

Integer types

7/14/2008

13

7/14/2008

14

Figure 2-8

Floating-point types

Note:
A character in C++ can be interpreted as a small integer (between 0 and 255). For this reason, C++ often treats a character like an integer.

7/14/2008

15

7/14/2008

16

2.5
Note:
In C++ the Boolean constants are true and false. Additionally, following traditional standards, any nonzero number is considered true, and zero is considered false.

Variables

7/14/2008

17

7/14/2008

18

Figure 2-9

Variables in memory

Note: When a variable is defined, it is not initialized. The programmer must initialize any variable requiring prescribed data when the function starts.

7/14/2008

19

7/14/2008

20

Program 2-2: Print the sum of three numbers


/* This program calculates and prints the sum of three numbers input by the user at the keyboard. Written by: Date: */ #include <iostream> using namespace std; int main () { int a; int b; int c; int sum;
7/14/2008 21

cout << "Welcome. This program adds\n"; cout << "three numbers. Enter three numbers\n"; cout << "in the form: nnn nnn nnn <return>\n"; cin >> a >> b >> c; // Numbers are now in a, b, and c. Add them. sum = a + b + c; cout << "\nThe total is: " << sum << "\n"; cout << "\nThank you. Have a good day.\n"; return 0; } // main
7/14/2008 22

2.6
Note: A character constant is enclosed in single quotes.

Constants

7/14/2008

23

7/14/2008

24

Note: Use single quotes for character constants. Use double quotes for string constants.

Note: The only bool types constants are true, printed as 1, and false, printed as 0.

7/14/2008

25

7/14/2008

26

Figure 2-10

Some strings

Figure 2-11

Null characters and null strings

"" "h" "Hello World!\n" "HOW ARE YOU?" "Good Morning!" "'Good' Morning!" "\"Good\" Morning!"

// A null string

\0

-> Null character -> Empty string

// 'Good' Morning // "Good" Morning

7/14/2008

27

7/14/2008

28

2.7

Program 2.3: Memory Constants


/* This program demonstrates three ways to use constants. Written by: Somebody Date: XX/XX/XXXX */ #include <iostream> using namespace std; #define PI 3.1415926536 int main () { const double pi = 3.1415926536; cout << "Defined constant PI: " <<PI<<"\n"; cout << "Memory constant pi: " <<pi<<"\n"; cout << "Literal constant: " <<3.1415926536<<"\n"; system("PAUSE"); return 0; } // main

Coding Constants

7/14/2008

29

7/14/2008

30

2.8

Figure 2-12

Standard streams

Reading and Writing Data


7/14/2008 31 7/14/2008 32

Program 2.4: Printing different types with cout


/* This program demonstrates the use of the insertion operator with several different types Written by: Date:

2.9

*/ #include <iostream> using namespace std; int main () { // Statements cout << 24 << " -> This is an integer\n"; // Print an integer cout << 12.3 << " -> This is a float\n"; // Print a float cout << 'A' << " -> This is a character\n"; // Print a character cout << "Hello World!" << " ->This is a string\n"; // Print a string system("PAUSE"); return 0; } // main
7/14/2008 33 7/14/2008

Programming Examples
Page 60 - 64
34

You might also like