You are on page 1of 15

Programming Fundamentals

Lecture 4

Copyright © 2013 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1
Introduction to C++

• A well-designed program

Copyright © 2013 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2
Functions

• In C++, modules can be


classes or functions. It helps to
think of a function as a small
machine that transforms the
data it receives into a finished
product.
The names allowed for functions
are also used to name other
elements of the C++ language and
are collectively referred to as
identifiers
3
• The C++ identifier is a name used to
identify a variable, function, class,
module, or any other user-defined item.
• Identifiers can be made up of any
combination of letters, digits, or
Identifiers underscores (_) selected according to the
following rules.
Rules for identifiers
• must begin with letter or the
underscore _
• Only letters, digits, or underscores can
follow the first letter (blank spaces
aren’t allowed to separate words in a
function name)
• recommend meaningful identifiers
4
Limitations on identifiers

• A function name can’t be one


of the keywords listed in
Table 2.1. (A keyword is a
word the language sets aside
for a special purpose and can
be used only in a specified
manner.1)
• • The maximum number of
characters in a function name
is 1024.2

Copyright © 2013 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5
Examples of identifiers

• 1AB3
(Begins with a number, which violates rule 1.)

• E*6
(Contains a special character, which violates rule 2.)

• for
(Consists of a keyword, which violates rule 3.)

6
Examples of identifiers

Copyright © 2013 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 7
Function names

• Function names can also consist of mixed uppercase and lowercase


letters, as in
• degToRad().

• Note :
• Function names end with a parenthesis.

• C++ is a case-sensitive language.


Example of three different identifiers :
TOTAL, total, and TotaL

8
The main() Function

• Each C++ program must


have one, and only one,
function named main().
• The main() function is
referred to as a driver
function because it tells
other functions the
sequence in which they
execute.

9
The main() Function

Copyright © 2013 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 10
The cout Object

• Commonly used C++ resources is an object named cout (see-out)


• Sends data to a standard display device (Computer Screen/Console)

11
Preprocessor commands

• The first line of the program is a preprocessor command that uses the
reserved word include:

• #include <iostream>
• Begin with a pound sign (#) and perform some action before the
compiler translates the source program into machine code.

• istream and ostream.


• These two classes provide data declarations and methods for data
input and output.
*Note: Preprocessor commands don’t end with a semi-colon.
12
using namespace std;
namespace • You can think of a namespace as a
section of source code the compiler
accesses when it’s looking for
prewritten classes or functions.
• Because the iostream header file is
contained in a namespace named std
(for “standard library”).

13
Example

Output ?

Why \n not appeared?


Copyright © 2013 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 14
Comments

1. Line Comments (//):


A line comment begins with two
slashes (//) and continues to the end
of the line. For example, the following
examples are line comments:

2. Block Comment (/*…*/)

15

You might also like