Basic Structure of C++
Lesson 2
STRUCTURE OF A C++ PROGRAM
Explanation of the components
allows direct access to the standard
library's features (like cout, cin, and
string)
cout (character output) is used to print text to
the console in C++.
endl (end line) moves the cursor to a new line
and flushes the output buffer.
C++ Comments
• Comments are hints
that we add to our
code, making it
easier to read and
understand. In C++,
comments start
with //.
Single Line
Comments
• In C++, a single line comment
starts with a double slash (//)
symbol
Multi-line comments
• In C++, any line between /*
and */ is also a comment.
Using Comments
for Debugging
• Comments are useful for
debugging a code. For
example,
• When you encounter an
error in your program, you
can use comments to
temporarily stop that part of
the code from running.
Using Comments
for Debugging
• This can help you figure out
what's wrong without removing
the code. For example;
Working with
Data
Variables, Constants, and Data Types
After studying this chapter, you will
be able to:
1 2 3 4
Name Declare and Use Use
variables and initialize arithmetic assignment
use variables operators in operators in
appropriate expressions assignment
data types statements
• A data item’s data type is a
classification that describes
the following:
Understandi • What values can be held
ng Data by the item
• How the item is stored in
Types computer memory
• What operations can be
performed on the item
Understanding Data
Types
• All programming languages support two broad data
types:
• Numeric describes data that consists of numbers,
possibly with a decimal point or a sign; numeric
data can be used in arithmetic operations.
• String describes data items that are nonnumeric;
string data cannot be used in arithmetic
operations.
Understanding Data
Types
• Most programming languages support several additional data types,
including multiple types for numeric values that are very large or very
small and for those that do and do not have fractional decimal digits
• Languages like C++, C#, Visual Basic, and Java separate numbers
into two main types:
• Integer variables: These store whole numbers (without decimal
points), such as 5, -12, or 100.
• Floating-point variables: These store numbers with decimal points,
such as 3.14, -0.75, or 100.5.
Understand
ing Data
Types
Understanding Unnamed,
Literal Constants
When you use a specific value in a computer program, it
is one of two types of constants:
• A numeric constant (or literal numeric constant)
is a number that does not change— for example, 43.
When you store a numeric value in computer memory,
additional characters such as dollar signs and commas
typically are not input or stored. Those characters
might be added to output for readability, but they are
not part of the number.
Understanding Unnamed,
Literal Constants
• A string constant (or literal string constant) appears
within quotation marks in computer programs.
• String values are also called alphanumeric values because
they can contain alphabetic characters as well as numbers and
other characters.
• For example, “Amanda”, “51”, and “$3,215.99 U.S.” all are
strings because they are enclosed in quotation marks.
• Although strings can contain numbers, numeric values cannot
contain alphabetic characters.
Working with
Variables
• Variables are named memory locations whose
contents can vary or differ over time.
• For example, in the number-doubling program in
Figure 2-1, myNumber and myAnswer are
variables.
• At any moment in time, a variable holds just one
value.
• Sometimes, myNumber holds 2 and myAnswer
holds 4; at other times, myNumber holds 6 and
myAnswer holds 12.
• The ability of variables to change in value is what
makes computers and programming worthwhile.
• Because one memory location can be used
repeatedly with different values, you can write
program instructions once and then use them for
thousands of separate calculations
Working with
Variables
• In most programming languages, before you can use
any variable, you must include a declaration for it.
• A declaration is a statement that provides these
things for a variable:
•• a data type
•• an identifier
•• optionally, an initial value
Understanding a Declaration’s
Data Type
Every programming language requires that you specify the
correct type for each variable, and that you use each type
appropriately.
• A numeric variable is one that can hold digits and have
mathematical operations performed on it.
• In the statement myAnswer = myNumber * 2, both myAnswer
and myNumber are numeric variables; that is, their intended
contents are numeric values, such as 6 and 3, 14.8 and 7.4, or
218 and 29.
Understanding a Declaration’s
Data Type
• A string variable can hold text, such as letters of the
alphabet, and other special characters, such as punctuation
marks.
• If a working program contains the statement lastName =
"Lincoln", then lastName is a string variable.
• Programmers frequently use strings to hold digits when they
will never be used in arithmetic statements—for example, an
account number or a zip code.
Declaration of Variable
Initialisation
ASSIGNMENT OPERATOR