Chapter 2
Chapter 2
C++ Programming
Basics
Animated Version
Chapter 2 - 1
Topics
Chapter 2 - 2
Basic Program Construction
#include <iostream>
using namespace std;
int main()
{
cout << "Every age has a language of its own\n";
return 0;
}
Function
Chapter 2 - 3
Functions (1)
#include <iostream>
using namespace std;
int main()
{
cout << "Every age has a language of its
own\n";
return 0;
}
int main()
{
cout << "Every age has a language of its own\n";
return 0;
}
Chapter 2 - 5
Functions (3)
#include <iostream>
using namespace std;
int main()
{
cout << "Every age has a language of its own\n";
Chapter 2 - 6
Program Statements
#include <iostream>
using namespace std;
int main()
{
cout << "Every age has a language of its own\n";
return 0;
}
Chapter 2 - 7
int main()
{
cout << "Every age has a language of its own\n";
return 0;
}
int main()
{
cout << "Every age has a language of its own\n";
return 0;
}
• Preprocessor Directives
– starts with a number sign ( # )
– An instruction to the compiler. A part of the compiler
called the preprocessor deals with these directives before
it begins the real compilation process.
– #include tells the compiler to insert another file into your
source file - header file
• IOSTREAM - concerned with basic input/output. Without these
declarations, the compiler won’t recognize cout and will think <<
is being used incorrectly.
Chapter 2 - 9
Directives (2)
#include <iostream>
using namespace std;
int main()
using directive {
cout << "Every age has a language of its own\n";
return 0;
}
• Using Directives
– C++ program can be divided into different namespaces.
• A namespace is a part of the program in which certain names are
recognized; outside of the namespace they’re unknown.
– using namespace std;
• says that all the program statements that follow are within the std
namespace.
• If we didn’t use the using directive, we would need to add the std
name to many program elements.
– std::cout << “Every age has a language of its own.”;
Chapter 2 - 10
Matching Comment Markers
/* This is a comment on one line */
/*
Comment number 1
*/
/*
Comment number 2
*/
/*
/*
This is a comment
*/
Error: No matching
beginning marker.
*/
Chapter 2 - 11
/*
This is a comment with
three lines of Multiline Comment
text.
*/
// This is a comment
// This is another comment Single line Comments
// This is a third comment
Chapter 2 - 12
Variables
– The most fundamental part of any language
– A variable has a symbolic name and can be given a variety of values.
– When a variable is given a value, that value is actually placed in the memory space
assigned to the variable.
– Most popular languages use the same general variable types, such as integers, floating-
point numbers, and characters
– The amount of memory occupied by the integer types is system dependent.
– On a 32-bit system such as Windows, an int occupies 4 bytes (which is 32 bits) of
memory. This allows an int to hold numbers in the range from –2,147,483,648 to
2,147,483,647.
Chapter 2 - 13
Integer Variables
// intvars.cpp
// demonstrates integer variables
#include <iostream>
using namespace std;
Chapter 2 - 14
Declarations and Definitions
Chapter 2 - 15
Variable Names
• names given to variables A-z, 1-9, _, $
Chapter 2 - 17
Other Types
– Size of type int is system dependent.
• 4 bytes in 32-bit system
– Types long (4 bytes) and short (2 bytes) have fixed sizes
no matter what system is used.
– char (1 byte) stores integers that range in value from –128
to 127.
– Standard C++ provides a larger character type called
wchar_t to handle foreign languages.
– Character constants use single quotation marks around a
character, like ‘a’ and ‘b’.
• When the C++ compiler encounters such a character constant, it
translates it into the corresponding ASCII code.
Chapter 2 - 18
Character Variables
// charvars.cpp
// demonstrates character variables
#include <iostream> //for cout, etc.
using namespace std;
int main()
- Declaration and initialization
{
char charvar1 = 'A'; //define char variable as character
char charvar2 = '\t'; //define char variable as tab
Chapter 2 - 19
Escape Sequences
Chapter 2 - 20
Input Using cin
// fahren.cpp
// demonstrates cin, newline
#include <iostream>
using namespace std;
Chapter 2 - 22
Floating Point Types (1)
Chapter 2 - 24
The setw Manipulator
Chapter 2 - 25
Chapter 2 - 26
Testing Signed and Unsigned Integers
// signtest.cpp
// tests signed and unsigned integers
#include <iostream>
using namespace std;
int main()
{
int signedVar = 1500000000; //signed
unsigned int unsignVar = 1500000000; //unsigned
Chapter 2 - 27
// mixed.cpp
// shows mixed expressions
#include <iostream>
using namespace std;
int main()
{
int count = 7;
float avgWeight = 155.5F;
Chapter 2 - 28
Type Conversion (2)
Chapter 2 - 29
Chapter 2 - 30
Type Conversion (4)
// cast.cpp
// tests signed and unsigned integers
#include <iostream>
using namespace std;
int main()
{
int intVar = 1500000000; //1,500,000,000
intVar = (intVar * 10) / 10; //result too large
cout << "intVar = " << intVar << endl; //wrong answer
Chapter 2 - 31
Operators (1)
– Arithmetic Operators:
– Common operators: + , - , * , and /
– Remainder Operator: % (works only on integer)
– Arithmetic Assignment Operators:
+=, -=, *=, /=, and %=
– Increment and decrement operators: ++, --
Chapter 2 - 32
Remainder and Arithmetic Assignment
Operator (example)
// remaind.cpp // assign.cpp
// demonstrates remainder operator // demonstrates arithmetic assignment operators
#include <iostream> #include <iostream>
using namespace std; using namespace std;
// increm.cpp
// demonstrates the increment operator
#include <iostream>
using namespace std;
int main()
{
int count = 10;
Chapter 2 - 34
Library Functions and Header Files (1)
• Perform file access, mathematical computations,
and data conversion, among other things.
• To use a library function like sqrt(), you must
link the library file that contains it to your program.
• The functions in your source file need to know the
names and types of the functions and other
elements in the library file. They are given this
information in a header file. Each header file
contains information for a particular group of
functions.
Chapter 2 - 35
int main()
{
double number, answer; //sqrt() requires type double
Chapter 2 - 37
Summary (1)
• Function is a major building block of C++ programs
• A function named main() is always the first one executed when a
program is executed.
• A function is composed of statements, which tell the computer to do
something.
– Each statement ends with a semicolon. A statement may contain one or more
expressions, which are sequences of variables and operators that usually evaluate
to a specific value.
• Output is most commonly handled in C++ with the cout object and <<
insertion operator, which together cause variables or constants to be
sent to the standard output device—usually the screen.
• Input is handled with cin and the extraction operator >> , which cause
values to be received from the standard input device—usually the
keyboard.
Chapter 2 - 38
Summary (2)
• Various data types are built into C++:
– char, int, long, and short are the integer types and
– float , double , and long double are the floating-point types. All of these types are
signed.
• Unsigned integer types, signaled by the keyword unsigned, don’t hold
negative numbers - hold positive ones twice as large.
• Type bool is used for Boolean variables and can hold only true or false .
• The const keyword stipulates that a variable’s value will not change in
the course of a program.
• C++ employs the usual arithmetic operators + , - , * , and / . In addition,
the remainder operator, % returns the remainder of integer division.
• The arithmetic assignment operators += , +- ,
– perform an arithmetic operation and an assignment simultaneously.
• The increment and decrement operators ++ and -- increase or decrease
a variable by 1.
Chapter 2 - 39
Summary (3)
• Preprocessor directives consist of instructions to the compiler, rather
than to the computer.
– The #include directive tells the compiler to insert another file into the present
source file, and
– the #define directive tells it to substitute one thing for another.
– The using directive tells the compiler to recognize names that are in a certain
namespace.
• If you use a library function in your program, the code for the function is
in a library file, which is automatically linked to your program.
– A header file containing the function’s declaration must be inserted into your
source file with an #include statement.
Chapter 2 - 40