Introduction To C++ Programming
Introduction To C++ Programming
Computer Programming
Lecture Note
Md Mozasser Rahman
FTK
1
Chapter 2 Introduction to C++
programming
No. Title Page
2
1. Introduction to C++ programming
3
Introduction to C++ programming
History of C/C++
C is a programming language developed in the
1970's alongside the UNIX operating system.
C provides a comprehensive set of features for
handling a wide variety of applications, such as
systems development and scientific
computation.
C++ is an “extension” of the C language, in
that most C programs are also C++ programs.
C++, as opposed to C, supports “object-
oriented programming.”
4
Program development software
1. Editing in a editor
2. Compiling in a compiler
3. Linking in a linker
5
1. Editing in a editor:
It is the 1st step in preparing your program.
Notepad or text editor are two of common editor that can
be use.
IDE that are part of C++ packages also provide built-in editor.
Both of editor above will only store what you type without
any addition of extra character to you file.
So, once you save your codes in file. The file that contain all
program instruction, is known as the source file.
The source file is said to contain the source code.
6
2. Compiling in a compiler
The 2nd step is to compile the source file.
Special program known as compiler is used.
Compiler can be define as a computer program that
transforms/translate source code (high-level compiled
language) into another computer language known as
machine language in the form of binary.
As part of compiler, a program named the pre-processor is
invoked, which takes place before your source code is
compile.
Pre-processor will attends your source code statement and
find statement with the ‘#’ sign.
These ‘#’ statement referred to as compiler directive.
7
Then, the compiler will process our source file and with pre-
processor line and produces a file known as object file
The object file contain what is know as object code, which
CPU of your computer understands, also known as machine
code.
However, pc cannot execute object code since it still has few
parts missing. Its like unfinished highway
9
3. Linking in a linker
The program that bridges all the gaps and completes
assembly of the program is known as the linker.
It will search all the object files and libraries to find the
missing sets of instruction.
Sometimes linker must be told to search certain libraries and
object files.
Its either 3rd party library that you purchased or you have
developed.
Linker will insert the missing sets of instruction into
appropriate places to form a file that has a gaps free
execution path.
This process know as linking which will produces executable
files.
10
The program must be loaded into computer’s memory before
execution can begin.
This action carried out by a loader.
Beside figure shows linking process of object code and RTL
into executable code.
11
Source file
1. Editing in a editor • Notepad or text editor (source file, contain the source
code – human understand)
Execution file 12
IDE for C++ Program
• What is IDE?
• IDE or Integrated Development Environment is
as the name suggests is a digital environment
used to develop games, software, hardware
that offers integration from debugging to all
the way to compiling.
IDEs don’t just let you compile and run your
code, they also give you the tools and features
to speed up your programming work and make
things easy for you.
13
IDE for C++ Program
• Best C++ IDE & Source Editor
1. Dev C++ 3. Code:: Blocks
4. Eclipse
14
C++ Program
A computer sees a program as a set of instructions to be
executed
While, the programmer arranges these instructions in a
certain order depending on the tasks the computer is
expected to perform.
For examples, program that add two numbers, the process
must start with reading or entered the numbers first before
proceed to instruction to add numbers.
These proper step of instruction call syntax.
What is Syntax?: it is the typography and the use of
punctuation marks.
So, now, let start with basic building block of C++
programming.. 15
So, now, let start with basic building block of C++
programming..
Basic building block of C++ can be viewed as a function, a
procedure that produce the result.
Therefore, every C++ program must contains main function
before everything else.
16
• Compiler Preprocessor
Directive to tell
compiler to include
iostream library
• iostream library is call
header file (h)
18
Structure of the function main()
19
USE OF IDENTIFIER, VARIABLE,
CONSTANT AND ETC.
1. COMMENT OR MESSAGE TO THE PROGRAM
Comments are explanatory notes.
They are ignored by the compiler. There are two ways to
include comment in program.
// A double slash marks the start of a single line comment.
or
// A double slash marks the start of a
// single line comment.
20
2. IDENTIFIER Identifier cannot have special
character in them. For examples:
WHAT IS IDENTIFIER? y=x, J –20, ~Ricky, *Michealare
Identifier is a name for variable, invalid identifier
constant, function, etc. Identifiers are case sensitive. For
It consists of a letter followed by examples: Hello, hello,
any sequence of letters, digits, and WHOAMI, WhoAmI, whoamiare
underscore unique identifiers.
Its color green in Turbo C++ Rules for identifiers
Examples of valid identifiers: Must begin with letter or
the underscore
First_name Followed by any
age combination of numerals or
y2000 letter
y2k Recommend meaningful
Examples of invalid identifiers: identifiers
Evaluate the following?
2000y ElectricCharge
8firstname 23Skidoo
snarFbLat
21
Examples 2.1
22
An example showing the syntax of a single
identifier declaration
a) int a ;
b) int a , b , c ;
c) int a = 3 ;
d) int a = 3 , b , c ;
23
3. Documentation
24
4. Keywords
Keywords appear in white in Turbo Programming language
C++. a set of rules, symbols, special
words
Each keyword has a predefined
Rules
purpose in the language.
syntax –specifies legal
Do not use keywords as variable and instructions
constant names!! Symbols
We shall cover the following special symbols ( + - * ! … )
keywords in this class: Word symbols
bool, break, case, char, const, reserved words
continue, do, default, double, else, (int, float, double, char…)
extern, false, float, for, if, int, long,
namespace, return, short, static,
struct, switch, typedef, true,
unsigned, void, while
25
DATA TYPES
most of the time, programming instruction manipulate data.
As such, data plays an important role in programs
Data comes in variety of data types that is sometimes mixed with other
types.
So, its important to be able to differentiate that types of data.
Three attributes that can describe data types;
Name of the data types
Size of the data types
Range of value that data type can handle
A set of value
Combined with a set of operation
data types can be broadly categorized into three types
Integral data types (simple)
Floating point data types (structured)
Pointer data types
26
Simple data types include
Integers
Floating point
Enumeration
Integer data types include
char
short
int
long
bool
Different floating-point
types
Note that various types
will
have different ranges
of values
require different
amounts of memory
27
Integers
Types short, int, and long are available for operation with integers
These types are distinguished by their value.
The int (integer);
Is Tylor-mad for computers and adapt to the length of a register on computer.
In 16-bit computer, its equivalent to short
In 32-bit computer, its equivalent to long
1 byte = 8 bits
1111 1111 (Binary)
FF (Hexadecimal)
255 (Decimal)
28
Floating point
Numbers with a fraction part are indicated by a decimal point in C++
and are referred to as floating point numbers
There are three types of calculation involving floating-point;
Float : for simple accuracy
Double : for double accuracy
Long double : high accuracy
Value range and accuracy of a type are derived from the amount of
memory allocated and internal representation of the type.
29
30
Exercise
2. Write C++ program that defines two variables for floating-point numbers
and initializes them with the values 123.456 and 76.543. Then, display the
sum and the difference of these two numbers on screen
31
Pre-processor directive
Pre-processor directive is also known as header
file (.h) as as include files.
Runs before the compiler
Modifies the text of the source code before
the compiler starts
Syntax
start with # symbol
#include <headerFileName>
Example: #include <iostream.h>
Compiler directives appear in blue in Visual C++.
The #include directive tells the compiler to include
some already existing C++ code in your program.
The included file is then linked with the program.
There are two forms of #include statements:
Note the pre-processor step in
• #include <iostream.h> // for pre-defined files the sequence
• #include "my_lib.h" // for user-defined files
32
Input/output expression
1.OUTPUT
Values sent to an output device
Usually the screen
Can also be a file or some device
Syntax for screen output:
cout << expression << …
#include <iostream>
using namespace std;
int main()
Output
{
int x;
cout << "Please enter x = " <<endl;
cin >> x;
cout << "Value x = " << x <<endl;
return 0;
}
36