0% found this document useful (0 votes)
11 views

c++

Uploaded by

vedangtiwari31
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

c++

Uploaded by

vedangtiwari31
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

:;/,,,<:?::~;~~:: :-".·':'-r,,-,•_··:··:"):~~ -,.>'~~: ... , ;-,t,/ ;IW;V /l'N' ' '"/////2 .

, 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.

Q. 4 What are iden tifie r ? Explain its rule.


Ans : Identifiers is the nam e of variables, function s, arra
ys and classes.
Each lang uag e has its own rules for nam ing
identifiers.
Rules of Iden tifie rs are:-
1) Onl y alph abe tic cha ract ers, digi ts and
und ersc or, ·s are
perm itte d.
2) The nam e can not star t with a digit.
3) Upp erca se and low erca se letters are distinct
.
4) A dec lare d key wor d can not be use d ·as a
variable name.
Q.5 What are diff eren t da,ta types in C++?
Ans : 1) Qat a type s in C++ are sho wn in fig~re belo w
:_

C++ data type s

llser d"fin cd type Bui.It in Type Deri vcd type

i. Struc ture
i. Arra y
ii. Union
ii. Function
iii . Class
iii. Point er
iv. Enum erati on

Integ ral type float ing type

Char Float do uble


TPS C.omp.Sci .lntrodudion t- o ( -.. ♦ 3~3
2J
- -----------------
(-t 1- allowi-i user to crea te new abst ract data types, whkh can
b<)ha V(' lik P r.my buil t-in data type. These are called use r.
ddirwd dnt a tyfWh. Tlwr-.; P include strudurr, union, class and
1 n omc,!ra f inn .
1

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

Integral type Void

int char float double

i) Integral data type :


lt includes integer (int) and character (char).
An int variable requires 2 bytes to store, while a character
varjable requires ] byte.
Integer variables arc al so of two types : (a) short int and (b)
Jong int. Long integer requires 4 bytes, while short integer
requfreg 2 bytci;.
fj ) Void data type :
\-oid data type is u84!d :
{a) to specify the retu rn type of a functi on when i,t is not
retur.ni ng any value.
~~ ... \.. ' . ·- ;:ft.~jtJf/~' ~,;
~~
f''

.
Introdu ction to C + +
p SCl· ·~ - - - -- - - - - ~ ·--- -
·A-6 cof11 · . 1 dicate an e n1pty argn n-wn t iist to <1 fu n f"' f t(ln
I' ) tO JJ .
(b declare generi c pointer-~ .
(d to · •
ting type .
. ,j j) ftoa . g type vana-, · of
· · l 1t'S· a1c- two typ<' S; fl o .:1t· and d .,uh 1f' I\
,
1 float1n .
_variable rt>c.pttrf'~ 4 byt(' ~, \.Vhilc- d(lt1hl <· r~·qo 1r,"'1 Ii< b•1t-- ,-;
f}L)ctl
~tore in memory.
to -~ . , another kind of double na111<'l y fqng d o ublP. wh,<" h
rhere JS •
. " 1() byh?S to store 1n n1en1ory.
rec!tnrtS . uo,,v ing table s hows a ll ba sk data type!=l, 9jze t1nd r .-J nge
The to -··----- - - -- -
Byte s Range I
'5r. No. Type . -1
char (Signed char) 1 - J 28 to 127
1
1 o to 255 I
~~.:.:::.=::..-=.:.:.-----t--::-- ~=-=-=-:-:---:::~:-=-- -,
int (short int or signed int)2 -32768 to 32767 1

inf 2 0 to 65535
4
-38 I~ ~

6 double 8 1.7 X 10·308 to 1~7 X : ci


4932 4932
.. long double 10 3.4 X 10 1.1 x 10
to____ 1
L ,;~_J.._-~ ---------- ---...._______ ___J

Q.7 Explain insertion and extraction operators in C++.


Ans :(i) Insertion operator: The operator << " is called as insertion
u

operator. .It is also called as uput to" OP.erator. It inserts the


contents of the variables on its right to the object on its left.
It is generally _used in output statement in C++.
E.g. (i) Cout <<a; . . (ii) Cout << programJ/;
0

