0% found this document useful (0 votes)
2 views2 pages

Sample Programs

Uploaded by

naacc3.klnce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Sample Programs

Uploaded by

naacc3.klnce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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

You might also like