fop-lec2a

17
Fundamentals of Programming (FOP) Instructor: Komal Nain Sukhia Military College of Signals, Rawalpindi Lecture 2A Writing Simple C++ Programs

Upload: zohairadnan

Post on 04-Dec-2015

214 views

Category:

Documents


1 download

DESCRIPTION

This is amazing

TRANSCRIPT

Page 1: FOP-Lec2A

Fundamentals of Programming (FOP)

Instructor: Komal Nain Sukhia

Military College of Signals, Rawalpindi

Lecture 2A

Writing Simple C++ Programs

Page 2: FOP-Lec2A

2

A Simple Program:Printing a Line of Text

• Comments– Document programs– Improve program readability– Ignored by compiler– Single-line comment

• Begin with //– Multiple-line comment

• Everything between /* and */ is ignored.

• Preprocessor directives– Processed by preprocessor before compiling– Begin with #

• # include• # define

`

Page 3: FOP-Lec2A

3

C++ Preprocessor

• C++ Compilers automatically invoke a preprocessor that takes care of #include statements and some other special directives.

• You don't need to do anything special to run the preprocessor - it happens automatically.

Page 4: FOP-Lec2A

4

The Preprocessor

• Lines that start with the character '#' are special instructions to a preprocessor.

• The preprocessor can replace the line with something else:– include: replaced with contents of a file

• Other directives tell the preprocessor to look for patterns in the program and do some fancy processing.

Page 5: FOP-Lec2A

5

Preprocessing

C++ ProgramC++ Program ExecutableProgram

ExecutableProgram

C++Compiler

C++Compiler

C++Preprocessor

C++Preprocessor

Temporary file(C++ program)

Temporary file(C++ program)

Page 6: FOP-Lec2A

1 // code1.cpp2 // A first program in C++.3 #include <iostream>4 5 // function main begins program execution6 int main()7 {8 std::cout << "Welcome to C++!\n";9 10 return 0; // indicate that program ended successfully11 12 } // end function main

Welcome to C++!

Single-line comments.

Preprocessor directive to include input/output stream header file <iostream>.

Function main appears exactly once in every C++ program..

Function main returns an integer value.Left brace { begins function body.

Corresponding right brace } ends function body.

Statements end with a semicolon ;.

Name cout belongs to namespace std.

Stream insertion operator.

Keyword return is one of several means to exit function; value 0 indicates program terminated successfully.

Page 7: FOP-Lec2A

7

A Simple Program:Printing a Line of Text

• Standard output stream object– std::cout– “Connected” to screen– <<

• Stream insertion operator

• Value to right (right operand) inserted into output stream

• Namespace– std:: specifies that cout belongs to “namespace” std– std:: removed through use of using statements

using namespace std;

• Escape character and escape sequences– \ called the “Escape Character”

– Indicates “special” character output

Page 8: FOP-Lec2A

8

A Simple Program:Printing a Line of Text

Escape Sequence Description

\n Newline. Position the screen cursor to the beginning of the next line.

\t Horizontal tab. Move the screen cursor to the next tab stop.

\r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line.

\a Alert. Sound the system bell.

\\ Backslash. Used to print a backslash character.

\" Double quote. Used to print a double quote character.

Page 9: FOP-Lec2A

91 // code2.cpp2 // Printing a line with multiple statements.3 #include <iostream>4 5 // function main begins program execution6 int main()7 {8 std::cout << "Welcome "; 9 std::cout << "to C++!\n";10 11 return 0; // indicate that program ended successfully12 13 } // end function main

Welcome to C++!

Multiple stream insertion statements produce one line of output.

Page 10: FOP-Lec2A

101 // code3.cpp2 // Printing multiple lines with a single statement3 #include <iostream>4 5 // function main begins program execution6 int main()7 {8 std::cout << "Welcome\nto\n\nC++!\n";9 10 return 0; // indicate that program ended successfully11 12 } // end function main

Welcome

to

 

C++!

Using newline characters to print on multiple lines.

Page 11: FOP-Lec2A

11

Another Simple Program:Adding Two Integers

• Variables – Location in memory where value can be stored

– Fundamental data types• int - integer numbers (whole numbers + and -)• char - characters• float - floating point numbers

– Declare variables with name and data type before useint integer1;

int integer2;

int sum;

– Can declare several variables of same type in one declaration• Comma-separated list

int integer1, integer2, sum;

Page 12: FOP-Lec2A

12

Another Simple Program:Adding Two Integers

• Variables– Variable names

• Valid identifier

– Series of characters (letters, digits, underscores)

– Cannot begin with digit

– Case sensitive

Page 13: FOP-Lec2A

13

Another Simple Program:Adding Two Integers

• Input stream object– >> (stream extraction operator)

• Used with std::cin• Waits for user to input value, then press Enter (Return) key

• Stores value in variable to right of operator

– Converts value to variable data type

• = (assignment operator)– Assigns value to variable

– Binary operator (works on two operands)

– Example:sum = variable1 + variable2;– Operands : sum, value of the expression variable1+variable2

Page 14: FOP-Lec2A

1 // code4.cpp2 // Addition program.3 #include <iostream>4 5 // function main begins program execution6 int main()7 {8 int integer1; // first number to be input by user 9 int integer2; // second number to be input by user 10 int sum; // variable in which sum will be stored11 12 std::cout << "Enter first integer\n"; // prompt13 std::cin >> integer1; // read an integer14 15 std::cout << "Enter second integer\n"; // prompt16 std::cin >> integer2; // read an integer17 18 sum = integer1 + integer2; // assign result to sum19 20 std::cout << "Sum is " << sum << std::endl; // print sum21 22 return 0; // indicate that program ended successfully23 24 } // end function main

Declare integer variables.

Use stream extraction operator with standard input stream to obtain user input.

Concatenating, chaining or cascading stream insertion operations.

Calculations can be performed in output statements: alternative for lines 18 and 20:

std::cout << "Sum is " << integer1 + integer2 << std::endl;

Stream manipulator std::endl outputs a newline

Page 15: FOP-Lec2A

15Enter first integer

45

Enter second integer

72

Sum is 117

Page 16: FOP-Lec2A

16

Memory Concepts

std::cin >> integer1;– Assume user entered 45

std::cin >> integer2;– Assume user entered 72

sum = integer1 + integer2;

integer1 45

integer1 45

integer2 72

integer1 45

integer2 72

sum 117

Page 17: FOP-Lec2A

Study Material

• Text Book : Chapter 1 • http://www.cplusplus.com/reference/library/manip

ulators/

17