Tuesday, September 6, 2011

Constant function member in C++ programming


A constant function member can never modify any of its class’s data member.
const keyword to used to define constant function.

#include< iostream.h >
#include< conio.h >
class sample
{

private:
int count;
public:
sample()
{
count=0;
}
void change()
{
count=10;
}
void  show()const
{
count=12;/* this statement will show error in compile time */
cout<< "\nThe data member:"<< count;
}
};
void main()
{
clrscr();
sample sp1;
sp1.change();
sp1.show();
getch();
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner