Program to illustrate 'const' Member Functions


#include <iostream.h>
#include<conio.h>
/*----------------------Class definition-------------------------*/
class sample
{
        int num;
    public :
        void set(int);
        void print_number( ) const; // 'const' member function prototype           
};
void sample::set(int n)
{
    num = n;
}
void sample::print_number( ) const     // 'const' member function definition
{
    cout << "Number = " << num << "\n";
}
/*----------------------Definition ends here---------------------*/
void main(void)
{
    clrscr();
    sample a;
    a.set(10);
    a.print_number( );
        getch( );
}

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|