Tese are
member functions
of class which allow only memory reading.It simply means we can not change anydata member
of class either const or non-constdata member
in Const member functions.The can be accessed by bothconst object
andnon-const object
.
constructors
are also special member functions of class but it is not allowed to make constructors const functions.
#include<iostream>
using namespace std;
class Student
{
String name;
const int Id = 1;
public:
//-----Getters/Accessors----
string getName() const
{
//Here if you try to modify data member name,it will cause error although it is non-const
// like:
// name="Waleed";
return name;
}
int getId() const
{
//Here if you try to modify data member Id,it will cause error
// like:
// Id=9;
return Id;
}
};
int main()
{
Student student_1;
Student const student_2;
//Accessing constant function by Non const-object
student_1.getId();
//Accessing constant function by const-object
student_2.getId();
}