Chapter Two: Programming Fundamentals
Chapter Two: Programming Fundamentals
Programming fundamentals:
1
C++ origins
2
Cont…
3
Cont…
• C++ is an object oriented programming
language
• C++ is a block structured programming
language which means we create functions
inside the function
• C++ is efficient
• C++ is the language of choice for creating
other programming languages.
4
What is toolset ?
The computer can only understand the machine
language code which is the combination of 0 and 1.
The three basic tools used to build c++ application are
the compiler, the linker, and the arc hiver (or librarian).
A collection of these program and possibly other tools is
called a toolset.
5
Cont…
Compilers: takes c++ source file as input and produces object
files, which contain a mixture of a machine executable code
and symbolic references to functions and data.
The arc hiver takes a collection of object files as input and
produces a static library, or archive, which is simply a
collection of object files grouped for convenient use.
The linker takes a collection of object files and libraries and
resolves their symbolic references to produce either an
executable or dynamic library.
6
Executable and Dynamic library
Machine
code
version
+
Linking
Complete
machine
executable
program
8
Structure of c++ program
Install IDE( example code block)
C++ program must have an extension of .cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" ; Function body
return 0;
}
9
Cont…
10
Cont…
• # is a signal for preprocessor, allows to include
<iostream>.
process
11
Cont…
• iostream is the header file which we want to include to our
program so that we can use some objects such as cin, cout etc
namespace.
14
A brief look at cout and cin
• Cout is an object used for printing data to the screen.
• To print a value to the screen, write the word cout,
followed by the insertion operator also called
output redirection operator (<<) and the object to
be printed on the screen.
• Syntax: Cout<<Object;
• Cin is an object used for taking input from the
keyboard.
15
Cont…
• To take input from the keyboard, write the word cin,
followed by the input redirection operator (>>) and
the object name to hold the input value.
• >> is extraction operator
• Syntax: Cin>>Object
• Cin will take value from the keyboard and store it in
the memory.
16
Cont…
• Thus the cin statement needs a variable which is a
reserved memory place holder.
• Both << and >> return their right operand as their
result, enabling multiple input or multiple output
operations to be combined into one statement.
• E.g.: Cin>>var1>>var2>>var3;
• Cout<<var1<<”, “<<var2<<” and “<<var3;
17
Putting Comments on C++ programs
• A comment is a piece of descriptive text which
explains some aspect of a program.
• Program comments are text totally ignored by the
compiler and are only intended to inform the reader
how the source code is working at any particular
point in the program.
18
Single line comment
• C++ provides two types of comment delimiters:
19
Multiple line comment
Multiple Line Comment: Anything enclosed by the
pair /* and */ is considered a comment.
E.g.: /*this is a kind of comment where Multiple lines
can be enclosed in one C++ program */
Comments should be used to enhance (not to hinder)
the readability of a program.
20
Basic Elements of c++
Identifiers
• Identifier is name of a variable or any other named
construct
• Identifier must start with a letter or underscore symbol
(_), the rest of the characters should be letters, digits or
underscores
• The following are legal identifiers:
x x1 x_1 _abc sum RateAveragE
21
Rules of defining identifiers
Identifier must start with letter or underscore
symbol (_), the rest of the characters should
be letters, digits or underscores (A-Z), (a-z)
or(_)
Identifier cannot be a keyword.(E.g. int,
switch, float…etc )
It can have a numbers
Identifier should not have special symbols
such as $, #, ., %, +,…
22
Cont…
Should not start with space and should not
contain empty space
It should not have two consecutive under
score
It should be simple and should exactly
represent why it is for?
Identifier is case sensitive
23
Case Sensitivity
• C++ is case-sensitive.
• In other words, uppercase and lowercase letters are
considered to be different.
• A variable named age is different from Age, which is
different from AGE.
24
Keywords
keywords are identifiers reserved as part of the
language
int, return, float,char
These are keywords used by the compiler to control
your program.
• Cannot be used by the programmer to name things
• Consist of lowercase letters only
• Have special meaning to the compiler
25
Cont…
case
do if return typedef
auto double inline short typeid
bool dynamic_cast int signed typename
break delete long sizeof union
else mutable static unsigned
catch enum namespace static_cast using
char explicit new struct virtual
class extern operator switch void
const false private template volatile
const_cast float protected this wchar_t
continue for public throw while
default friend register true union
delete goto reinterpret_cast try unsigned
26
Variables
• In C++ a variable is a place to store information.
• A variable is a location in your computer's memory in
which you can store a value and from which you can
later retrieve that value.
NOTE: RAM is random access memory.
When you run your program, it is loaded into RAM
from the disk file.
All variables are also created in RAM. When
programmers talk of memory, it is usually RAM to
which they are referring.
27
Variables cont...
• When you define a variable in C++, you must tell the
compiler what kind of variable it is: an integer, a
character, and so forth.
• This information tells the compiler how much room
to set aside and what kind of value you want to store
in your variable.
28
Variable declaration
• Syntax:
30
Example
Eg. Accept variable to compute the value of y where
y=3x+b
Solution
int y, x, b
cout <<“enter the value of x” << endl;
cin >> x;
cout <<“enter the value of b” << endl;
cin >> b;
y=3*x+b;
Cout<<“The value of y is: ”<<y;
31
Scope of Variables.
• Scope of a variable is the boundary or block in a
program where a variable can be accessed.
• The boundary or block is identified by the left and
right bracket
• You can declare variables any where in the
source code.
• But you should declare a variable before using.
32
Scope of variables
35
Fundamental Variable types
• Several other variable types are built into C++.
• They can be conveniently classified as integer, floating-point
and character variables.
• Floating-point variable types can be expressed as fraction
i.e. they are “real numbers”.
• Character variables hold a single byte.
• The type of variables used in C++ program are described in
the next table, which lists the variable type, how much room
36
Cont…
Type Length Range
char 8 bits -128 to 127
37
Signed and Unsigned.
38
E.g.: Lets us have only 4 bits to represent
numbers
39
Characters.
40
Constants
• A constant is any expression that has a fixed value.
• Like variables, constants are data storage locations in
the computer memory. But, constants, unlike
variables their content can not be changed after the
declaration.
• Constants must be initialized when they are created
by the program, and the programmer can’t assign a
new value to a constant later.
• C++ provides two types of constants: literal and
symbolic constants.
41
Cont…
• Literal constant: is a value typed directly into the program
wherever it is needed.
E.g.: const int a= 43;
• 43 is a literal constant in this statement:
• Symbolic constant: is a constant that is represented
by a name, similar to that of a variable, but unlike a
variable, its value can’t be changed after initialization. E.g.:
Int studentPerClass =15;
students = classes * studentPerClass;
• studentPerClass is a symbolic constant having a value of
15, and 15 is a literal constant directly typed in the
program.
42
Thank you for your
attention!
43