The document discusses various aspects of functions and constructors in C++, including inline functions, static data members, and friend functions. It provides syntax examples and explanations for declaring and using these features within classes. Additionally, it touches on parameterized constructors and their role in initializing class objects.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
11 views27 pages
Chapter No-02. Functions and Constructors.
The document discusses various aspects of functions and constructors in C++, including inline functions, static data members, and friend functions. It provides syntax examples and explanations for declaring and using these features within classes. Additionally, it touches on parameterized constructors and their role in initializing class objects.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 27
Unitas: Functions 4 construdbrs .
¥* Tnitne funchon -
~ C44 Pnting uncbion 1S Concept tratt 1S Commonly
Used with classes .
~ TE function 78 Iniine ithe compjieg places aoPy
of code of that function ad each Point where
the function TS caned Compile ctime
~ A functlon definition in wadass defini hon
iSan inline function definition, even without
dhe use of the inline keyword .
~ To ntine a function, place the keyword fnline
before the function name and define tne-funchisn
bef ove ny calls Are made fo the Lunckion
+ Declating inline function.
Syntax :
elass class name
/l othey members declaration
Peblic s
Pnline -eturn tyre function Mame
C dist of parameters )
i
/I body of tnline tunchion
{
I otheé member qealaection
i(yc
Xm pre .
Ttnelude
clas ¢ Nomber
S Pubtic:
h>
flout add Sub mul, dy}
Fowl YS
Pubic :
void accept >
Void calewate cd’,
void displyc);
4
§ni¥ne Void num
coute< entes tre value of ody
cind > >t ery)
bes ‘accept ()
inline void numbet ss cwrulete Od
{ add = +9}
Sub = 7-95
mule ak YS
FF
Yntine Votd numbes 2, diseleyo
i cou} 22" add"
e = ct i
S
f @ Include <-> > conto-h>
: class test:
r ee
: Tat no, ee
te State int count 5 aaa at
{ Public *.
__void getved Cint) 3 a :
r nS. Void disptount cvoid)s
2
void test >: getval Cint x)
No ~ x, ee
44" _numbee = "dp ylueot coun 6
ti) Jetveal (100)3 a
_ to. geval (2100)5 ; eet ue
4a. served (300) = Z
the Aispeourt ( Direct ie =
Ho. dispcounkOos _ Bae
43: dispeount 5 it ce il
a sehen iO) asi Heer HMIEE Eta [pas
tern) ~ ebeb?
4 — fe ae
; er: efaclass
ee imctiontens Habe a)
aecessipie Ff we dont have on
- “Scope, re. ee solu}
“include Lconios h> [ :
cla $8 je St
int noy = ! eee
Static tnt Count, =
Pes a —
pan! Set no (void);
disp no ¢ C void)
__ Static void disPeount ¢void )
y| = yu
Afevente a different
_ int meatn c)
le L
Fest ti, t23
+s setnoO))
| ta. Serno0)3
iy
a dispeount (35
test 133 7
hos Serno)
test disteount 0% tountvedue inevesey
tLe dis?no Os __ oP
fo-disPnow', a ey
$3: dispnocy3 ae bi a
5 7 a obpect no 7
SSE
Seo 7
Fad_ =| but biend fy net
fend Function os Fan
Fiend oPadass)
MH tan not arcess
glen: pai a
Fiend chien tyre in name ¢e)ass “fy,
Here hwo differen] -Clacg were Shate thee
"Ranchos - i
+e—— -- —.
tl Acommony ince can be made Frendiy
with Muttipie classes & function is auasd >
fe access Private dala rae
That fanctfon Need not not: to be me mbee,
oF any of thedass =, fo
| Syntae ik Eo
sS name.
ee iclaco..cla
aweot in the” Sere of the class. fowhith ib
has declate has fF jend
Invoked life nogmed Function
has FO use of Fab “name” dot i
ieoch membee .
Ct Can be decloted ejthee In public ‘0 ® The peivale
Poe F of aclaSS without affecting the ae
a usualy it has object cae orqument i i
J “| Aeee
nn mee
Saceceecsaepaed it
seaae =
}
Write a C++ program (o find smallest number from two numbers
using friend function. (Hint: use two classes).
(Note: Any other correct logic shall be considered)
#include
#include
| | class class2; _
_—| class class! i
If -
int nol;
public:
“| void getl() eae
: : tind
cout<<"Enter number 1:"; az
cin>>nol;
} me
| friend void smallest(class1 nol ,class2 no2); I+
Ths ;
| class class2
t
int no2;
public:
| void get20)
cout<<"Enter number 2:";
cin>>no2;
}
friend void smallest(class! nol class? no2);
void smallest(class! c1,class2 ¢2)
_| iffet.no1
Binclude Coonioh>
class Constuchot Test
Pabl tc :
Conshaete @ Test C)
cout ee" Con shrug toe Cs waked
~—
45
ind maine)
: tonshucloey +ob
vcr ns a a
4Tyres, of constrcloe
“Treve ave “Lee types of Constructo é
[iss pean atop
| Charamees). (const clasSrar
SS fold bj)
|
Defourt consbuctoe
J eon Consbietoe js the Construclo€ which des
not dake on eee any aryument tt has
[be Freamelt? in |
= | eae closS tlessname
| “Publics
classname ()
4
membe é data; .
membet fonction?
|
|
4
t o_o
|O.24 |esak
aVinelude Lost ecm. hy
include
class Shay *
4
Public:
Show C )
"Ne
Coat 2c" show Constructo! 5
Vold main.
Show Ob ()5
—Jtteh 3
Explain Par Povameleeire d Cons buco’ . :
=). A Constructoe which hos Porameteaired fs cated
foramelerize d Conshuclog
_|Constouctoe “Jsused Provide | different i
value to distinct abject ee
_AMindude ¢fosteamh> a
class add
Publi ¢ + 7
add Cinta, int b) Passing pasame}ez .
I + : —_——S ” ~po9.96
LOOPY Consteados
A ey fonsfeudo@ Ls used {odectate and
\ an ¢ Niet from cmothet obed :
nitlig&
ht cont :
“oN I tery the data oCane obied fioro cno thes
lobfect d ,
|
|
|
|
|
|
| ten
class shudeap
t
Public”
Student (sfdeny fob ) [Lory condsbud >
i
5
a
ky ;
Shidend $4) Sdudent C student oh)
i
4
Coul] COPY Constuctot
Syptace =
classname aeged _ object (souece off’) 5
class. Mame Aarge} Hed & youre obs
f=
SWderd barged > (source),
Student favre: source.208 foe
at include X fosbeearn sh >
Class Shidenb
\
j ink tolls
f& Ink maek3
t
Public
Student Ciatm, tnt n)
| foll-m)
maels 05
Student C studerd §t)3
Vol d show — data ()
tou <<"#on no! “<< toll Os
cout acs \n mae % Ne maakss
j
Studend <% student Cshident €b).cey
4. : —__ tonshudte!e
de Fine.
ts
: tol = be tol]
. mae = . :
4 votd mary C) Bema;
— Cout<<™ |» Patameterized conshuviot. o/P € \nl
| Studemt € (60, a6}
Cout <<” \n\n COPY Conshichoe output stae Ir)
| Studerd stu cr} swory Cons
| Sty + Shao. data co! —
gether, 7[int toll 003
4
0, le
Wullire Consteudee In eloss
atenlnctee Construtto® essentiaty have [he Same
Name Cnome of dass) And dif (e sen
No ofcargumen{ :
A Comshucto® Vs canted depend?
epending upon th °
of any type o ( chajume nd. Pass eq fon hemmed
Toshile Ctealing the Objech *guinen} must be
passed 10 let Compile & Know which tonshucto &
needs to be Canhed ‘
arciude ~foshecam -h>
cass Shident?
staing names, byihi
chdent O ~— de Faull cmspuctor —
__ touno =o
tolino=e5
“near = sn) a
ya diaries =2,048
—|
eat : : eral
———} toeth el pope << tel) £6
| Cout<<"” name" ce name<< @nbi-
Rs
_Notd meu nc)
Jf Shdent sis
| _Studenh s2@ yk, "ages
ee Sid isrlanay \
. | Sa dP sPloy Cd)
lth C9)
The_actya) fotcumedee, > per eneanr
Bees with deCiay agency shel
~—VBEIES ei th defauul Rreameteg \desfy
coud <<"
Sma nt Collen 10! 5
hs}
Void display O
“void main C)
__ Student $2 Cut);
2 “OS :
Shudend 82C12,"PaRs'):
BLAIS Play (9 |
<6: disrtay CD 5
seth O}
yon cOlth Conshuchot ion de faut
pea
cout <<” oli nd < tol OKendi
name" ce name <