In first example, the value of variable 'a' is printed on screen/


while in second example the word program" is printed on
0

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>!:

E, amp k of post version is


if .,·~ SOand y • x ++ then y ~ 50 and x will be 51
6) Conditi onal Opera tors :-
or
C++ includes vt~ry spec ial opera tor ca ll ed the tern ary
cond itional operator. It is called ternary beca use it uses three
expressio ns. Its general format is
expressio n 1 ? expression 2 : expr~ssion 3
Ex - .x = y > 5 ? 50 : 80
It means x is assig ned the value 50 if y is grea ter than 5, otherwise
x is assigned 80.
7) Bitwise Operators :-
s are to
TI1ese are certain situations wher e in bitwise oper ation
to
be performed. These perm its the prog ramm er to access and
with
man ipula te indiv idua l bits. Bitwise oper ators can be used
char and int data types.
Operator Meaning
~ Bitwise One's com plem ent
& Bitwise AND
I Bitwise OR
I\ Bitwise x OR
>> Bitwise right shift

. 8) << Betwise left shift


Spec ial oper ator s of C+~ :-
New oper ators in C++ are
Scope resol ution oper ator
.. *
Pointer to mem ber declarator
poin ter to mem ber oper ator
pointer to member oper ator *

mem ory release oper ator delete


line feed oper ator endl
. ..,._ C+

.• pr\
on opert1t ,r n ,,
1 opt:-rJ.t)

,_ 0 note on scop(' resolution operator

tt' e atur · , C'dlkd n~ -:cope resolutm 1 I


')t'

C . _ -•. b LK'k $tn1ctured lath:.uagp Le a C


~ Q

•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

,HI \nth,\ ,l,1

t.' '-\\ tput ,, ll l~ il~ tollm,"' .

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

inettuat!:Cl at the beginning

-1ifillifid 41.-ID,_..llt the data


ti . . . . . . . and
I cin....>x,
c;um = '>um + x
setw.endl manipulator, when used in an output _staternenl
The
causes a linefeed to be inserted. It has same effect as usmg new)j" float average , //d e lurati
average. sum/i. on
character " \ n ". ·
cout<<average ;,
For example-
cout<<" m= ''<< m << endl ;
cout<<" n= " << n << endl ; The only disadvantage of th·1s sty e f
cannot see at a glance all th U\;;l.]<>1".-,h,.~
If values of m and n are 2597 and 14 then output of above __ e vanabe<; i
Q.17 What• do you mean by dyna rruc . .wtializati
program is Give an example. on
m = 2597
.AnS : C ++ permits initialisation of th e vanab le at un
n == 14
Jn above output numbers are left justified. referred .as dynamic
· initialization. ThIS means at
To make output right justified setw manipulator is used as declaration and initialization of a vana b e 0
simultaneously at the place where the vanab e 1
follows-
cout<<" m= "<< setw(S) << m << endl ; first time.
cout<<"n= " <<setw(S) << n << endl ; For example -
float average ; //declare where it Le; necessary
output will be
average= sum/i;
m == 2597 can be combined into a single statement
n= 14 float average == sum/i ; //initialize dynamically at nm mne
The manipulator setw (5) specifies a field width 5 for printing
Q.18 Whal do you ,nean by operator overloading? E,pbin,

~
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

• I ht 0 C'h11111d t' 1 <'0 11nt1111 1 l


l)bJt.'dS. .
J\ ch.11 111 fr•r l t' j)l l' 1p11f,•d
Fr1ll0win~ t)pt' i'i\!(11':s l .nnll,t h· ll\ c1 h1,1dt•d W1t11111
11111 Rl 1111t If , 1 , ,,r II) 11
111 .,1
111 I
memtwr- ,ll'l'\'88 np~rnll)r" \ ,11nd.~ ) ·A' I YI! If I ( '
,n nh•· I
, .i , ,, t I '·' I '
'' r11
condith)rt,,1 upt.'l\lhH (? ) f\i 1r

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 '

8 bytes and 10 bytes H'ttuired t't)r long double. t•1111mh•II, ,)UI ;

You might also like