Professional Documents
Culture Documents
C++ (Unit-3) No Operator Overloading
C++ (Unit-3) No Operator Overloading
C++ (Unit-3) No Operator Overloading
*. Constouctors:
A danA Lont9uctot id Apocinl, Jmomkesr unction
HOnte
O cdana that J orotutocl Juhoneuet we
inti
Poblic
AC/constductor declarecl
iL
iflocitradius.aea
Publid y
ciocle C onstovctor
fadli us = 5-0;1
void computearea C)
J
e cannot cail a constvuctor cause cconStoLCtors age
Callecl automaticalls uwhenever we coeate an ohje,t.
ciclee Ci.c2;
ahoulel Jbe initiolinerk usi th
we connot nny that elieut c
Jue initiolpecl wih angtdo
one value and CQ louldl
valuo. ule sannot Je Jolerkire
Sw AUEunt dijesant
DU
aliuas o diestand ojnt
than, cicle C)
c i o c l e ci.c2;
ciscle co C4-)
hi l ahQUr AN O A D Jernuaa h a CoAtoudo
uollA
whith hap JeQaN dorUagAsl dnarrtoke Dny
i-e ciocle C).
S we houe t fLOUica a snntsurtgl h a t takan A
class Cfloat);
with cL
citcle cívcle C)/ratches
gadius= a c ; ,
int main ()
class CL;
class C2C4;
C1.ehow C)
c9.show C):
Tetuon O;
Hhat d 2 D o t tara auy prgLOmTO
A coNtuLrts
NAisucto,
ib Lalao nowun , a dolou
JUonotsuctou
he e n e which mautches
uith c
E
Ccird)é c);
Je bBu
9natsuuclo hat tahap a-moiMhtoe
A
ACotamatOAiaAbl Londd9uucbuL
alno
honOLesl Wderddaline LoNotguuct ,
dodut Auomatimlly psDuislad
S o N a t o r t l in
mombeo.
8/JUl
Chaecacteristics of ConstIUctor:
tho public saction
hay
Jhay hould be doclasud in
lypes ot ConstoUCto
cors.
-Default Constouctog, arameteless o 2evo Axgument
Parametes ized Constuctor
Copy ConstoUCtor
-Defaut Constouctor:
Ho Conaturton Juhich
Uolault conotsuuctg ' J
deand takos aniy 0olgumonta.
the
h o 9Uogsunmél doon ho+ spacijy
Lonatuuctaou in the piusg9iam than"Compilas
conatsuctol autonatirnly
A
D
couunotas te a conatoucton.
f
Copy constructo v/s Assigoment Opeutor:
Rectangle i,2,
Rectangle 79 »vi; ||copy constouctor
.2 L; ( / assiqnment opevator, 1
-Jhe copy cenetosuctgl and tho aAaignnont
oposLtosin asto Jinod to inibinlig onc objoct to
anetho oljord.
- Dho Jmnin ditpswnca betweon thom i o that the copy
99
Jrahna
1de,
it
0Jul
class Rectangle.
int len. wid
Public:
Rectangle C); /Oefault constouc to
RectangleC int a) :/Pasameterized constructor
Rectang le Cinta. int b);/Paa.meterize constUetor
RectangleC Rectangle 5)i//copy constouctom.
3
* Dynamic Constouctogs:
nt n;
elements:"
cout <" How many
cin > :
int *p:
new int[hJ; //Here, menoty gets ollocated at -ne
allocation
This is knowrn as unamic memo
/n is 6 1900]
int p l o o 24 DytesS
1012
JO16
o20
Cout" Enter element";
tor Cint i=O; ikn ; i++)
cin >p[;
and
h e "new" opasatol ia Jnoc t o alocate mQMay
he "clelete" oposuatol io Uocl to donllocate Mortoy.
SyntacC
Rontevasiable =newr olatatype
Eccanple:
- tntP: p Dew int; '-)f
1 0*P1 new int C20;
-flaat q, new float C17-26);
SyntaL
-
delete Pointervawiable;
£cample:
-delete P
-detete [ip;
int *p-new i
COUt " Enter a numbar:
in s*p i
p end l;
"
*
cout < You enterec s
Tetuon0
iD delloco2
As a Tesponsible prooqmamme,it is out. joD
IF
memoTy hatis allocatecl manualy dynamically.
ue
elemenb in he aTVay
Find he ayaay
-
Cout"Ente no. o
elements:
fo C4o intt i
=O; i<n; it+)
cin aLiJ;
int sunm O
for Cint i=o; í2n; i+t).
SUM SUM+aLiJ;
float, mean - Float Csum/n;
cout Meamn of the elements is:" mean zencd l;
int main C)
int n
cout"Enter no. of elements"
cin>n
inta New int aLnJ;
Cead Values Ca.n)
find Min Ca.d;
find Mau Ca.);
ind Mean Can);
e t u n O; deletell a.;
.
4Cb). Deuelop a C++
pIoggm to illustwate dy namic
allo cation
S dellocation of memovy Using new and
delete
opesatoS Fo an agay of nintegeos.
Include Functions to
-Teacl he elemertss
find 5 odd
-
even
iotegers in he aoray
dis play elements. t
#include <iostmecm.
USing namespace std
void gead Values Cint al], intn)
4 CountOdd e<
"
3
void dispValue Cint atl, int n)
cout e end l;
int Main C)
fnt sum-0;
ForCinb i-O; izn itt)
SUM = SUM + Maoks LiJ.3
inb ten
i+)
fo Cint i-o; ian:
forCint j-0; jn-i-1s j+H)
LjJ > marks Lj+U)
F Cmarks
int main&)
St0Uct item .
int itenCode;
stoing name;
float poice
int itemCecde
string name;
float price
"vencll;
Cout" he details of«nee"
the
Ente items:
int main C
int n
cout " How many items ?";
cin>n;
Item *p new ItemLnJ;
Tead Items Cp.od
Find Highest rice Cp.nd
delete CIp
getuTn O