c++
c++
, r~~fRODtlt.iTI()N To · c+~
_,,.,,,_,. ,, •• ,.. ,c;•
~~; -~~ c~, ;.;. ~,/( '"f// ((4
~ ~ : . ...
e. c ½-+ wa s
Q .1 'W hat tE; C-tr-+ ? \\rh ai ar~ t~ ;tpp lk atio ns? · r:, Jan gua g ·es Ne, A,
· .
A. n$ : ("' +-t 1~ an oh,r·\ t en tt"nh' d pt {)rr, .,
1mm 1nM ·
r t..aboratoof n ,
. . .,an·w Strn , k . . rt C
"V
d\'2'Vt,;1101,'t?d hy R u~tn 1r at A~
· tht~ t",11 ly (' .1ght .w~ . C + + •1s. ('nn exte n ~1o , , l ,
· · y t iS- A n1
Jt?r$ ·
· d- , on to C a re c asses 1
( ++a c s 1 f t .
Th1..' tlWt."1~ in1p ortn nt tfwi.lit ie8 that . , 1 : din g. The se ea ures
. . . rnto r ovt r oa . _
_
htr1titnn. OVt' rl•)nd1ng i\l1d ope
. h ::irit nrop Prt1 es from
hlt' us to cn'n tt') ahs· t ract dato typi 2s , U1 c r .
ena
. dat- ' type s and sup por t po1ymo
. ttng . rph ism th us ma king
ex1~
C++ true lf object oric ntt'd lang uag e.
Ap plic atio n of C++ -
com mu nica tion
l} It is suit able edit ors, com pile rs, data ba ses,
syst ems and real life app lica tion sys tem s•
2) It allo ws to create hier arch y rela ted objects.
3) lt is able to map the real wor ld pro blem pro perly.
able .
4) C ++ pro gram s are easily mai ntai nab le and exp and
Exp lain its features .
Q.2 What are object orie nted pro gra mm ing ?
ch tha t provides a
Ans : Object orie nted pro gra mm ing is an app roa
ned memory
way of mod ular izin g pro gra ms by crea ting par titio
area for bot h data and function.
Features of OO P :
1) En1phasis is on data rath er than pro ced ure .
2) Pro gram s are divi d~d into objects.
ects are tied
3) Fun ctio ns tl;tat ope ra te on the dat a of an obj
toge ther in the data stru ctur e.
l function.
4) Data is hid den and can not be acc esse d by exte rna
functior1.
5) Objects may com mun icate wit h eac h oth er thro ugh
__,.,
6) FoUows botto1n-up approach in pro gra m des ign .
3 -l
TPS Comp.Sci.ln.troduction to C++
l ,_· - -
comp.Sci.Introduction to C++
ffS ·
~ What are tokens? What is ke :, - 2
Q,3 . YWords?
. The sn1
5 alle st ind ivid ual uni t .
;\J\ · C h f
tokens. ++ as o1low ing tokesns. in a pro gram are kno wn as
1) key wor ds 2) Identifie rs
3) con stan ts . 4) stri ngs
. 5) ope rato rs
rd
Keywo s- Key wor d is a wor d ·that is already
defined and is
rese rve d for a uni que pur pos e . C++ ho s 48
key wor d s and
stan dard C++ now has 74 key wor ds.
Example :.:. do, for, int, long, class, float, dou ble, void,
while, switch,
public etc.
i. Struc ture
i. Arra y
ii. Union
ii. Function
iii . Class
iii. Point er
iv. Enum erati on
1) ( 1{ prov1 d<-'" thrc<' bui lt -in <lo tt1 types whi ch arc! integ,•r, void
"nd f10;1 tin g.
4) Jnt11g,lr includes int eger nnd charactPr (string) whiJe floating
Iyp{~indudt;f, fl on t and do uble.
~ 1) Jn ad dit .ion h, these• dala types, C++ provides user with arrays,
1unct.1ons and polnh1rs, which arc referred as derived data types.
Q.6 Enli st the basic data types used in C++ with size of data~
terms of bytes for each.
Ans : Th<:rc are three main basic built-in data types used in C++ viz.
integral type, void and floating type.
Built-in type
or
Basic data type
inf 2 0 to 65535
4
-38 I~ ~
screen.
(ii)
Extraction operator : The operator "' >> ,, is called as extraction
operator. It is also called as 'get from' Qperator. It extracts or
~kes the value from keyboard and assigns it to a variable on
its .right
T · use d 1n
. • It 1s ·
· input statement 1n · >> a;
· C· ++. E .g. c1n
. his instruction will extract a value from keyboard and assign
it to the variable a. .
~ -++ allows us to redefine insertion and extraction operators
y overloading them .
n n voi data t
Aflt
f l ti
n( t 1 ( < 1d) ,
-·-----
Q 9 What att operaton in C++? Explain
AN A , p( t 1 a ymbol th t t ll th mput r
matheman al and logi al manipulation. Oper t r r
progr-.am to marupulate data and vanable C++ h
operators They are classified into following categ
A thmetie Operators 2. Relational Operat r
1..ogkal Operators 4. Assignment Operator
5 Increment and Decrement Operators
Q:>nditional Operators 7. Bitwise Operator
Speaal Operator of Ctt
l Anlhmetic Operators :
++ ~rforms its numerical calculations by means
arithmeti operators as follows:
Qperator• Meaning
+ Add•tion or u.nary plus
Substract,on or unary minu
Multipl att
D
equ
· . Introductio n to C++
coJ11P.5ci. ~======:=:;::==;r==::::=======:============::::
~==
====
1::::
. Grea ter tha n or equ al to
Equ a l to
N ot equal to
J
_ _ : , _ - - - ~-
Logical Operator s :-
The operators which are used to perform logic;;il operation f;twh
as logical AND, logical OR and logica l NOT are ca ll E-!d }ogka l
operators.
These are as follow s-
,.......
Operator · Meaning
- && Logical AND
II Logical OR
I Logic NOT
4) Assignme nt Operator :-
Assignmen t Operators are used .to assign the result of an
expression or constant to a variable. Assignmen t op erator in
C++ 1s equal sign(==). In addition to this operator, C++ also
includes following composite operator.
Op erator Meanin g
+= X + = ymeans x = x+y
- - X - ='y means X = X - y
*= X * = y means x = x * y
I= X I = y means x = x/y
5) -- %= x %= y means X = X % y
Increment and Decremen t operators :-
C++ has two unary operators called ·incren1ent and d ecrem ent
operators. These are very useful operators used for ad d ing on e
~ ubstracting one from variable. ·
r--2,perator Meaning
++
Increm ent Ex.b++ m eans b = b +1
Decre1nen t ·Ex b -- means b = b - 1
_____ J.7
TPS Comp.Sri .lntrodttdion to (H
~ - - -- --- -- --two- vcrs1.0n -prevns,.on and
-------~
pn~t
Earh nf thest' op('ro tors ha~
vers ion .
Extunpl\) '-'f prl' Vl"rsion is
if x 50 and y ,., ➔ ·+ .r Own y valu e wi ll be .1 J
I>!:
.• pr\
on opert1t ,r n ,,
1 opt:-rJ.t)
•1 t a1
• qne
'-
block \\ithin another block.
~ 1ler .:i. , anable !$ ..~eclar~d in program scope extend fr
th€' ~ 1 int ct dedaratJ.on till the end of the block in \\ h1ch ,t 1
&iro.
The 5'1Gle ,-ariable nan1e can 'be used to have different meaning
ilifurent bl0e'5.
(tlnsid€r the iolluwing segrnent of program.
{
int X = 10;
int X = 1·
I Block 2 Block 1
}
liere Block 2 is contained in Block 1. Note that declaration of
a V~riable in an inner block hides the declaration of the same
61 \anable in an outer block.
;ope resolution operator is used to uncover a hidden variable.
takes the form
e.g.
I: variable name I
\
\ t l t \l I \ 1
t i.'.tb., ·:- H)
Q 11 \\hat ,tre backsla!--h constants in C++?
l\n~ : t' backslash (\)is used h.) 1.knott.' non-graphic ch.uactcrs and
~,thl'f ~pt:{'ial dHtta~t~rs f1..'r .i spt.'dflc opl'r,1tions. They are not
printable char.1cter but they Mt' uSt.'d in output functions. The
ist of backslash dldrach~r are as follows.
Code Meaning
\a alert a bell
\n newline
\t horizontal tab
\b backspace
\r carria~ return
\f form feed
\v vertical tab
\\ back slash
\' single quote
\0 null character
\? Question mark
trod 3 -10
n structure of C ++ program.
c++ pr gram n · Th sections may be placed
ttl separate d mp d independently or
1ouitly
fhe cJa d
ara n a header file and the
of member f .................__ r e Finally the 11\illlJi
tu which includes;
tw f
clo -
contents
tions
diner cout
beadet'file
~
value in right justified form. Ans : Overloading means assigning different meaning to an operation
Q,16 ~n C++ va_rl_able can be declared anywhere in the scope. Wful · depending on the context. C++ permits overloading of0 operators
1s the s1gruficance o·f this feature ? thus allow1ng as to assign multiple meanings t~ 1"'"'.:,
Ans : C~ allows the declaration of a variable anywhere in the SCoP'· For exampie- The opera tor • when applied to a pamte•
But itvan
is .1Iso'
~ means that a variable can be declared right at the place ~ gives the value pointed . to1 by
h ointer
. tt e Pnurnbers.
· The numb,er
1ts first use. This makes the program much easier to write and
commonly used for rnultip.d ymg wo of operation.
the nature .
reduces the errors. and type of operands dea e od examples of
It a_Iso makes the program easier io understand because th< << and >> are go
The input/output operator taternent cou t << 75.86 ;
vanabJes are declared in the contest of the1r
. use. Th 5
operator overloading. . us
Example- displays double type value and
void main()
cout<<" well done " i
( float x;_ I/declaration
displays char value.
float sum=O ,•
for(int i=l ; i<S; i++) //declaration of i
S l'omp.Sci.lntn1du('tl\m h• Ct 1
1
" l •,.m11 •1 I l11l 1o d1 11 !1<, 11 I
111 J., 111,,
Fl)Tt.•:x,m1plt'· \Vt' l'<lll d,,ruw • 11p, tll ll11 t11 ,1tltl l\\'11 :-.11111 ti 11 ·s
1
rd1•1t•t11.:l' v 1 l
sc<,pt.· rt.'St'llutilm op,'r,11or(::) Q,20 Wh,ll ,u,• , r ,1 1•Ioli ? J
sizl' opcr.1tor (siw l\t) .,,~ • l{l'lt•r1•11lv vn,i,1b1t , 1)11 1v11I Xpltln
I' . ' lit •r 1
1 1t-t11wd vnri 11,ft,
n hv ,r
.19 What are l)·pt.'S ot const.mts "! ~xvi.tin
s : Ther~ .ire thl'l't' h pt'S \,( cnnst.mt:--• Ill ring t'tH1st,mts, mun,•tii :,;ynt.1x 111 n•tt,rt•nn• v' ,ri ,111) I''
d,Jl11 typl' & rl'i1•n•nt 1,
constants and ch.1r,1ctt'r C'lmst,mt:-. no1m,, v 1r1 hl1
1) String constants - A ~tl'in~ const,mls is .1 St'l{Ul'nct'oJ H\· 11011! tot.ti 100
tlnnl l'{i; Slllll fol.I!;
alphan\1mt'rIC ch.1r.1ctcrs l.'nclt,st'd in doubll' qunt,1tiu~
marks whost' maximum length 1s 2-55 ch,1ractt'rs. llollnwh1 totnl i~ ,l tlo,1l typt• v,in nhl,• th I h
• " ,1 ' ,1!r ,1dy be r
arc valid string const,mts. "Tht' MmlL l~s.2000.00 11 ls the ,lltl rn,1hvt• n,,m1, ,It-t·I ,r, 1
1
'
' t l) rq1r1 nt th v
Following an' Slime of invalid string constnnts. N1>w tlw st,1tcmcnt rout .~ lot,il ,
nnd cnut,.._ sum ,·
HydrabJd- Charadt'r are not t'ndost'd in duuhk t1uottttio~
My name i~ : dosed double qw)tatinn is missing linth print vnluv 100 Tlw ·sl•ilt•m.,, ... 111otaI tot,,1 H
2) Numeri~ !Constants - Numeric cnnst,1nts ol'l' pnsitiw ot wm
dwngt' V,llUt.' of blHh tot,,1 and 11um to 110 '
negative numbers. There .ire twn typt'S nf num\'l'!Q Thus ,1ssignnwnt sum O;
constanr5 integer constants and tlnttting point t'tmslan~ will d'1.,Hlg1· the v.1lt1t' of both v,ui.1blt.! to 11 ro
a) lntege{'rnn~tants- fntq~cr constants d\) nt,t cont,,in ckcirn11 Q.21 What nrt• cnumcr.ltt•d data type ?
poinf1 '. Vari,1bles can be dedart•d as intq~t'fS in lht' fotlowi,~ Ans : An t'nnnwr,lll'd d,ll,l typt• is ,moltwr us1r defined t pi: wh h
war,s. providl'S ,\ w.1y for ,1tt,1d1in~ n,111\l's h>number~ lh num
Int#, y; kt')'\l\1 ord ,H1ltHH.1!k,1lly 1•1111m1 1t\ltc~ ,I hst 111 worcis by 1 ,1 mng
loryg irtt 1x, y l'lwm v,1l11t'S 0, I, 2 ,ind so ,m.
The k~ywor<l int stands for tlw intq,,,l'r' data typt•s in Cl synt,l\ or l'tnnn st,1h'tllt't1l is
and its size is l 6 bits nnd tangt' if. - 3276~ In J':!'i'ht l'ntm1 ~h,ipt• ktrdt•, squ.in•, tri.111!;!1•1 ;
The!keyword long int stands fur long inh•gpr ,rnd its 1:11;. t'ntm1 l'olnu r In•d, hh1,·, r,u'l'll) ,
is 4 'pytes ,md range is • .21474&.164?1 to 21 t7•l8J6•! p11sltil)11 {oft 1111I ;
t'l\111n
b) Flt>dting point n,nstnnts- Posifat• or rwg.1tlw numln•r~ •'" \lso w,, c,111 lh•d,tn• t•111t1 11 ,is fpllows
rQ'p,resented in t'Xponential fonn. ,•n11m 1·t1ltn1rlrl tl. hlu,• I gn•,•n I:!) i
1 "
. I ' 1~ ~ 111d i-;rn•n h,111 n v.i 1IIIJ,
There are thre~ types of flt)ating point const,mt (hl,ll H,•w t'l•d is O11, , dl'f,1till h m
.
•
ii ,m111ymotts ciu1111-i ,t
':l follow~
double and long doubll'. Flont siic is 4 hytt>, doubll' si:1'~ C't I .ll~ll l'l'll\11111 tlll' l'l't', 1110111 '