C++ allows an argument of a function can be declared as constant type. The prefix “const” keyword tells compiler that the function should not modify the argument. The compiler will generate an error, when this condition is violated. This type of declaration is significant only when we pass argument by reference.
Example:
#include<iostream.h>
void update(const int a, int b);
void main()
{
clrscr();
int x=10,y=20;
update(x,y);
getch();
}
void update(const int a, int b)
{
a=a+10;
b=b+20;
cout<<”x= “<<x<<”\t”<<”y=“<<y;
}
When we compile this program the line ( a=a+10 ) generates
the compile time i.e (cannot modify a const object )
No comments:
Post a Comment