Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

BNJ 20802

Computer Programming
Lecture Note
Md Mozasser Rahman
FTK

1
Chapter 2 Introduction to C++
programming
No. Title Page

1. Introduction to C++ programming 3


a) History of C/C++
b) Program development software
c) C++ Program
2. Fundamental of C++ 14

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

Figure shows the process of pre-processor and


the compiler are gradually becoming merged.
8
 At this stage, object code still contain undefined references.
 The undefined references refer to pieces of object code that
need to be retrieved from elsewhere to complete entire
program.
 The compiler can be viewed in basic term as a translator that
checks grammatical content.

Figure shows the compiling process of the


source codes

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)

• Using compiler to compile the source file


• before source code is compile, the pre-processor ( # -
Object file compiler directive) is invoked
• compiler will process our source file and with pre-
2. Compiling in a processor line and produces a file object file (contain
compiler object code / machine code – CPU understand)
• Pc still cannot execute object code – part missing
(undefined references)

• Linker - competed all the gaps, search all the object


files and libraries to find the missing sets of instruction
3. Linking in a linker to produces executable files , contain the execution
code.
• Loader – To loading the program into computer’s
memory before execution begin.

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

2. Visual Studio Code

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)

• Tell the compiler to


find the names in
Output standard library
• Body of the function

• Semicolon ( ; ) - statement terminator.


• Two slash ( // ) - line comment, ignores all text after.
• /* and */ - line comment ignore any text between.
• Return 0 - placed at the end of main function to exit the program and 0
indicates that the program has terminated successfully.
• { } - left brace { beginning of function, right brace } end of function.
• int main () - function main will return as integer and () = void no arguments are
being received by main
17
2. Fundamental of C++

18
Structure of the function main()

 C++ contains member function and global function.


 Each function fulfils its own particular task.
 In C++, global function main() is compulsory since its
contain the main program.

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.

/* A slash followed by an asterisk marks the start of a multiple line


comment. It ends with an asterisk followed by a slash. */

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

Please state either valid or not valid for identifier below:


No Questions Valid/not valid? Why
1 First_name:
2 2000y
3 age
4 y2000
5 snarFBlat
6 y2k
7 *Micheal
8 y=x
9 23skidoo

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 ;

Data type identifier Assigned value Semicolon ( ; ) – terminate the line

23
3. Documentation

 Comments specified between


/* this is a comment */
and following // also a comment
 Always put at beginning of program
/* name,
date,
cpo,
purpose of program
*/

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

1. which of the variable definitions shown on below is invalid or does not


make sense?

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 << …

 Escape sequences also used to manipulate output


cout<< "The total is\t "<< sum << endl;
Output
The total is
33
Example:
#include <iostream>
using namespace std;
int main()
{
cout << "1.Hello world hello you Do you ready!!!" << endl;
cout << "2.\n Hello world\n hello you\n Do you ready!!!\n" << endl;
cout << "3.\t Hello world\t hello you\t Do you ready!!!\t" << endl;
cout << "4.\b Hello world\b hello you\b Do you ready!!!\b" << endl;
cout << "5.\b Hello world\b hello you\b Do you ready!!!\bA" << endl;
cout << "6.\r Hello world hello you Do you ready!!!" << endl;
cout << "7. Hello world hello you Do you ready!!!\rA" << endl;
cout << "8. Hello world\r hello you Do you ready!!!" << endl;
cout << "9. Hello world\r hello you Do you ready!!!\rA" << endl;
cout << "10.\\Hello world \\ hello you \\ Do you ready!!!\\" << endl;
cout << "11.\*Hello world \* hello you \* Do you ready!!!\*" << endl;
cout << "12.\"Hello world \" hello you \" Do you ready!!!\"" << endl;
cout << "13.\*\*\\\\\"\"" << endl;
return 0;
}
Output
2. INPUT
 Storing data in the computer's memory requires two steps
 1. Allocate the memory by declaring a variable
 2. Have the program fetch a value from the input device and place it
in the allocated memory location

#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

You might also like