Sie sind auf Seite 1von 10

Macros : Macros are symbolic constants and they are used for improving the readability.

The general syntax for defining the macro is , #define macroname macroexpansion e.g. #define MAX 5 !o", MAX is a macro and "here ever "e "rite MAX it "ill be considered e#uivalent to 5 . And also , MAX$% & "ill results in an error, as MAX is replaced by 5 , so the statement means , 5 $% & as "e cannot assign one constant into another so it "ill gie an error . 'onsider the follo"ing code , #include(iostream.h) #include(conio.h) #define *+ ,.-.

void main/0 1 float radius,area,circum& clrscr/0& cout((endl((23nter the radius of the circle :2& cin))radius& 44calculate the area of the circle area$*+ 5 radius 5 radius& 44 calculate the circumference of the circle circum $ 6 5 *+ 5 radius 5 radius & cout((endl((2Area of circle :2 (( area& cout((endl((2'ircumference of the circle :2((circum& getch/0& 7 8eference 9ariables : The reference variable "ill store the reference of the variable. The general form of declaring the reference variable is , datatype : referencevariablename$variablename&

e.g. int a$- & int :b$a& !o", b "ill be the reference variable and it "ill reference to a. The reference variable is prefixed by an ampersand sign at the time of its declaration. As , the pointer variable "ill store the address of the variable , the reference variable "ill store the refernce of the variable. That is , it "ill reference to the same location to "hich the variable is referencing to. +n case of pointers , int a$- & int 5b& b$:a&

+n case of reference variable, int a$- & int :b$a&

;o , if "e ma<e any change in b , the change "ill get reflected in a and vice versa. +n c==, the reference variable can be used in three different "ays , /a0 +ndependent 8eference variable /b0 *assing references to the functions /c0 8eturning references from the function. /a0 +ndependent 8eferences 9ariables : 'onsider the follo"ing programs , #include(iostream.h) #include(conio.h) void main/0 1 int a$- & int :b$a& cout((endl((2a$ 2 (( a ((2 b$2((b& a$a=6& cout((endl((2a$ 2 (( a ((2 b$2((b&

b$b>-& cout((endl((2a$ 2 (( a ((2 b$2((b& int c$5 & b$c& cout((endl((2a$ 2 (( a ((2 b$2((b((2c$2((c& b$b=5& cout((endl((2a$ 2 (( a ((2 b$2((b((2c$2((c& getch/0& 7 output : a$- b$a$-6 b$-6 a$-- b$-a$5 b$5 c$5 a$55 b$55 c$5 /b0 *assing references to the function 'onsider the follo"ing program , #include(iostream.h) #include(conio.h)

45function prototype54 void s"ap/int : , int :0& void main/0 1 int a,b& clrscr/0& cout((endl((23nter the value of a and b:2& cin))a))b& cout((endl((2?efore ;"ap a$2((a((2 b$ 2((b& s"ap/a,b0& 45function call54 cout((endl((2After ;"ap a$2((a((2 b$2((b& getch/0& 7 45function definition54 void s"ap/int : a , int :b0 1 int c& c$a& a$b& b$c& 7 !o" , "e have used the reference variables in the formal arguments ,so they "ill reference to the actual arguments ,

so the changes "hich "e ma<e in the formal arguments "ill also get reflected in the actual arguments.This is <no"n as 'all ?y 8eference /c0 8eturning 8eferences from the functions . 'onsider the follo"ing program , #include(iosteam.h) #include(conio.h) 45fucntion prototypr54 char : func/int0& 45global declaration 54 char s@ A $ 2Bello Corld2& void main/0 1 func/50$D5D& 45function call54 cout((endl((s& getch/0& 7 45function defintion 54 char : func/int pos0 1 return s@posA&

7 output : Bello5Corld *rotected Eata +tems : 'onsider the follo"ing program , #include(iostream.h) #include(conio.h) class A 1 int a& public: int b& protected: int c& A/0 1 a$- & b$6 & c$, & 7 7& class ? : public A 1

public: void sho"/0 1 cout((endl((2a$2((a& cout((endl((2b$2((b& cout((endl((2c$2((c& 7 7 void main/0 1 ? ob& ob.a$- & ob.b$-6& ob.c$6 & ob.sho"/0& getch/0& 7 !o", the statements , ob.a$- and ob.c$6 are invalid "hile the statement ob.b$-6 is valid. This is because , the a is private and c is protected and both the members are not directly accessible by the obFect of the class.

Chile , the member b is defined as public so it can be direclty accessible by the obFect of the class. And in the sho"/0 function , The statement, cout((endl((2a$2((a& is invalid , "hile the statements cout((endl((2b$2((b& cout((endl((2c$2((c& are valid . a is private so it "ill not get inherited , "hile the b and c are the public and the protected members so they "ill get inherited.

Das könnte Ihnen auch gefallen