CE 204: Computer Programming Sessional
Introduction to C++
1
Tazwar Bakhtiyar Zahid
Lecturer,
Department of Civil Engineering, BUET
2
Course Contents
• Programming concepts and algorithms
• Internal representation of data
• Elements of structured programming language
– Data types
– Operators
– Expressions
– Control structures
– Functions
– Pointers and arrays
– Input and output
• Concept of Object-Oriented Programming (OOP)
– Encapsulation
– Inheritance
– Polymorphism and abstraction.
3
Activities
• Attendance
• Assignments/Homeworks
• Class Assessments
• Mid Quiz
• Final Quiz
4
Lecture Schedule
Sl. No.
Syllabus
1.
Introduction to C++, Data, Data-types, Parts of a Program
2.
Variables & Declaration, Input Output Operators
3.
Arithmetic Operators, Conditional and Logical Operations
4.
Loop
5.
Functions
6.
Object-oriented programming - 1:
Idea of class, passing classes to functions, Access modifier (public/private)
7.
Mid-Quiz
8.
Object-oriented programming - 2:
Constructor, Destructors, Inheritance, Encapsulation.
9.
Array-1
10.
Array-2
11.
Pointers
12.
File Input/Output
Object-oriented programming - 3: Polymorphism, Abstraction
13.
Final Quiz
14.
Extra Class (If Required)
5
Programming Language
• Machine Language
• Assembly Language
• Mid level and High level Language
6
Communicating with the Computer
7
Communicating with the Computer
• Assembler
• Compiler
• Interpreter
– Text Editor
– Debugger
– Integrated Development Environment (IDE)
➢Microsoft Visual Studio/Microsoft Visual C++
➢Codeblocks
➢C++ is CASE SENSITIVE
8
Programmer’s Job
• What to do – Problem Identification
• How to do – Algorithm
• Execute – Programming
9
Data Representation
• In our familiar number system, we have 10
symbols (0 to 9) to represent a number
• We call it decimal system or 10 base system
• The place value of a digit in this system is a
multiple of 10x
• For any other base value b, place value of a digit is
bx
• To process data in a computer, we need to
represent them in the registers of the processor and
a register consists of several circuits
10
Data Representation
• We can symbolize an electric circuit into two states
such as:
– contains current
– no current
• We may think these two states as two symbols (say
one is 0 and the other is 1) to represent a number
• Thus we have only two digits to represent a
number in the computer processor, in the memory
or in the data storage devices
11
Data Representation
• Each individual circuit represents a digit, and we
term it as a bit (it is an abbreviation of binary digit)
• The largest number for a given number of digits
can be obtained by filling out each position with
the largest symbol. Such as:
– in the decimal system, the largest number with 3
digits, (103-1) = 999
– similarly, in binary, largest number with 3 digits
= 111 [ in decimal system (23-1) = 7]
12
Data Representation
• It is understood that the greater number of circuits
we engage to represent a number, the bigger
number we can store
• Also, the leftmost bit is reserved for sign.
• 8 bits (i.e., 8 circuits) make a unit of memory which
is called a byte.
• If we employ 2 bytes (16 bits) for a number then,
we will have 15 bits for digits and the largest
number that can be stored will be 215-1 = 32767
13
Data Representation
• We can represent any integer number in binary, if
sufficient bits are used
• What happens with non-integer (i.e. floating point)
numbers?
• (0.1)10 = ? In binary
14
Data Representation
• (0.1)10
= (0.00011001100110011..........)2
= (0.00011)2
• It is clear that it is never possible to
represent (0.1)10 exactly in binary.
• However,
we
can
increase
the
accuracy by using more bits for
floating point numbers than that for
integer numbers
15
Data Representation
• The
scheme for integers and floating point
numbers is different
• A floating point number is first converted to its
equivalent in exponential form with the decimal
point to the leftmost significant digit.
16
Data Representation
• 57.234 = 0.57234 x 102
Exponent
Mantissa
• Besides numbers we have to represent / store
characters and other symbols in the computer
• Usually, they are coded in 8 bit words
17
Data Representation
• If we use 8 bits, we can store numbers ranging
from
– (00000000)2 --> decimal equivalent 0
– (11111111)2 --> decimal equivalent -->28-1= 255
• So, 256 symbols may be coded.
• From the above discussion it should be understood
that different types of data are stored using
different schemes
• So, before we store any data, we must specify the
type of the data
18
Basic Data Types
• Standard C++ has different fundamental data types
19
Basic Data Types
• Typically float will provide 7-digit precision,
double will provide 15-digit precision and long
double will provide 19-digit precision
Keywords
• These are reserved words that have particular
significance and when needed are to be used as it is
such as : include, iostream, using, namespace, std,
int, cout, return are keywords
20
CE 204: Computer Programming Sessional
The First Program
21
Hello World! Program
#include <iostream>
Preprocessor Directive
int main()
{
std::cout << "Hello,World!\n";
}
➢ The first line of this source code is a
preprocessor directive that tells the C++
compiler where to find the definition of the
std::cout object that is used on the third line. 22
Hello World! Program
#include <iostream>
int main()
Preprocessor Directive
Identifier (Header File)
{
std::cout << "Hello,World!\n";
}
➢ The identifier iostream is the name of a file in
the Standard C++ Library
➢ The pound sign # is required to indicate that the
word “include” is a preprocessor directive
23
Hello World! Program
#include <iostream>
int main()
Preprocessor Directive
Identifier (Header File)
{ std::cout << "Hello,World!\n";
}
➢ Angle brackets < > are required to indicate that
the word “iostream” (“input/output stream”) is
the name of a Standard C++ Library file.
➢ The expression <iostream> is called a standard
header
24
Hello World! Program
#include <iostream>
int main()
Identifier & Main Function
{ std::cout << "Hello,World!\n";
}
➢ The identifier main is the name of a function
➢ Every C++ program must have one and only
one main() function
➢ The required parentheses that follow the word
“main” indicate that it is a function
25
Hello World! Program
#include <iostream>
int main()
Data Type
{ std::cout << "Hello,World!\n";
}
➢ The keyword int is the name of a data type in
C++. It stands for “integer”
➢ It is used here to indicate the return type for the
main() function
26
Hello World! Program
#include <iostream>
int main()
Standard Output Stream Operator
{ std::cout << "Hello,World!\n";
Program
}
Body
Output Operator
➢ A program body is a sequence of program
statements enclosed in braces { }
➢ The single symbol << represents the C++
output operator
27
Hello World! Program
#include <iostream>
int main()
Newline Character
{ std::cout << "Hello,World!\n";
}
➢ When this statement executes, the characters
enclosed in quotation marks " " are sent to the
standard output device (computer screen)
➢ The last two characters \n represent the
newline character
28
Escape Sequence
• We can control the appearance of string output or
message by using escape sequence
Escape Sequence
Control Character
\n
newline
\t
horizontal tab
\v
vertical tab
\b
backspace
\r
carriage return
\f
form feed
\a
alert-bell
\\
backslash
\’
single quote
\’’
double quote
\?
question mark
29
Hello World! Program
#include <iostream>
int main()
Ends Statement
{ std::cout << "Hello,World!\n";
}
➢ Every program statement must end with a
semicolon (;)
➢ C++ compiler ignores formatting
30
Hello World! Program
#include <iostream>
Preprocessor Directive
int main()
Blank Space
{ std::cout << "Hello,World!\n";
}
➢ Blank spaces are ignored by the compiler
except where needed to separate identifiers
➢ Note that the preprocessor directive must
precede the program on a separate line
31
Hello World! Program
#include <iostream>
using namespace std;
int main()
{ // prints "Hello, World!":
cout << "Hello, World!\n";
return 0;
}
➢ using namespace std tells the C++ compiler to
apply the prefix std:: to resolve names that
need prefixes
➢ A namespace is a named group of definitions 32
Hello World! Program
#include <iostream>
using namespace std;
int main()
{ // prints "Hello, World!":
Comment
cout << "Hello, World!\n";
return 0;
}
➢ A comment in a program is a string of
characters that the preprocessor removes
before the compiler compiles the programs
➢ C style comment: /* prints "Hello, World!": */ 33
Thank You!
34