Created
July 27, 2016 15:40
-
-
Save ravikiran0606/c86e1036197ff6e0acff7cf114a125dd to your computer and use it in GitHub Desktop.
C++ program to implement const objects and const functions :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
class temp{ | |
public: | |
void fn(); | |
void constantfn() const; | |
}; | |
void temp::fn(){ | |
cout<<"Hello, Am an normal function."<<endl; | |
} | |
void temp::constantfn() const{ | |
cout<<"Hi, Am an constant function :P"<<endl; | |
} | |
int main() | |
{ | |
// Normal Objects can call both normal and constant functions. | |
temp x; | |
x.fn(); | |
// Constant Objects can call only constant functions. | |
const temp y; | |
y.constantfn(); | |
int a=10; | |
// Constant Pointer... | |
int* const ptr=&a; | |
cout<<"The value pointed by the constant pointer is "<<*ptr<<endl; | |
int b=20; | |
// Pointer to constant... | |
const int* ptr2=&b; | |
cout<<"The constant value pointed by the pointer is "<<*ptr2<<endl; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment