Вы находитесь на странице: 1из 17

Notes for Class XI computer science

Q1a) Name the header files that shall be needed for the following code:
void main( )
{
char String[ ] = .eace.!
co"t ## setw($)##String!
%

Ans) iomani&.h
iostream.h

b) '(&lain different t)&es of errors in *++.
,ns ,n error or -a b"g./ is an)thing in the code that &revent a &rogram from com&iling and r"nning
correctl). 0here are three t)&es of errors:1
1. com&ile time errors
$. r"n time errors
2. logical errors
Compile time Errors:- 'rrors that occ"rs d"ring com&ile1time/ are com&ile1time errors. 3hen a &rogram
com&iles its so"rce code is chec4ed for whether it follows the &rogramming lang"age5s r"les or not.
0)&es of com&ile1time errors are:1
i. S)nta( errors
ii. Semantic error
Syntax Errors: S)nta( errors occ"r when r"les of a &rogramming lang"ages (s)nta() is mis"sed.
i.e. when a grammatical r"le of *++ is violated.
'g (i) c=a+b
6n this statement/ since there is no semicolon at the end of the statement/ there will occ"r a s)nta( error.
(ii)cin##a!
6n this statement/ since stream insertion o&erator (##) has given instead of stream e(traction
o&eration(77)/ there will occ"rs a s)nta( error.
Semantic Errors: Semantic refer to the set of r"les which give menaing of a statement. Semantic errors
occ"rs when statements are not meaningf"l.
'g : 8+9=:!
3ill res"lt in sematic eror as an e(&ression cannot come on the left side of an assignment statement.
Run Time Errors: 'rrors that occ"r d"ring the e(ec"tion of a &rogram are called as r"n time errors.
6t is ca"sed of some illegal o&eration ta4ing &lace or in availabilit) of desired or re;"ired
conditions for the e(ec"tion of the &rogram. <or instance/ if a &rogram is tr)ing to o&en a file
which does not e(ist or it co"ld not be o&ened/ it res"lts into an e(ec"tion error. Similarl)/ if eno"gh
memor) is not available or an e(&ression is tr)ing to divide a n"mber b) =ero are r"n1time errors.
'g: >ivision b) =ero.
c=a?b !
@ser will give the val"es of a and b at the time of &rogram e(ec"tion. 6f the val"e of b as A/ then
division b) =ero/ i.e. a r"n time error occ"rs.

Logical Error: , logical error is that error which ca"ses a &rogram to &rod"ce incorrect or
"ndesired o"t&"t. ,n incorrectl) im&lemented algorithm or "se of a variable
before its initiali=ation/ or "nmar4ed end for a loo&/ or wrong &arameters &assed are ca"ses
logical errors. 0hese m"st be handled caref"ll).
<or instance/ if we are tr)ing to &rint the table of a n"mber B and if we sa)
co"nter=1!
while(co"nter7C)
{
co"t##nDco"nter!
co"nter=co"nter+1!
%
Eere the loo& wo"ld not be e(ec"ted even once as the condition (co"nter7C) is not f"lfilled at all.
0herefore/ no o"t&"t will be &rod"ced. S"ch an error is logical error.
c) >ifferentiate between a *all b) Fal"e and *all b) Geference/ giving s"itable e(am&les of each.

Ans: Call y !alue: 6n call b) val"e method/ the called f"nction creates a new set of variables and co&ies
the val"es of arg"ments into them. 0he f"nction does not have access to the original variables (act"al
&arameters) and can onl) wor4 on the co&ies of val"es it created. assing arg"ments b) val"e is "sef"l
when the original val"es are not to be modified.
6n call y reference method/ a reference to the act"al arg"ment (original variable) is &assed to
the called f"nction. (Geference is an alias for a &redefined variable. i.e. the same variable val"e can
be accessed b) an) of the two names: the original variables name and the reference name.) 0h"s/ in
call b) reference method/ the changes are reflected bac4 to the original val"es. 0he call b)
reference method is "sef"l in
sit"ations where the val"es of the original variables are to be changed "sing a f"nction.
"rogram to illustrate t#e call y !alue met#o$ of function in!o%ing:
Hincl"de#iostream.h7
Hincl"de#conio.h7
int change(int)!
void main( )
{ clrscr( )!
int orig=1A!
co"t##.In0he original val"e is.##orig##.In.!
co"t##.InGet"rn val"e of f"nction change()is.##change(orig)##.In.!
co"t##.In0he val"e after f"nction change() is over.##orig##.In.!
getch()! %
int change(int d"&licate)
{
d"&licate=$A!
ret"rn d"&licate!
%
Ans: J"t&"t:
0he original val"e is 1A
Get"rn val"e of f"nction change() is $A
0he val"e after f"nction change() is over 1A
"rogram to illustrate t#e call y Reference met#o$ of
function in!o%ing:
Hincl"de#iostream.h7
Hincl"de#conio.h7
int change(intK)!
void main( )
{ clrscr( )!
int orig=1A!
co"t##.In0he original val"e is.##orig##.In.!
co"t##.InGet"rn val"e of f"nction change()is.##change(orig)##.In.!
co"t##.In0he val"e after f"nction change() is over.##orig##.In.!
getch()!
%
int change(int Kd"&licate)
{
d"&licate=$A!
ret"rn d"&licate!
%
&utput:
0he original val"e is 1A
Get"rn val"e of f"nction change() is $A
0he val"e after f"nction change() is over $A
d) 3hat is the difference between global variables and local variablesL Mive an e(am&le to ill"strate the
same.
Ans: 0he local variables are the variables defined within an) f"nction (or bloc4) and are hence
accessible onl) within the bloc4 in which the) are declared. 6n contrast to local variables/ variables
declared o"tside of all the f"nctions in a &rogram are called global variables. 0hese variables are
defined o"tside of an) f"nction/ so the) are accessible to all f"nctions. 0hese f"nctions &erform
vario"s o&erations on the data. 0he) are also 4nown as '(ternal Fariables.
'g: Hincl"de#iostream.h7
int a/b!
void main()
{
float f!
111!
111!
%
6n the above &rogram segment/ a and b are global variables/ we can access a and b from an) f"nction. f
is local variable to f"nction main( )/ we can access f from main( ) onl).
e) 3h) main( ) f"nction is so s&ecial. Mive two reasonsL
Ans: '(ec"tion of the &rogram starts and ends at main( ). 0he main( ) is the driver f"nction of the
&rogram. 6f it is not &resent in a &rogram/ no e(ec"tion can ta4e &lace.
3hat is the difference between Hdefine and constL '(&lain with s"itable e(am&le.

Ans: 3hile the) both serve a similar &"r&ose/ Hdefine and const act differentl). 3hen "sing Hdefine the
identifier gets re&laced b) the s&ecified val"e b) the com&iler/ before the code is t"rned into binar).
0his means that the com&iler ma4es the s"bstit"tion when )o" com&ile the a&&lication.
'g: Hdefine n"mber 1AA
6n this case ever) instance of .n"mber. will be re&laced b) the act"al n"mber 1AA in )o"r code/ and
this means the final com&iled &rogram will have the n"mber 1AA (in binar)).

