Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.