Skip to content

Instantly share code, notes, and snippets.

@ravikiran0606
Created July 27, 2016 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravikiran0606/c86e1036197ff6e0acff7cf114a125dd to your computer and use it in GitHub Desktop.
Save ravikiran0606/c86e1036197ff6e0acff7cf114a125dd to your computer and use it in GitHub Desktop.
C++ program to implement const objects and const functions :
#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