'$efine (it# $ifferent types of $ata:

N0he Hdefine &re&rocessor allows " s to define s)mbolic names and constants.
'g: Hdefine 6 2.1O1BP
Global variable
Local
variable
0he Hdefine allows )o" to ma4e te(t s"bstit"tions before com&iling the &rogram.
'g: Hdefine Q,8 RA
Sefore com&ilation/ if the *++ &re&rocessor finds Q,8 as
one word/ in the so"rce code/ it re&laces it with the n"mber RA.
0he Hdefine &re&rocessor can be "sed in the creation of macros (code s"bstit"tion).
'g: Hdefine SQ@,G'(() (D(
Sefore com&ilation/ if the *++ &re&rocessor finds
SQ@,G'(()/ where ( is an) val"e in the so"rce code/ it re&laces it with its s;"are (ie (D().
Eere a macro s"bstit"tes te(t onl)! 6t does not chec4 for data t)&es. Jn the other hand/ when we "se
const and the a&&lication r"ns/ memor) is allocated for the constant and the val"e gets
re&laced when the a&&lication is r"n.
S)nta(: const t)&e variableTname=val"e!
'g: const int a=1A!
0he val"e of a constant is fi(ed and in the above e(am&le/ the val"e for a in entire &rogram
is 1A onl). 9o" cannot change the val"e of a/ since it is declared as constant.

