Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
C++ program to implement const objects and const functions :
using namespace std;
class temp{
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;
// Constant Objects can call only constant functions.
const temp y;
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