oops unit 1
TRANSCRIPT
-
8/9/2019 OOPs Unit 1
1/25
Object Orientation
Fundamentals & Overview toProgramming language C++
Bhagwant Singh
-
8/9/2019 OOPs Unit 1
2/25
Index
Procedure Oriented Programming
OOP Paradigm
Basic concept of object oriented program
Benefits of OOPs
Evolution of C++.
Input and Output operator.
Structure of C++ program
Tokens
ata t!pes
-
8/9/2019 OOPs Unit 1
3/25
Procedure-OrientedProgramming
"ie#ed as t$e se%uence of t$ings to be done
T$e primar! focus is on functions
&ierarc$ical decomposition
In a multi'function program( man!
important data items are placed as global
but in a large program it is ver! difficult to
identif! #$at data is used b! #$ic$
function.
)unctions are action'oriented and do not
reall! corresponding to t$e element of t$e
problem.
-
8/9/2019 OOPs Unit 1
4/25
OOPS Paradigm
Object Oriented Programming *OOP is an approac$ to program organi,ation anddevelopment t$at attempts to eliminate some of t$e pitfalls of conventional programming
met$ods b! incorporating t$e best of structured programming features #it$ several
po#erful ne# concepts.
Ties data more closel! to t$e function t$at operate on it.
OOP allo#s decomposition of a problem into a number of entities called objects and t$en
builds data and function around t$ese objects.
-
8/9/2019 OOPs Unit 1
5/25
Features
Emp$asis is on data rat$er t$an procedure.
Programs are divided into #$at are kno#n as objects.
ata structures are designed suc$ t$at t$e! c$aracteri,e t$e objects.
)unctions t$at operate on t$e data of an object are ties toget$er in t$e data structure.
ata is $idden and cannot be accessed b! e-ternal function.
Objects ma! communicate #it$ eac$ ot$er t$roug$ function.
e# data and functions can be easil! added #$enever necessar!.
)ollo#s bottom up approac$ in program design.
-
8/9/2019 OOPs Unit 1
6/25
Basic Concepts of OOPs
Objects
Classes
Data abstraction and encapsulation Inheritance
Polymorphism
Dynamic binding
Message passing
-
8/9/2019 OOPs Unit 1
7/25
Benets T$roug$ in$eritance( #e can eliminate redundant code e-tend t$e use of e-isting
Classes.
/e can build programs from t$e standard #orking modules t$at communicate
#it$ one anot$er( rat$er t$an $aving to start #riting t$e code from scratc$. T$is
leads to saving of development time and $ig$er productivit!.
T$e principle of data $iding $elps t$e programmer to build secure program t$at
can not be invaded b! code in ot$er parts of a programs. It is possible to $ave multiple instances of an object to co'e-ist #it$out an!
interference.
It is possible to map object in t$e problem domain to t$ose in t$e program.
It is eas! to partition t$e #ork in a project based on objects.
T$e data'centered design approac$ enables us to capture more detail of a modelcan implemental form.
Object'oriented s!stem can be easil! upgraded from small to large s!stem.
0essage passing tec$ni%ues for communication bet#een objects makes to
interface descriptions #it$ e-ternal s!stems muc$ simpler.
Soft#are comple-it! can be easil! managed.
-
8/9/2019 OOPs Unit 1
8/25
!"olution O# C$$
C$$ was de"eloped by BjarneStroustrup at %&'& Bell (aboratoriesin Murray )ill* +ew ,ersey* -S%* in the
early ./012s3 C$$ is an extension o# C
4arious #eatures o# C$$ enable
creating o# abstract data types* inheritproperties #rom existing data typesand support polymorphism3
-
8/9/2019 OOPs Unit 1
9/25
C$$ Program
5include6iostream7-sing namespace std8
intmain9:
;
cout66< =elcome to C$$>n
-
8/9/2019 OOPs Unit 1
10/25
+amespace is a new concept introducedby the %+SI C$$ standards committee3
&his denes a scope #or the identiersthat are used in a program3
In C$$* main 9: returns an integer "alue
to the operating system3 &here#ore* e"erymain 9: in C$$ should end with a return91: statement8 otherwise a warning anerror might occur3 Since main 9: returns
an integer type #or main 9: is explicitlyspecied as int. +ote that the de#aultreturn type #or all #unction in C$$ is int3
-
8/9/2019 OOPs Unit 1
11/25
Input %nd Output Operator
Input Operator @
Cin77 siAe8
Cin is Standard input stream
It causes the program to wait #or the user to type in a
number
&he operator 77 is nown as extraction or get#romoperator3
Output Operator @
Cout 66 &he SiAe is 66 siAe8Cout is Standard output stream
&he operator 66 is called the insertion orput tooperator3
-
8/9/2019 OOPs Unit 1
12/25
5include6iostream3h7
-sing namespace std
"oid main9:;
int a.1118
cout 66 a is 66a66endl8cout 66enter number
-
8/9/2019 OOPs Unit 1
13/25
-
8/9/2019 OOPs Unit 1
14/25
Classes and Objects
% Class is a static description o# a type
It encapsulates the data and all legal
operations on that data
It also denes who has access to that data
%n object is an instance o# a class9sometimes we say an instantiation:
-
8/9/2019 OOPs Unit 1
15/25
5include6iostream3h7
using namespace std8
class person
;
char nameG1H8
Int age8
public@"oid getdata9"oid:8
"oid display9"oid:8
?8
"oid person @@ getdata9"oid:
;
cout 66 !nter name@ 8
cin 77 name8
cout 66 !nter age@ 8
cin 77 age8
?
4oid person @ @ display9"oid:
;
cout 66 >n+ameame@ 66 name8
cout 66 >n%ge@ 66 age8
?
Intmain9:
;person p8
p3getdata9:8
p3display9:8
eturn 18
?
-
8/9/2019 OOPs Unit 1
16/25
Client Ser"er Model
-
8/9/2019 OOPs Unit 1
17/25
&oens
Smallest indi"idual units in a programe is called astoens3
Jeywords are explicitly reser"ed identier that can2tbe used as names #or the programe "ariable3
Identiers re#er to the names o# "ariables* #unctions*arrays * classes etc
Major diKerence between the C and C$$ is the limiton the length o# a name3 C recogniAe only rst GL
characters o# the name but there no such limit in C$$
=chart type is a wideNcharacter literal introduce byC$$
-
8/9/2019 OOPs Unit 1
18/25
Data types
C$$ Datatypes
-serDenedStructure
-nionClass
!numeration
Deri"edtype
%rrayFunctionPointer
e#erence
Build in &ype
Integral &ype Floating &ype4oid
Int Char oatDoubl
e
-
8/9/2019 OOPs Unit 1
19/25
4oid
-ses o# 4oid&o speci#y the return type o# a #unction when
it is not returning any "alue3
&o indicate empty argument list to a #unction&o declare a generic Pointers3
4oid gp8
Statement .
Int ip8gp ip8
Statement G
ip gp8
-
8/9/2019 OOPs Unit 1
20/25
Structure "Qs -nion
% structure is dened withRstruct2 eyword3
%ll members o# the structurecan be manipulatedsimultaneously3
&he siAe o# structure objectis eEual to the sum o# theindi"idual siAe o# themember #unction3
Structure member areallocated distinct memorylocation3
Structure are not consideredas memory ecient3
%n -nion is dened withRunion2 eyword3
Members o# the union canbe manipulated only one
at a time3 &he siAe o# union object is
eEual to the siAe o# thelargest member #unction3
-nion member share
common memory space#or their exclusi"e usage3
-nion are not consideredas memory ecient3
-
8/9/2019 OOPs Unit 1
21/25
!numeration
%n enum is a user dened data type whichpro"ides away #or attaching name to anumbers* thereby increasing comprehensibilityo# the code3
enum shape9circle* sEuare* triangle:8
enum color9red* blue* green* yellow:8
In C$$ the tag name shape and color becomes
new types names3 By using these tag names wecan declare new "ariables3 Shape ellipse8 QQ ellipse is o# type shape
Color bacground8 QQ bacground is o# type color
-
8/9/2019 OOPs Unit 1
22/25
!numeration
Statement . Color bacground blue8
Statement L Color bacground T8
Statement G Color bacground 9color: T8
Statement U Int c blue8
-
8/9/2019 OOPs Unit 1
23/25
Storage Classes
In addition to data types* "ariable alsoha"e storage classes3
% storage class species the li#e time
and "isibility with in the program3
&here are #our storage classes %utomatic
!xternal Static
egister
-
8/9/2019 OOPs Unit 1
24/25
Deri"ed Data types
%rray Character %rray
9)ow will you declare a character array
that store xyA< V :
Functions
Pointer Statement .@ Char const ptr.
WOOD
-
8/9/2019 OOPs Unit 1
25/25