Chapter Two: Basic Elements of C++
Chapter Two: Basic Elements of C++
Chapter Two: Basic Elements of C++
INTRODUCTION
C++ is a cross-platform language that can be used to create high performance
applications.
C++ was developed by Bjarne Stroustrup, as an extension to the C language.
C++ gives programmers a high level of control over system resources and
memory.
The language was updated 3 major times in 2011, 2014, and 2017 to C++11,
C++14, and C++17.
C++ is one of the world's most popular programming languages.
C++ can be found in today's operating systems, Graphical User Interfaces,
and embedded systems.
C++ is an object-oriented programming language which gives a clear
structure to programs and allows code to be reused, lowering development
costs.
STRUCTURE OF C++ PROGRAM
On line 1:
the file iostream.h is included in the file
The first character is the # symbol, which is a signal to the preprocessor
Each time you start your compiler, the preprocessor is run.
The preprocessor reads through your source code, looking for lines that begin with
the pound symbol (#), and acts on those lines before the compiler runs.
…
Include
√ is a preprocessor instruction that says, "What follows is a filename. Find that
file and read it in right here."
√ The angle brackets around the filename tell the preprocessor to look in all the
usual places for this file.
√ If your compiler is set up correctly, the angle brackets will cause the
preprocessor to look for the file iostream.h in the directory that holds all the
H files for your compiler.
√ The file iostream.h (Input-Output-Stream) is used by cout and cin, which
assists with writing to the screen and inserting user inputs respectively
Line 3
∂ begins the actual program with a function named main().
∂ Every C++ program has a main() function.
∂ When your program starts, main() is called automatically
∂ main(), like all functions, must state what kind of value it will return.
∂ The return value type for main() in HELLO.CPP is int, which means that this
function will return an integer value.
…
All functions begin with an opening brace ({) and end with a closing brace (}) on
lines 4 and 7.
Everything between the opening and closing braces is considered as part of
functions.
The object cout is used in C++ to print strings and values/messages to the
screen
type the word cout, followed by the output redirection operator (<<).
Whatever follows the output redirection operator is written to the screen.
If you want a string of characters written, be sure to enclose them in
double quotes ("), as shown on line 5.
※ A text string is a series of printable characters
※ The main() function ends on line 7 with the closing brace
The final two characters, \n, tell cout to put a new line after the words Hello World!
BASIC ELEMENTS
Keywords (reserved words)
↘ Reserved/Key words have a unique meaning within a C++ program.
↘ These symbols, “the reserved words”, must not be used for any other purposes.
↘ All reserved words are in lower-case letters.
※ The following are some of the reserved words of C++.
IDENTIFIERS
An identifier is name associated with a function or data object and used to refer to that
function or data object
An identifier must:
※ Start with a letter or underscore
※ Consist only of letters, the digits 0-9, or the underscore symbol _
※ Not be a reserved word
※ the use of two consecutive underscore symbols, _ _, is forbidden.
Syntax of an identifier
…
The following are valid identifiers
At this stage, it is worth noting that C++ is case-sensitive. That is lower-case letters are
treated as distinct from upper-case letters.
the word NUM different from the word num or the word Num.
Identifiers can be used to identify variable or constants or functions.
Function identifier is an identifier that is used to name a function.
CON…
Literals
Literals are constant values which can be a number, a character of a string.
Eg: the number 129.005, the character „A‟ and the string “hello world” are all literals.
There is no identifier that identifies them.
Comments
A comment is a piece of descriptive text which explains some aspect of a
program.
Program comments are totally ignored by the compiler and are only intended
for human readers.
C++ provides two types of comment delimiters:
∆ Anything after // (until the end of the line on which it appears) is considered a comment.
∆ Anything enclosed by the pair /* and */ is considered a comment.
#include<iostream.h>
int main()
{
//this is a comment
int n=10; //valid variable declaration
cout<<“my first variable declaration” <<endl;
cout<<n;
return 0;
}
DATA TYPES, VARIABLES,
AND CONSTANTS
Variables
A variable is a symbolic name for a memory location in which data can be
stored and subsequently recalled.
Variables are used for holding data values so that they can be utilized in
various computations in a program.
All variables have two important attributes
1. A type
which is, established when the variable is defined (e.g., integer, float, character).
Once defined, the type of a C++ variable cannot be changed.
2. A value
which can be changed by assigning a new value to the variable.
The kind of values a variable can assume depends on its type.
For example, an integer variable can only take integer values
e.g., 2, 100, -1 not real numbers like 0.123.
…
Variable Declaration
Declaring a variable means defining (creating) a variable.
You create or define a variable by stating its type, followed by one or more
spaces, followed by the variable name and a semicolon
Good variable names tell the function of the variable
using good names makes it easier to understand the flow of your program.
e.g, int myAge;
Reading assignment
Basic Data Types
Signed and Unsigned
Characters
OPERATORS
Assignment Operators
The assignment operator is used for storing a value at some memory location
means variable.
Its left operand should be an lvalue,
denotes a memory location
its right operand may be an arbitrary expression.
Examples
int m, n, p;
m = n = p = 100; // means: n = (m = (p = 100));
m = (n = p = 100) + 2; // means: m = (n = (p = 100)) + 2;
ASSIGNMENT OPERATOR
EXAMPLE
Code Output
#include<iostream.h>
int main()
{
intn=10;
cout<<“n=“<<n; n=10
return 0;
}
…
…
Arithmetic Operators
C++ provides five basic arithmetic operators.
…
arithmetic operations defined by C++ library
Nb: if you want to use these you must put a #include statement at the start of your program
EXAMPLES OF ARITHMETIC
OPERATIONS DEFINED BY C++ LIBRARY
#include<iostream.h>
#nclude<math.h>//including c++ library
int main()
{
cosin of 10 is =-0.839072
float n=10;
float m=cos(n);
Cout<<“cosin of 10 is =“<<m;
return 0;
}
…
Relational Operators
Used for comparing numeric quantities.
…
Logical Operators
Used for combining logical expression
…
Bitwise Operators
…
Example
…
Increment/decrement Operators
INCREMENT OPERATOR
EXAMPLE
#include<iostream.h> #include<iostream.h>
#include<iostream.h>
int main() int
int main()
main()
{ {{
int x=10, y=15; int
int x=10,
x=10, y=15;
y=15;
cout<<“pre_increment of x is=“<<++x; cout<<“pre_increment
cout<<“pre_increment of
of xx is=“<<++x;
is=“<<++x;
cout<<endl; cout<<endl;
cout<<endl;
cout<<“post_increment of y is=“<<y++; cout<<“post_increment
cout<<“post_increment of
of yy is=“<<y++;
is=“<<y++;
return 0; return
return 0;
0;
} }}
LAB EXERCISE
Write c++ programs for the following problems
1. displaying hello world
2. addition of numbers
3. area of a circle using user input data
4. value swapping between variable using third variable
5. that will calculate simple interest