• No results found

3-CompleteCPlusPlusProgram.pdf

N/A
N/A
Protected

Academic year: 2020

Share "3-CompleteCPlusPlusProgram.pdf"

Copied!
17
0
0

Loading.... (view fulltext now)

Full text

(1)

STRUCTURED

PROGRAMMING

(2)

Content

2

Main Function

Preprocessor directives

User comments

Escape characters

cout

statement

String literals

C++ keywords

(3)

Objectives

3

By the end you should be able to:

Identify the

main

function, Preprocessor directives, and

user comments in a C++ program

Recognize some of the C++ keywords

Apply C++ syntax rules in producing output messages

using

cout

stream and

iostream

library

(4)

Complete Program

4

1 // Fig. 2.1: fig02_01.cpp

2 // Text-printing program.

3 #include <iostream> // allows program to output data to the screen

4

5 // function main begins program execution

6 int main()

7 {

8 std::cout << "Welcome to C++!\n"; // display message

9

10 return 0; // indicate that program ended successfully

11

12 } // end function main

Welcome to C++!

comments statement main function preprocessor directive braces string literal

(5)

C++ Program Structure

Comments: remarks that are ignored by the compiler

Compiler directives: commands for compiler, which are needed to

compile and run program effectively

Main function: where every program begins

Braces: mark the beginning and ending code blocks  Statement: a line of C++ code

(6)

Comments

 Explain programs, their purpose, authors names and future

modification notes to other programmers

 Improve program readability

 Ignored by compiler

 Single-line comment

 Begin with //

 Multi-line comment

 Start with /*

 End with */

6

// Fig. 2.1: fig02_01.cpp // Text-printing program

(7)

Preprocessor Directives

 Instructions to the compiler rather than part of C++ language

 Processed by preprocessor before compilation

 Tells preprocessor to include the input/output stream header file

<iostream>

 Begin with #

 It tells the compiler to put code from iostream into the program

before actually creating the executable

 Forgetting to include the file will result in a compilation error 7

#include <iostream>

(8)

main

Function

 Block of codes carries out a specific task

 Part of every C++ program

 ONLY one function in a program must be main

 Can or can’t be “return” a value

 Returns an integer; once return

 Body is delimited by braces { }

 return statement

 The value 0 indicates the program terminated successfully

8

int main() {

return 0;

(9)

Using

cout

 Namespace

 std::

 Specifies using a name that belongs to “namespace” std  Can be removed through use of using statements

 Standard output stream object

 std::cout

 “Connected” to screen

 Defined in input/output stream header file <iostream>

(10)

Using

cout

(cont.)

 Stream insertion operator <<

 Value to right (right operand) inserted into left operand

 Example

 std::cout << "Hello";

 Inserts the string "Hello" into the standard output then displays to the

screen

 Escape characters

 A character preceded by "\"

 Indicates “special” character output

 Example

 "\n“  Cursor moves to beginning of next line on the screen

(11)

Escape Sequences

11

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.

\' Single quote. Use to print a single quote character.

(12)

Modifying First Program - 1

12

1 // Fig. 2.3: fig02_03.cpp

2 // Printing a line of text with multiple statements.

3 #include <iostream> // allows program to output data to the screen

4

5 // function main begins program execution

6 int main()

7 {

8 std::cout << "Welcome ";

9 std::cout << "to C++!\n";

10

11 return 0; // indicate that program ended successfully

12

13 } // end function main

Welcome to C++!

(13)

Modifying First Program - 2

13

1 // Fig. 2.4: fig02_04.cpp

2 // Printing multiple lines of text with a single statement.

3 #include <iostream> // allows program to output data to the screen

4

5 // function main begins program execution

6 int main()

7 {

8 std::cout << "Welcome\nto\n\nC++!\n";

9

10 return 0; // indicate that program ended successfully

11

12 } // end function main

Welcome to

C++!

(14)

Syntax Rules

14

 Must include <iostream> for cout to work properly

 You must use the namespace std for cout to work properly  C++ is case sensitive

 Make sure you don’t capitalize any of the letters in C++ keywords

 Every statement ends with a statement terminator ( ; )

 except for function header, function braces and preprocessor directives

 String literals must be enclosed in “ ”

 Main function must return a value to the OS

(15)

Exercise - 1

15

Write a program that display your name, ID, and section in

our course. Then, save the source code file as

(16)

Exercise - 2

16

Write a program that print the message of your choice to

the screen. Make the message at least four lines long.

(17)

Included Sections

17

References

Related documents

The STERIS System 1E (SS1E) liquid chemical sterilant processing system can be used to process reusable heat-sensitive devices such as endoscopes and their

• read and write are the only input and output methods provided by the file input and output classes. • Java stream package principle: each class should have a very

• endl is a stream manipulator that issues a newline character and flushes the output buffer.. – cout

Níveis mais elevados da IL-6 encontrados no soro dos pacientes com EAR no início de nosso estudo quando comparados ao grupo controle, confirmam os dados do

In England and Wales this campaign resulted in the 2011 introduction of the domestic violence disclosure scheme (Clare’s Law), a scheme aimed at preventing the

To make programs really useful we have to be able to input and output data in large machine- readable amounts, in particular we have to be able to read and write to files.. Since

- Feasibility of providing fast recharging spots with their own primary storage device in order to avoid network unbalance when power is needed during a fast recharge

• 0-19 points for students who show no active participation, who do not participate in group discussions, are