Practical - Oopm
Practical - Oopm
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
1.
Wri
teapr
ogr
am i
nC++t
ofi
ndl
argestno.
#i
ncl
ude<i
ost
ream>
i
ntmai
n(){
/
/Decl
arev
ari
abl
est
ost
oret
hreenumber
s
doubl
enum1,
num2,
num3;
/
/Pr
omptt
heusert
oent
ert
hef
ir
stnumber
st
d::
cout<<"
Ent
ert
hef
ir
stnumber
:";
st
d::
cin>>num1;
/
/Pr
omptt
heusert
oent
ert
hesecondnumber
st
d::
cout<<"
Ent
ert
hesecondnumber
:";
st
d::
cin>>num2;
/
/Pr
omptt
heusert
oent
ert
het
hir
dnumber
st
d::
cout<<"
Ent
ert
het
hir
dnumber
:";
st
d::
cin>>num3;
/
/Fi
ndt
hel
argestnumber
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
doubl
elar
gest=(
num1>num2)?(
(num1>num3)?num1:
num3):
((num2>num3)?num2:
num3);
/
/Di
spl
ayt
her
esul
t
std:
:cout<<"
Thelar
gestnumberamong"<<num1<<"
,"<<num2<<"
,and"<<num3<<"i
s:"
<<largest<<st
d::
endl
;
/
/Ret
urn0t
oindi
cat
esuccessf
ulcompl
eti
on
r
etur
n0;
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
out
put
:
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
2.Wri
teapr
ogr
am i
nC++t
oimpl
ementt
hemul
ti
pli
cat
ionof[
3*3]mat
ri
x
usi
ngArr
ay.
#i
ncl
ude<i
ost
ream>
consti
ntSI
ZE=3;
v
oidmul
ti
ply
Mat
ri
ces(
intmat
1[SI
ZE]
[SI
ZE]
,i
ntmat
2[SI
ZE]
[SI
ZE]
,i
ntr
esul
t[
SIZE]
[SI
ZE]
){
f
or(
inti
=0;
i<SI
ZE;
++i
){
f
or(
intj
=0;
j<SI
ZE;
++j
){
r
esul
t[
i]
[j
]=0;
f
or(
intk=0;
k<SI
ZE;
++k){
r
esul
t[
i]
[j
]+=mat
1[i
][
k]*mat
2[k]
[j
];
v
oiddi
spl
ayMat
ri
x(i
ntmat
[SI
ZE]
[SI
ZE]
){
f
or(
inti
=0;
i<SI
ZE;
++i
){
f
or(
intj
=0;
j<SI
ZE;
++j
){
st
d::
cout<<mat
[i
][
j]<<""
;
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
st
d::
cout<<st
d::
endl
;
i
ntmai
n(){
i
ntmat
ri
x1[
SIZE]
[SI
ZE]={
{1,
2,3}
,{4,
5,6}
,{7,
8,9}
};
i
ntmat
ri
x2[
SIZE]
[SI
ZE]={
{9,
8,7}
,{6,
5,4}
,{3,
2,1}
};
i
ntr
esul
tMat
ri
x[SI
ZE]
[SI
ZE]
;
/
/Mul
ti
plymat
ri
ces
mul
ti
ply
Mat
ri
ces(
mat
ri
x1,
mat
ri
x2,
resul
tMat
ri
x);
/
/Di
spl
aymat
ri
ces
st
d::
cout<<"
Mat
ri
x1:
"<<st
d::
endl
;
di
spl
ayMat
ri
x(mat
ri
x1)
;
st
d::
cout<<"
\nMat
ri
x2:
"<<st
d::
endl
;
di
spl
ayMat
ri
x(mat
ri
x2)
;
st
d::
cout<<"
\nResul
tantMat
ri
xaf
termul
ti
pli
cat
ion:
"<<st
d::
endl
;
di
spl
ayMat
ri
x(r
esul
tMat
ri
x);
r
etur
n0;
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
Out
put
:
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
3.Wri
teapr
ogr
am i
nC++t
oimpl
ementquadr
ati
cequat
ionusi
ngcl
ass&
Obj
ect.
#i
ncl
ude<i
ost
ream>
#i
ncl
ude<cmat
h>
cl
assQuadr
ati
cEquat
ion{
pr
ivat
e:
doubl
ea,
b,c;
publ
i
c:
/
/Const
ruct
ort
oini
ti
ali
zecoef
fi
cient
s
Quadrat
icEquat
ion(
doubl
ecoef
fA,
doubl
ecoef
fB,
doubl
ecoef
fC):
a(coef
fA)
,b(
coef
fB)
,
c(
coef
fC){}
/
/Funct
iont
ocal
cul
atet
hedi
scr
imi
nant
doubl
ecal
cul
ateDi
scr
imi
nant
(){
r
etur
nb*b-4*a*c;
/
/Funct
iont
ocal
cul
ateanddi
spl
ayr
oot
s
v
oidcal
cul
ateRoot
s(){
doubl
edi
scr
imi
nant=cal
cul
ateDi
scr
imi
nant
();
i
f(di
scr
imi
nant>0){
doubl
eroot
1=(
-b+sqr
t(di
scr
imi
nant
))/(
2*a)
;
doubl
eroot
2=(
-b-sqr
t(di
scr
imi
nant
))/(
2*a)
;
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
st
d::
cout<<"
Root
sar
ereal
anddi
ff
erent
."<<st
d::
endl
;
st
d::
cout<<"
Root1="<<r
oot
1<<st
d::
endl
;
st
d::
cout<<"
Root2="<<r
oot
2<<st
d::
endl
;
}el
sei
f(di
scr
imi
nant==0){
doubl
eroot=-
b/(
2*a)
;
st
d::
cout<<"
Root
sar
ereal
andsame.
"<<st
d::
endl
;
st
d::
cout<<"
Root1=Root2="<<r
oot<<st
d::
endl
;
}el
se{
doubl
ereal
Par
t=-
b/(
2*a)
;
doubl
eimagi
nar
yPar
t=sqr
t(-
discr
imi
nant
)/(
2*a)
;
st
d::
cout<<"
Root
sar
ecompl
exanddi
ff
erent
."<<st
d::
endl
;
st
d::
cout<<"
Root1="<<r
eal
Par
t<<"+"<<i
magi
nar
yPar
t<<"
i"<<st
d::
endl
;
st
d::
cout<<"
Root2="<<r
eal
Par
t<<"-"<<i
magi
nar
yPar
t<<"
i"<<st
d::
endl
;
}
;
i
ntmai
n(){
/
/Exampl
e:Quadr
ati
cequat
ion2x^
2+4x+2=0
Quadr
ati
cEquat
ionquadr
ati
c(2,
4,2)
;
/
/Cal
cul
ateanddi
spl
ayr
oot
s
quadr
ati
c.cal
cul
ateRoot
s()
;
r
etur
n0;
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
Out
put
:
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
4.
Wri
teapr
ogr
am i
nC++t
oimpl
ementCopyConst
ruct
or.
#i
ncl
ude<i
ost
ream>
cl
assMy
Class{
pr
ivat
e:
i
ntdat
a;
publ
i
c:
/
/Const
ruct
ort
oini
ti
ali
zedat
a
My
Class(
intv
alue):
dat
a(v
alue){
}
/
/Copyconst
ruct
or
My
Class(
constMy
Class&ot
her
):dat
a(ot
her
.dat
a){
st
d::
cout<<"
Copyconst
ruct
orcal
l
ed.
"<<st
d::
endl
;
/
/Funct
iont
odi
spl
aydat
a
v
oiddi
spl
ayDat
a(){
st
d::
cout<<"
Dat
a:"<<dat
a<<st
d::
endl
;
}
;
i
ntmai
n(){
/
/Cr
eat
eanobj
ectusi
ngt
heconst
ruct
or
My
Classobj
1(42)
;
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
/
/Di
spl
aydat
ausi
ngt
heor
igi
nal
obj
ect
st
d::
cout<<"
Ori
ginal
Obj
ect
:"<<st
d::
endl
;
obj
1.di
spl
ayDat
a()
;
/
/Cr
eat
eanewobj
ectusi
ngt
hecopyconst
ruct
or
My
Classobj
2=obj
1;
/
/Di
spl
aydat
ausi
ngt
hecopi
edobj
ect
st
d::
cout<<"
\nCopi
edObj
ect
:"<<st
d::
endl
;
obj
2.di
spl
ayDat
a()
;
r
etur
n0;
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
Out
put
:
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
5.Wr
it
eapr
ogr
am i
nC++t
oimpl
ementI
nher
it
ance.
#i
ncl
ude<i
ost
ream>
/
/Basecl
ass
cl
assShape{
pr
otect
ed:
doubl
ewi
dth;
doubl
ehei
ght
;
publ
i
c:
Shape(
doubl
ew,
doubl
eh):
widt
h(w)
,hei
ght
(h){
}
/
/Vi
rt
ual
funct
ionf
orcal
cul
ati
ngar
ea
v
irt
ual
doubl
ecal
cul
ateAr
ea(
){
r
etur
n0.
0;
}
;
/
/Der
ivedcl
ass(
inher
it
sfr
om Shape)
cl
assRect
angl
e:publ
i
cShape{
publ
i
c:
Rect
angl
e(doubl
ew,
doubl
eh):
Shape(
w,h){
}
/
/Ov
err
idet
hecal
cul
ateAr
eaf
unct
ionf
orr
ect
angl
es
doubl
ecal
cul
ateAr
ea(
)ov
err
ide{
r
etur
nwi
dth*hei
ght
;
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
}
;
/
/Der
ivedcl
ass(
inher
it
sfr
om Shape)
cl
assTr
iangl
e:publ
i
cShape{
publ
i
c:
Tr
iangl
e(doubl
ew,
doubl
eh):
Shape(
w,h){
}
/
/Ov
err
idet
hecal
cul
ateAr
eaf
unct
ionf
ort
ri
angl
es
doubl
ecal
cul
ateAr
ea(
)ov
err
ide{
r
etur
n0.
5*wi
dth*hei
ght
;
}
;
i
ntmai
n(){
/
/Cr
eat
eobj
ect
sofder
ivedcl
asses
Rect
angl
erect
angl
e(4,
6);
Tr
iangl
etr
iangl
e(3,
5);
/
/Di
spl
ayar
easofshapes
st
d::
cout<<"
AreaofRect
angl
e:"<<r
ect
angl
e.cal
cul
ateAr
ea(
)<<st
d::
endl
;
st
d::
cout<<"
AreaofTr
iangl
e:"<<t
ri
angl
e.cal
cul
ateAr
ea(
)<<st
d::
endl
;
r
etur
n0;
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
Out
put
:
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
6.
Wri
teapr
ogr
am i
nC++t
oimpl
ementPol
ymor
phi
sm
#i
ncl
ude<i
ost
ream>
/
/Basecl
ass
cl
assShape{
publ
i
c:
/
/Vi
rt
ual
funct
ionf
orcal
cul
ati
ngar
ea
v
irt
ual
doubl
ecal
cul
ateAr
ea(
){
r
etur
n0.
0;
}
;
/
/Der
ivedcl
ass(
inher
it
sfr
om Shape)
cl
assRect
angl
e:publ
i
cShape{
pr
ivat
e:
doubl
ewi
dth;
doubl
ehei
ght
;
publ
i
c:
Rect
angl
e(doubl
ew,
doubl
eh):
widt
h(w)
,hei
ght
(h){
}
/
/Ov
err
idet
hecal
cul
ateAr
eaf
unct
ionf
orr
ect
angl
es
doubl
ecal
cul
ateAr
ea(
)ov
err
ide{
r
etur
nwi
dth*hei
ght
;
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
}
;
/
/Der
ivedcl
ass(
inher
it
sfr
om Shape)
cl
assTr
iangl
e:publ
i
cShape{
pr
ivat
e:
doubl
ebase;
doubl
ehei
ght
;
publ
i
c:
Tr
iangl
e(doubl
eb,
doubl
eh):
base(
b),
hei
ght
(h){
}
/
/Ov
err
idet
hecal
cul
ateAr
eaf
unct
ionf
ort
ri
angl
es
doubl
ecal
cul
ateAr
ea(
)ov
err
ide{
r
etur
n0.
5*base*hei
ght
;
}
;
/
/Funct
iont
odi
spl
ayar
eausi
ngpol
ymor
phi
sm
v
oiddi
spl
ayAr
ea(
Shape*
shape){
st
d::
cout<<"
Area:
"<<shape-
>cal
cul
ateAr
ea(
)<<st
d::
endl
;
i
ntmai
n(){
/
/Cr
eat
eobj
ect
sofder
ivedcl
asses
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
Rect
angl
erect
angl
e(4,
6);
Tr
iangl
etr
iangl
e(3,
5);
/
/Di
spl
ayar
easusi
ngpol
ymor
phi
sm
st
d::
cout<<"
Rect
angl
e";
di
spl
ayAr
ea(
&rect
angl
e);
st
d::
cout<<"
Tri
angl
e";
di
spl
ayAr
ea(
&tr
iangl
e);
r
etur
n0;
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
Out
put
:
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)
Obj
ect
iveOr
ient
edPr
ogr
ammi
ng&Met
hodol
ogy(
CS-
305/I
T-304)
7.Expl
ainPi
l
lar
sofoops.
Encapsul
ati
on:
Defi
nit
ion:
Encapsulati
oni
sthebundl
ingofdat
a(at
tri
but
es)andt
hemet
hods(
funct
ions)t
hat
oper
ateont hedat
ai nt
oasingl
euni
tknownasaclass.
Purpose:
Ithidestheinter
nal
det
ail
sofhowanobj ectoper
atesandprovi
desawell
-defi
ned
i
nterf
acet oi
nteractwit
htheobj
ect
.Encapsul
ati
onhelpsindatahi
dingandpr
otect
st hei
ntegr
it
y
oftheobject
s
Abst
ract
ion:
Defi
nit
ion:
Abstr
acti
onistheprocessofsi
mpli
fyi
ngcompl exsyst
emsbymodel
i
ngcl
asses
basedontheessent
ial
propert
iesandbehavi
orstheypossess.
Purpose:I
tall
owst heprogrammertofocusontherel
evantdet
ail
sofanobj
ectandignor
e
i
rrel
evantdetai
ls.Abstr
acti
onprov
idesaclearsepar
ati
onbetweenwhatanobj
ectdoesandhow
i
tachievesthatfunct
ional
it
y .
I
nher
it
ance:
Defi
nit
ion:
Inher
it
anceisamechanism thatal
lowsanewcl ass(der
ivedorchi
l
dcl
ass)t
oinher
it
at
tri
butesandbehavi
orsf
rom anexist
ingclass(baseorparentcl
ass).
Pur
pose:Itpromot
escodereusabi
li
tybyall
owingther
euseofcodef
rom t
heexist
ingcl
ass.I
t
al
soestabli
shesarel
ati
onshi
pbetweenclasses,
enabl
ingahi
erar
chi
cal
struct
ure.
Pol
ymor
phi
sm:
Defi
nit
ion:
Polymorphi
sm al
l
owsobject
sofdi
ff
erentty
pestobetr
eat
edasobject
sofa
commont ype.I
tcanbeachiev
edt
hroughmet
hodov erl
oadi
ngandmethodov
erri
ding.
Purpose:I
tpr
ovidesawaytoperf
orm asingl
eacti
oni ndi
ffer
entways.Pol
ymorphi
sm al
l
ows
fl
exibi
l
ityi
ncode,maki
ngitmoreadaptabl
etodif
ferentty
pesofdataorobject
s.
Gwal
i
orI
nst
it
uteeofI
nfor
mat
ionTechnol
ogy
,Gwal
i
or(
M.P.
)