Академический Документы
Профессиональный Документы
Культура Документы
2
M. H. Bhuyan Lec 4 and 5
Overview of C++
C++, an extension of C, was developed by Bjarne Stroustrup in
the early 1980s at Bell Laboratories.
a b c d
C++ programs consist of pieces called classes and functions
C++ programs typically go through six phases to be executed .
These are: edit, preprocess, compile, link, load and execute.
C++ program file names often end with the .cpp, .cxx, .cc or
.c extensions
3
M. H. Bhuyan Lec 4 and 5
4
M. H. Bhuyan Lec 4 and 5
Procedure-Oriented Programming
In procedure oriented programming, the problem is viewed as a
sequence of steps to accomplish the task.
5
M. H. Bhuyan Lec 4 and 5
Object-Oriented Programming
A way of modularizing programs by creating partitioned
memory area for both data and functions
Following are some features of OOP
Emphasis is on data rather than procedure
Programs are divided into what are known as objects
Data is hidden and cannot accessed by external functions
Objects may communicate each other through functions
Follows bottom-up approach in program design
6
M. H. Bhuyan Lec 4 and 5
Basic concepts of OOP
Objects
Classes
Data abstraction and encapsulation
Inheritance
Polymorphism
Dynamic binding
Message passing
7
M. H. Bhuyan Lec 4 and 5
Contd
8
M. H. Bhuyan Lec 4 and 5
Contd
Inheritance is the process by which objects of one class acquire the
properties of objects of another class. For eg., Bird (flying and
nonflying)
Polymorphism is the process to exhibit different behaviors in
different instances. For eg., Shape [draw()- circle, square, triangle]
Dynamic binding is the function at run-time
Message passing involves specifying the name of the object, the
name of the function (message) and the information to be sent.
9
M. H. Bhuyan Lec 4 and 5
Applications of OOP
Real-time systems
Simulation and modelling
Object-oriented databases
AI and expert system
CAD/CAM system
Neural network and parallel programming
10
M. H. Bhuyan Lec 4 and 5
A C++ Program
#include<iostream>
using namespace std;
int main()
{
cout<< C++ is better than C \n<<endl;
return 0;
}
11
M. H. Bhuyan Lec 4 and 5
Contd
Namespace
It is introduced by ANSI C++ standard committee
This defines a scope for the identifiers that are used in a
program
std is the namespace where ANSI C++ standard class libraries
are defined.
Each C++ program must include this directive
using and namespace are the two new keywords in C++
12
M. H. Bhuyan Lec 4 and 5
Compute Random Numbers
Header Files: #include <ctime>, #include <cstdlib>
Functions: rand(), srand()
srand((unsigned)time(0)
rand()%n // 0 to (n-1)
rand()%n+1 // 1 to n
13
M. H. Bhuyan Lec 4 and 5
Tokens, Expressions and Control Structure
14
M. H. Bhuyan Lec 4 and 5
Data Types
C++ data types
15
M. H. Bhuyan Lec 4 and 5
Data Types
Type Bytes Range
char 1 - 128 to 127
unsigned char 1 0 to 255
signed char 1 -128 to 127
int 2 -32768 to 32767
unsigned int 2 0 to 65535
signed int 2 -32768 to 32767
short int 2 -32768 to 32767
unsigned short int 2 0 to 65535
signed short int 2 -32768 to 32767
long int 4 -2147483648 to 2147483647
signed long int 4 -2147483648 to 2147483647
unsigned long int 4 0 to 4294967295
float 4 3.4E-38 to 3.4E+38
double 8 1.7E-308 to 1.7E+308
16 longM.double 10 3.4E-4932 to 1.1E+4932
H. Bhuyan Lec 4 and 5
Operators
All C operators are valid in C++
Following are some C++ operators
<< - insertion operator
>> - extraction operator
:: - scope resolution operator
::* - pointer-to-member declarator
->* - pointer-to-member operator
.* pointer-to-member operator
delete memory release operator
new memory allocation operator
17
M. H. Bhuyan Lec 4 and 5
Operators
Scope resolution operator
Syntax:
:: variable name
18
M. H. Bhuyan Lec 4 and 5
Operators
Scope resolution operator
Syntax:
Return type class name :: function name ()
For example,
void programming :: output()
{ }
//to define function outside the class
19
M. H. Bhuyan Lec 4 and 5
Operators
Memory management operators
new operator syntax
pointer-variable = new data-type;
20
M. H. Bhuyan Lec 4 and 5
Operators
21
M. H. Bhuyan Lec 4 and 5
Operators
Manipulators are operators that are used to format the data
display.
<iomanip> stream file is for manipulators
Manipulators: ws, endl, ends, flush, setw, setfill, setprecision,
The most commonly used manipulators are: endl and setw
For example,
cout<<C is subset of C++ <<endl;
cout<<C is subset of C++<<setw(10)<<2013<<endl;
Type cast operator
Syntax: (type-name) expression; - C notation
type-name (expression); - C++ notation
22
M. H. Bhuyan Lec 4 and 5
Expressions
An expression is a combination of operators, constants and
variables.
Types of expression
Constant expressions
Integral expressions
Float expressions
Pointer expressions
Relational expressions
Logical expressions
Bitwise expressions
23
M. H. Bhuyan Lec 4 and 5
Expressions
Constant expressions
- Constant expression contains only constant values
For example, 47, 28+3+9/5.0
Integral expressions
- Integral expression are those which produce integer results
For example, x
x * y 9 /3.0
24
M. H. Bhuyan Lec 4 and 5
Expressions
Float expressions
- Float expressions are those which produce floating point
results
For example, 12.57
6+float(10)
Pointer expressions
- Pointer expressions produce address values
For example, &p
*(&p) + 13
25
M. H. Bhuyan Lec 4 and 5
Expressions
Relational expressions
- Relational expressions yield results of type bool which takes a
value true or false
- For example, x <= y
a+b == c+d
Logical expressions
- Logical expressions combine two or more relational expressions
and produces bool type results
For example, a>b && p ==20
x==10 || y ==6
26
M. H. Bhuyan Lec 4 and 5
Expressions
Bitwise expressions
- Bitwise expressions are used to manipulate data at bit level
For example, x << 4 // shift four bits to left
y >> 2 // shift two bit to right
a=60, 0011 1100 a=a<<2, 1111 00 00
a=60, 0011 1100 a=a>>2, 0000 1111
Assignment expressions
-Chained assignment: x = (y = 10);
p=q=20;
-Embedded assignment: x = (y = 50) + 10;
27
M. H. Bhuyan Lec 4 and 5
Expressions
Compound assignment
- The operator += is known as compound assignment
operator or short hand assignment operator
For example, x += 15;
x = x+15;
28
M. H. Bhuyan Lec 4 and 5
Control Structure
29
M. H. Bhuyan Lec 4 and 5
Sequence structure
Example:
C=A+B
30
M. H. Bhuyan Lec 4 and 5
Selection Structure
F T
T
if Structure (Single
Selection) if/else Structure(Double
Selection)
31
M. H. Bhuyan Lec 4 and 5
Selection Structure
T Break
F
T Break
T
F
T
while Structure
F
do/while structure
33
M. H. Bhuyan Lec 4 and 5
Loop Structure
for Structure
34
M. H. Bhuyan Lec 4 and 5
Problems
35
M. H. Bhuyan Lec 4 and 5
Questions?
36
M. H. Bhuyan Lec 4 and 5