class Date
{
private:
int m_nMonth;
int m_nDay;
int m_nYear;
public:
void SetDate(int nMonth, int nDay, int nYear)
{
m_nMonth = nMonth;
m_nDay = nDay;
m_nYear = nYear;
}
int GetMonth() const { return m_nMonth; }
int GetDay() const { return m_nDay; }
int GetYear() const { return m_nYear; }
};
void PrintDate(const Date cDate)
{
// although cDate is const, we can call const member functions
cout << cDate.GetMonth() << "/" <<cDate.GetDay() << "/" <<
cDate.GetYear() <<endl;
}
int main()
{
const Date cDate;
cDate.SetDate(10, 16, 2020);
PrintDate(cDate);
return 0;
}
#include <iostream>
using namespace std;
class Object
{
public:
Object() {}
void Print() const
{
cout << "const" << endl;
}
void Print()
{
cout << "mutable" << endl;
}
};
void print_obj(const Object& obj)
{
obj.Print();
}
int main()
{
Object obj1;
const Object obj2;
Object*const pobj1 = &obj1;
print_obj(obj1);
print_obj(obj2);
obj1.Print();
obj2.Print();
pobj1->Print();
return 0;
}
Output
const
const
mutable
const
mutable