f) )ifference et(een '$efine an$ const in $eclaration:*
+*'$efine: Hdefine s)mbolicTconstant val"e.
Eg: Hdefine n"mber 1AA ??No semicolon /no e;"al to s)mbol.
,*const: const t)&e variableTname=val"e!
Eg: const n"mber=1AA! ??Semicolon/ e;"al to s)mbol.
g) 3hat is the &"r&ose of "sing a t)&edef command in *++L '(&lain with s"itable e(am&le.
Ans: *++ allows )o" to define e(&licitl) new data t)&e names b) "sing the 4e)word t)&edef.
@sing t)&edef does not act"all) create a new data class/ rather it defines a new name for an
e(isting t)&e. 0his can increase the &ortabilit) of a &rogram as onl) the t)&edef statements wo"ld have to
be changed. 0)&edef ma4es )o"r code easier to read and "nderstand. @sing t)&edef can also aid in
self doc"menting )o"r code b) allowing descri&tive names for the standard data t)&es.
0he s)nta( of the t)&edef statement is
t)&edef t)&e name!
3here t)&e is an) *++ data t)&e and name is the new name for this t)&e. 0his defines another name
for the standard t)&e of *++. <or e(am&le/ )o" co"ld create a new name for float val"es b) "sing the
following statement:
t)&edef float amo"nt!
0his statement tells the com&iler to recogni=e amo"nt as an alternative name for float. Now )o"
co"ld create float variables "sing amo"nt.
amo"nt loan/ saving/ installment!
@sing t)&edef does not re&lace the standard *++ data t)&e name with the new name/ rather the new
name is in addition to the e(isting name. 9o" still can create float variables "sing float.
Jnce a new name has been defined b) t)&edef/ it can be "sed as a t)&e for another t)&edef also.
'g: t)&edef amo"nt mone)!
Now/ this statement tells the com&iler to recogni=e mone) as another name for amo"nt/ which itself is
another name for float. 0)&edef does not create an) new data t)&es rather &rovides an alternative
name for standard t)&es. Geference &rovides an alias name for a variable and t)&edef &rovides
an alias name for a data t)&e.

h) 6ll"strate the "se of Hdefine in *++ to define a macro.

Ans: 0he Hdefine &re&rocessor can be "sed in the creation of macros (code s"bstit"tion).
'g: Hdefine SQ@,G'(() (D(
Sefore com&ilation/ if the *++ &re&rocessor finds SQ@,G'(()/

where ( is an) val"e in the so"rce code/ it re&laces it with its s;"are (ie (D(). Eere a macro s"bstit"tes
te(t onl)! 6t does not chec4 for data t)&es.

i) 3hat is &ol)mor&hismL Mive an e(am&le in * ++ to show its im&lementation in *++.
Ans:ol)mor&hism is the attrib"te that allows one interface to be "sed with different sit"ation. *++
im&lements &ol)mor&hism thro"gh virt"al f"nctions/ thro"gh overloaded f"nctions and
overloaded o&erators. , virt"al f"nction is "sed to s&ecif) the interface in abstract class/ b"t its
im&lementation details are made available b) the concrete class(es).
,n overloaded f"nction refers to a f"nction having (one name and) more than one distinct
meanings. Similarl)/ when two or more distinct meanings are defined for an o&erator/ it is said to be
an .overloaded o&erator..6t is the com&iler.s Uob to select the s&ecific action as it a&&lies to each
sit"ation.
U) 3hat do )o" "nderstand b) f"nction overloadingL Mive an e(am&le ill"strating its "se in a c++
&rogram.
Ans: , f"nction name having several definitions that are differentiable b) the n"mber or t)&es of their
arg"ments/ is 4nown as an overloaded f"nction and this &rocess is 4nown as f"nction overloading.
<"nction overloading not onl) im&lements &ol)mor&hism b"t also red"ces n"mber of com&arisons in a
&rogram and thereb) ma4es the &rogram r"n faster.
Example program illustrating function o!erloa$ing:
??rogram to find o"t area of a circle or area of rectangle "sing
??f"nction overloading.
Hincl"de#iostream.h7
Hincl"de#conio.h7
void area(float r)
{ co"t##.In0he area of the circle = .##2.1O1BDrDr! %
void area(float l/float b)
{ co"t##.In0he area of the rectangle = .##lDb! %
void main( )
{ float rad/len/bre!
int n!
clrscr( )!
co"t##.In1. ,rea of a *ircle..!
co"t##.In$. ,rea of a Gectangle..!
co"t##.InIn'nter )o"r choice: .!
cin77n!
switch(n)
{
case 1: co"t##.In'nter the radi"s: .!
cin77rad!
area(rad)!
brea4!
case $: co"t##.In'nter the length and breadth: .!
cin77len77bre!
area(len/bre)!
brea4!
defa"lt: co"t##.In9o" have to enter either 1 or $.!
% ??end of switch
getch( )!
%
Q$ a)<ind the o"t&"t of the following &rogram:
Hincl"de#iostream.h7
Hincl"de#string.h7
Hincl"de#ct)&e.h7
void *hange(char Qsg[]/int Ven)
{
for(int *o"nt=A!*o"nt#Ven!*o"nt++)
{
if(islower(Qsg[*o"nt]))
Qsg[*o"nt] = to"&&er(Qsg[*o"nt])!
else if(is"&&er(Qsg[*o"nt]))
Qsg[*o"nt] = tolower(Qsg[*o"nt])!
else if (isdigit(Qsg[*o"nt]))
Qsg[*o"nt]=Qsg[*o"nt]+1!
else Qsg[*o"nt] = .D.!
%
%
void main( )
{
char Qessage[ ]=.$AAB 0ests ahead.!
int Si=e=strlen(Qessage)!
*hange(Qessage/Si=e)!
co"t##Qessage##endl!
for(int *=A/G=Si=e . 1! *#=Si=e?$!*++/G11)
{
char 0em&=Qessage[*]!
Qessage[*]=Qessage[G]!
Qessage[G]=0em&!
%
co"t##Qessage##endl!
%
Ans:
J"t&"t:
-++./tESTS/A0EA)
)AE0A/SSTEt/.++-
b)>ifference between if and switchW.case.
0he if statement can be "sed to test conditions so that we can alter the flow of a &rogram. 6n other words:
if a s&ecific statement is tr"e/ e(ec"te this instr"ction. 6f not tr"e/ e(ec"te this instr"ction. 6< Statement:
*hec4s the val"e of data is less than or greater than. (in ranges).
e(am&le: can tell whether an in&"t age is more than 1C and less than XA.
6f (age71C)
co"t##.'leigible to vote.!
else
co"t##.In not eligible to vote.!
0he switch statement is almost the same as an if statement. 0he switch statement can have man)
conditions. 9o" start the switch statement with a condition. 6f one of the variables e;"als the condition/
the instr"ctions are e(ec"ted. 6t is also &ossible to add a defa"lt. 6f none of the variable e;"als the
condition the defa"lt will be e(ec"ted. Switch *ase: *hec4s the val"e of data that is &res&ecified. onl)
e;"al to.
switch(ch)
{
case 1: co"t##.In one.! brea4!
case $:co"t##.Itwo.!brea4!
defa"lt:co"t##.error.!
%
c)3hat is the difference between entr) control or e(it control loo&L Mive e(am&le
Jr difference between while and doWwhile loo&L
,ns'ntr) *ontrolled will chec4 the *ondition at <irst and doesnYt e(ec"te if it is <alse eg. 3hile and for
loo&
while (condition)
{
Statements!
%
'(it *ontrolled will chec4 the *ondition at Vast and at least once the statement will e(ec"te tho"gh it is
<alse eg doWwhile loo&.
do while loo& :
do
{
Statements!
%while(condition)!
d) >efine f"nction and give e(am&le.
, f"nction is a gro"& of statements that is e(ec"ted when it is called from some &oint of the &rogram.
@sing f"nctions we can str"ct"re o"r &rograms in a more mod"lar wa)/ accessing all the &otential that
str"ct"red &rogramming can offer to "s in *++.
0he following is its format:
t)&e name ( &arameter1/ &arameter$/ ...) { statements %
where:
t)&e is the data t)&e s&ecifier of the data ret"rned b) the f"nction.
name is the identifier b) which it will be &ossible to call the f"nction.
&arameters (as man) as needed): 'ach &arameter consists of a data t)&e s&ecifier followed b) an
identifier/ li4e an) reg"lar variable declaration (for e(am&le: int () and which acts within the
cout<<\one; will be executed if value of
ch is 1
default is the last case, & is executed if
none of the value is present
f"nction as a reg"lar local variable. 0he) allow to &ass arg"ments to the f"nction when it is
called. 0he different &arameters are se&arated b) commas.
statements is the f"nctionYs bod). 6t is a bloc4 of statements s"rro"nded b) braces { %.
Eere )o" have the first f"nction e(am&le:
1
$
2
O
B
X
R
C
P
1A
11
1$
12
1O
1B
1X
1R
1C
// function example
#include <iostream>
using namespace std!
int addition (int a/ int b)
{
int r!
r=a+b!
return (r)!
%
int main ()
{
int =!
= = addition (B/2)!
co"t ## Z0he res"lt is Z ## =!
return A!
%
e) 3hat if f"nction &rotot)&ing K f"nction definition
Ans: 6n *++/ a f"nction &rotot)&e is a declaration to the com&iler that a certain f"nction e(ists/ witho"t a
f"ll definition. 0his allows other f"nctions to call that f"nction. 0he lin4er will later ma4e s"re that a
f"nction definition act"all) e(ists (&erha&s somewhere else)/ and will t"rn the call to the f"nction to a call
to the correct f"nction. <"nction &rotot)&es e(ist in *++ and essentiall) are initiali=ations of f"nctions/
meaning that the) define the t)&e and the arg"ments of the f"nction/ the) omit the contents. Qost of the
time these are located in a header (or .h) file/ while the contents are in a *++ ( .c&&) file.
'(am&le:
int add(int a/ int b)! ??<"nction &rotot)&e.
int add(int a/ int b) ?? <"nction >efinition.
{
ret"rn a + b!
%
d) >ifferentiate between brea4/ contin"e/ e(it K ret"rn
Ans rea% 1 0he brea4 statement is "sed to U"m& o"t of loo&. ,fter the brea4 statement control &asses to
the immediate statement after the loo&. asses control o"t of the com&o"nd statement.
0he brea4 statement ca"ses control to &ass to the statement following the innermost enclosing while/ do/
for/ or switch statement. 0he s)nta( is sim&l)
brea4!
continue 1 @sing contin"e we can go to the ne(t iteration in loo&.
exit 1 it is "sed to e(it the e(ec"tion of &rogram. '(it terminates the entire &rogram. 6f )o" U"st want to
sto& loo&ing/ )o" "se brea4. 6f )o" want to sto& the c"rrent loo& iteration and &roceed to the ne(t one/ )o"
"se contin"e.
note: brea4 and contin"e are statements, e(it is function.
return: '(its the f"nction. Get"rn e(its immediatel) from the c"rrentl) e(ec"ting f"nction to the calling
ro"tine/ o&tionall) ret"rning a val"e. 0he s)nta( is:
ret"rn [e(&ression]!
<or e(am&le/
int s;r (int () void dis&la)()
{ {
ret"rn ((D()! co"t##.hello.! ret"rn!
% %
f) 3hat is goto statement in c++. Mive e(am&le
Ans: Moto &erforms a one-(ay transfer of control to another line of code! in contrast a f"nction call
normall) ret"rns control. 0he U"m&ed1to locations are "s"all) identified "sing labels. , statement label is
meaningf"l onl) to a goto statement! in an) other conte(t/ a labeled statement is e(ec"ted witho"t regard
to the label. , jump-statement m"st reside in the same f"nction and can a&&ear before onl) one statement
in the same f"nction. 0he set of identifier names following a goto has its own name s&ace so the names
do not interfere with other identifiers. Vabels cannot be redeclared.
Hincl"de#stdio.h7
Hincl"de#conio.h7
void main()
{
int (!
&rintf(Zenter a n"mber Z)!
scanf(Z[fZ/K()!
if (([$==A)
goto even!
else
goto odd!
even :
&rintf(ZIn [f is even noZ)!
ret"rn!
odd:
&rintf(ZIn [f is odd noZ)!
getch()!
%
Expression or value is optional a
void function will not have an
expression or value
Q2 a) 3hat is nested ifL
6t means one if statement inside another if statement.
if( condition )
{
if( some other condition)
{
%
else
{
%
%
else
{
if( another condition)
{
%
%
b) 3hat are t)&es of e(itL
Ans: '(it are of $ t)&e:1 normal K abnormal e(it.
Normal e(it ta4es &lace when loo&5s test condition fails. 6n this caseI/ control is transferred to first
e(ec"table statement following the loo& statement or nest statement of loo& statement.
'g
for(int i=A!i#1A!i++)
{
co"t##i!
%
,bnormal e(it ta4es &lace when the control from the loo& to some other statement/ the ne(t statement of
the loo& or else where even if the condition is tr"e.
'g
for(int i=A!i#1A!i++)
{
6f (i==B)
brea4!
co"t##i!
%
Loop will terminate when i!,
thou"h loop condition is still true#
c) >efine mani&"lator K give e(am&le
, mani&"lator in c++ constr"ct is "sed to control formatting of o"t&"t and? or in&"t val"es. Qani&"lator
can onl) be &resent in i1o statements li4e endl. endl is defined in iostream.h
d) >iffernetiate between get()/ getchar()/ getch()/ getche().
1et2): 0he get() in&"t a single character from standard in&"t device and waits for enter 4e)
cin.get(ch)! it belong to iostream.h
getc#ar2) : 6t wor4s differentl) from others two. 3henever )o" are &ressing an) 4e) then the these are
4e&t in S"ffer. ,fter hitting enter the first character gets &rocessed. ,nd it obvio"sl) echoes on the screen.
'g getchar(ch)! Selongs to stdio.h
getc#2) : 6t reads a character and never wait for 'nter 4e).\"st gets &rocessed after getting an) 4e)
&ressed.,nd it never echoes the character on screen which " &ressed. 'g ch=getch()!Selongs to conio.h
getc#e2) : it wor4s same as getc#2) b"t it echoes on screen. 'g ch=getche()!Selongs to conio.h
e) 3hat is the &"r&ose of header fileL 3hat will ha&&en in absence of header fileL
,ns: , librar) is a collection of s"b&rograms "sed to develo& other &rograms and software. 0he c++
standard librar) contains file containing the standard f"nctions that the &rogram ma) "se. 0hese files are
called heard files. Eeader files &rovide f"nction &rotot)&es/ definition for librar) f"nctions. 6n the absence
of header file &rogram wo"ld not com&ile.
f) >efine comment. Mive brief descri&tion abo"t t)&es of comments.
,ns: A comment is a line or paragraph of text in a file such that that line or paragraph is not
considered when the compiler (or the parser) is decoding the code of the file.
The C++ language accepts two types of comments:
Single line comment entry- To write a comment on one line type two forward slashes
!! and type the comment. Anything on the right side of "oth forward slashes would
not "e read "y the compiler (actually the parser)
#ere are examples:
// This are various examples of a one-line comment
$ultiple line comment entry- To write a comment you can start it with a forward
slash ! followed "y an asteris% & write the comment. To end the comment type an
asteris% & followed "y a forward slash !. This type of comment can "e spread on
'arious lines
#ere are examples:
/* This are various examples of a forward slash asterisk comment */!!
g) 3hat are reference variableL 3here the) are "sedL Eow the) are declared
,ns: C++ references allow you to create a second name for the a 'aria"le that you can use to read or
modify the original data stored in that 'aria"le. (eclaring a 'aria"le as a reference rather than a
normal 'aria"le simply entails appending an ampersand to the type name such as this )reference to
an int).
*g. int x;
int& foo = x;
// foo is now a reference to x so this sets x to 56
foo = 56;
std::cout << x <<std::endl;
0he) are "sed in f"nction called "sing call b) reference.
void swap (int first, int& second)
{
int tep = first;
first = second;
second = tep;
!
void ain()
{
int a = ";
int # = $;
swap( a, # );
!
h) 3hat is t)&e conversionL Mive its t)&e.
,ns: (ata needs to "e con'erted from one type to another type. This is called type
conversion. Type con'ersion is the process of con'erting one predefined type into another.
Implicit type conversion is done automatically "y the compiler whene'er data from
different types is intermixed. +hen a 'alue from one type is assigned to another type the
compiler implicitly con'erts the 'alue into a 'alue of the new type. ,or example:
-
.
dou#le d%alue = $; // iplicit conversion to dou#le value $&'
int n%alue = $&()(56; // iplicit conversion to inte*er value $
$ost "inary operators re/uire their operands to "e of the same type. 0f operands of mixed
types are used the compiler will con'ert one operand to agree with the other. To do this it
uses a hierarchy of data types:
1ong dou"le (highest)
(ou"le
,loat
2nsigned long int
1ong int
2nsigned int
0nt (lowest)
Char and short are always implicitly promoted to integers (or unsigned integers) "efore
e'aluation. This is called widening or type promotion.
Explicit type con!ersion or type Casting represents a re/uest "y the programmer to do an
explicit type con'ersion. 0n standard C programming casts are done 'ia the () operator
with the name of the type to cast to inside. ,or example:
$han"es will be re%ected in b as second is
a reference variable of b sharin" memor&
with b#
-
.
3
int n%alue( = (';
int n%alue" = );
float f%alue = (float)n%alue( / n%alue";
QOa) Eow man) times will the following &rogram will &rint -e(amination.L 1
Hincl"de#iostream.h7
void main( )
{
while(1)
{
co"t##.e(amination.
%
%
,NS $7@nless ]* is &ressed /&rogram will &rint -e(amination. infinitel).
b) 3hat is str"ct"re in c++L
,ns:1 Str"ct"res form a ver) large b"ilding bloc4 with which to collect li4e data into one
collective "nit. 0he) are a versatile data str"ct"re in which to cl"m& data together in convenient
little &ac4ages. ,rra)s are one of the most widel) "sed data str"ct"res in &rogramming
lang"ages. Jne downfall of "sing s"ch a data t)&e is that one m"st "se homogeneo"s data t)&es/
an arra) can onl) hold m"lti&le items of the same t)&e. Str"ct"res overcome this &roblem b)
allowing the &rogrammer to have an "nlimited n"mber of items of different data t)&es.
str"ct name
{ t)&e1 Qember1!
t)&e$ Qember$!
t)&e8Qember(!
% co&)name/listname[(] !
c) what is the si=e of following str"ct"re
str"ct Mame
{
int gameTcode!
char &la)erTname[$A]!
float score!
char res"lt!
%
,ns: $+$A+O+1=$R b)tes
d) >efine nested str"ct"re. Mive s"itable e(am&le.
,ns: Structures can also "e nested so that a 'alid element of a structure can also "e in its turn
another structure. 'estin" of structures is placin" structures within structure#
struct ovies+t {
'g.
str"ct Mames
{ char Name[CA]!
char Gating!
long N"mberJf^ills!
%!
strin* title;
int ,ear;
!;
struct friends+t {
strin* nae;
strin* eail;
ovies+t favorite+ovie;
! charlie, aria;
After the pre'ious declaration we could use any of the following expressions:
(
"
$
)
charlie&nae
aria&favorite+ovie&title
charlie&favorite+ovie&,ear
pfriends-.favorite+ovie&,ear
e) 3hat is sco&e and life time of a variableL
,ns: The 'aria"les used in a program are always declared. The location you declare a
'aria"le controls its 4'isi"ility5 and role in a program. The area where a 'aria"le is
declared and can "e accessed is referred to as its scope. This area usually has delimiters.
The most used delimiter of scope is the curly "rac%et. 0f a 'aria"le is declared after an
opening curly "rac%et 465 that 'aria"le is a'aila"le and accessi"le until the first and next
closing "rac%et 465. Such a 'aria"le is referred to as local. 0f a 'aria"le is declared outside
of any function such a 'aria"le is /ualified as glo"al. 0f a 'aria"le mostly a constant is
part of the operating system or the compiler such a 'aria"le has the widest scope7 it can
"e 4seen5 and accessi"le "y any function and any program pro'ided the right header file
is included.
Type of scope:
- 1ocal or "loc% scope - A scope starts with an opening curly "rac%et and ends with a
closing curly "rac%et. This scope is called a "loc%. The following program will produce an
error "ecause the side of the shape is confined to one scope and "eing accessed from
outside.
8 int x9.
6
$. <"nction sco&e 1 (f a variable is declared inside of a function or a method but
outside of an& bloc), such a variable has a function or a method scope because it is
accessible b& any other variable that is inside of the same function or method#
void abc*+
,
int x-;
.
/# Global variable 0 1ariable which is accessible throu"hout the pro"ram, b& all the
functions is called "lobal variable#
'g
int (=$!
void abc()
favorite2movie is a
nested structure of
movies2t
x is accessible here onl&
x is accessible inside
x is accessible to both abc*+ &
main*+
{
%
void main()
{ %
The Lifetime of a particular variable is the range within an executing program in which that variable is
instantiated and retains its' value. When lifetime rules are violated, no compiler errors result, rather
the program will demonstrate logic errors. Logic errors are typically much more difficult to isolate
than compiler errors.
f) 3hat is the significance of e(it()L
,ns: 0he exit f"nction/ declared in the standard incl"de file S0>V6S.E/ terminates a *++
&rogram. 0he val"e s"&&lied as an arg"ment to exit is ret"rned to the o&erating s)stem as the
&rogramYs ret"rn code or e(it code. S) convention/ a ret"rn code of =ero means that the &rogram
com&leted s"ccessf"ll).
g) 6n the following &rogram/ if the val"e of N given b) the "ser is $A/ what ma(im"m and
minim"m val"e the &rogram co"ld &ossibl) dis&la) ()L
Hincl"de#iostream.h7
Hincl"de#stdlib.h7
void main()
{
int N/ M"essme!
randomi=e()!
cin77N!
M"essme = random(N11A) + 1A !
co"t##M"essme##endl!
%
,ns: random() will generate val"e between A to P ($A11A)
Qinim"m val"e of M"essme: A+1A=1A
Qa(im"m val"e of M"essme :P+1A=1P
0herefore/ o"t&"t is a val"e between 1A to 1P
storage class specifiers in C++.
The storage class specifiers are used to change the way of creating the memory storage
for the variables.
auto:
This auto specifier tells the compiler that the variable declared will go out of scope
once the program exits from the current block. The program block can be a function, a
class or a structure.
This is the most widely used and non-used storage class specifier in C++. ecause, all
the variables declared in C++ are of the type auto by default. !o no one need to worry
about specifying this one. The declarations,
auto int var"# $$ declared with auto specifier for the c++ tutorial
int var"# $$declared without the storage class specifier
oth the above declarations will produce the same result.
static:
This static specifier when used will preserve the value for a particular variable upon re-
entry into the same function. %or example
$$C++ Tutorial - &xample code for demonstrating static variable
void static'function'example()
*
static int x + ,# $$variable for C++ tutorial example
x++#
cout -- x --endl#
.
/f this function is called ", times, the output will be ",0,1,2..etc., The value of the
variable x is preserved through function calls.
/f this static variable is declared as a member of a class, then it will preserve the value
for all the ob3ects of the class.i.e, one copy of this data variable will be shared by all
ob3ects of the class.
extern:
This extern keyword is used to specify that the variable is declared in a different file.
This is mostly used to declare variables of global scope in C++ pro3ects. 4hen the
keyword extern is used, the compiler will not allocate memory for the variable.
5rogrammers in C++ would have very fre6uently faced linker errors because of wrong
external linking.
register storage specifier:
This register keyword tells the C++ compiler to allocate some storage in the registers.
7ny operations using the register is bound to be the fastest. ut a mere specification of
register keyword won8t get the variable a place in the register. /f the compiler finds no
space in the register, it8ll use the cache memory also.

Вам также может понравиться