Вы находитесь на странице: 1из 36

Foundation of C++

Department of Computer Science and Engineering


School of Engineering and Technology
Kaziranga University
Jorhat-785006, Assam.
monowar@kazirangauniversity.in
The Overview of C
C was invented and first implemented by Dennis Ritchie in 1972
C is often called a middle-level computer language

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.

Following are some characteristics


Emphasis is on algorithms
Large programs are divided into smaller modules known as
functions
Most functions shares global data
Functions transform data from one to another
Employs top down approach in program design

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

Objects are the basic run-time entities in an object-oriented


design
A Class is a collection of similar types of object
Data abstraction refers to the act of representing essential
features without including the background details or
explanations
The wrapping of data and functions into single unit is known
as encapsulation

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.

For eg., e1.Salary(name);


e1 is the object, Salary is the message and name is the information

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;
}

<< is the insertion operator and >> is the extraction


operator

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

The smallest individual unit of a program is known as token


Keywords are explicitly reserved identifiers and cannot be
used as names for the program variables.
Some new keywords are: asm, catch, class, delete, friend, inline,
new, operator, private, protected, public, this, try, throw,
virtual, bool
Identifiers refer to the name of variables, functions, arrays,
classes etc. created by programmer
Constants are the fixed values that do not change during the
execution of a program

14
M. H. Bhuyan Lec 4 and 5
Data Types
C++ data types

User-defined type Built-in type Derived type


structure array
union function
class pointer
enumeration reference
Integral type Void Floating type
int float
char double

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

For example, m =50


cout<< m= << ::m <<endl;
//to access global variable
C++ is also known as block structured language

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;

For example, int *p = new int;


int *p = new int [20];

delete operator syntax


delete pointer-variable;

For example, delete p;


delete [ ] p;

20
M. H. Bhuyan Lec 4 and 5
Operators

Advantages over C memory management function


It automatically computes the size of the data object. We need
not use the operator sizeof
It automatically returns the correct pointer type
It is possible to initialize the object while creating the memory
space
new and delete operators can also be overloaded

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;

Implicit or automatic type conversion


For example, m = 5 + 2.75;

28
M. H. Bhuyan Lec 4 and 5
Control Structure

Types of control structure


- Sequence structure (straight line)
- Selection structure (branching)
- Loop structure (iteration or repetition)

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

Switch Structure(Multiple Selection)


32
M. H. Bhuyan Lec 4 and 5
Loop Structure

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

Write a program in C++ to evaluate the following functions


1) Sin x = x x3/3! + x5/5! x7/7!+ ..
2) S = 1 + (1/2)2 + (1/3)3 + (1/4)4 + ..

Write a program to print the following output using loops


1
22
333
4444
55555

35
M. H. Bhuyan Lec 4 and 5
Questions?

36
M. H. Bhuyan Lec 4 and 5

Вам также может понравиться