Basic Elements of C++
Basic Elements of C++
by Neil A. Basabe
The Basics of a C++ Program
A C++ program is a collection of one or more subprograms, called
functions. Some functions called predefined or standard functions,
are already written and are provided as part of the system. But to
accomplish most tasks, programmers must learn to write their own
functions(user-defined functions).
Example:
/*This is a multiple-line
comment……..
*/
Note: the text after the // and between /* */ will be ignored by the
compiler.
Token – is the smallest individual unit of a program written in any
language.
3 Types:
1. Special symbols
* / % + -
. ; ? ,
<= != == >=
2. Reserved words or keywords
intchar float
double string const
void return
5. lastName
6. number1
7. second_Number
8. netIncome
Examples of Invalid Identifiers:
Example:
int number1;
string firstname, lastname, middlename;
char mid_initial;
float salary, hourly_rate;
Whitespaces
Every C++ program contains whitespaces. Whitespaces include blanks,
tabs, and newline characters.
special symbols
Each character is enclosed in single quotes
Information Interchange
• Each of 128 values in ASCII code set
represents a different character
• Characters have a predefined ordering
based on the ASCII numeric value
Collating sequence: ordering of characters
Precedence Rules:
2. 5 – (3 + 7) / ((3 % 4) * 2) 3. 10. 5 % 3
= 5 – (3 + 7) / (3 * 2) = error
= 5 – 10 / (3 * 2)
= 5 – 10 /6
=5–1
=4
Mixed Expressions
Mixed expression:
Has operands of different data types
Contains integers and floating-point
Examples of mixed expressions:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
Mixed Expressions (cont’d.)
Evaluation rules:
If operator has same types of operands
Evaluated according to the type of the
operands
If operator has both types of operands
Integer is changed to floating-point
Operator is evaluated
Result is floating-point
Entire expression is evaluated according to
precedence rules
Type Conversion (Casting)
• Implicit type coercion: when value of one type
is automatically changed to another type
Using more than one variable in cin allows more than one value to be
read at a time
Example: if feet and inches are variables of type int, this
statement:
cin >> feet >> inches;
Inputs two integers from the keyboard
Places them in variables feet and inches respectively
Output Statement
The syntax of cout and << is:
x = 5; x = 5;
y = ++x; y = x++;
Preprocessor Directives
• For example:
#include <iostream>
– Causes the preprocessor to include the header file iostream in the
program
• Preprocessor commands are processed before the program goes through
the compiler
cin and cout are declared in the header file iostream, but within std
namespace
To use cin and cout in a program, use the following two statements:
#include <iostream>
using namespace std;
Using the string Data Type in a Program
Compile a program
◦ Compiler will identify the syntax errors
◦ Specifies the line numbers where the errors occur
Example2_Syntax_Errors.cpp
c:\chapter 2 source
code\example2_syntax_errors.cpp(9) : error
C2146: syntax error :
missing ';' before identifier 'num'
c:\chapter 2 source
code\example2_syntax_errors.cpp(11) :
error C2065: 'tempNum' :
undeclared identifier
Program Style and Form: Syntax
y = w + x; //Line 4: error
Use of Blanks
Example:
Example:
double hours = 35.45;
double rate = 15.00;
Output:
hours = 35.45, rate = 15, pay = 531.75
Use of \n and endl
To insert a new line
Example:
cout << “Hello!\nThis is my \nfirst program!”;
Output:
Hello!
This is my
first program!
Example:
cout << “Hello!” << endl << “This is my” << “\n”
<< “first” << ‘\n’ << “program!”;
Output:
Hello!
This is my
first
program!
The string data type and using
getline()
String – a collection of characters
Example:
string name;
Output: