Lecture - Accessor, Mutator
Lecture - Accessor, Mutator
member Functions
Host Object
It gets the value of one or more data members but does not change
their values in the host object.
class Myclass{
private :
type MyVariable;
public :
type getMyVariable(); // accessor
};
class MyClass{
private :
type MyVariable;
public :
void setMyVariable(type);
};
class MyClass{
private :
type MyVariable;
public :
void SetMyVariable( );
};
void MyClasse::SetMyVariable()
{
cin>>MyVariable ;
}
Example for accessor and mutator functions in C++
#include <iostream>
using namespace std;
class Toto{ int Toto::getAge() //accessor
private : {
int age; return age;
public: }
void setAge(int a) // mutator
{
age=a;
} int main()
public : {
int getAge(); Toto t;
t.setAge(10);
}; cout<<t.setAge();
}
OUTPUT: 10
Auxiliary Member Functions:
•Are the member functions which provide
support for the operations
• examples:
void OutputMonth(); //public
void IncrementDate(); // private
Mutator, Accessor, Auxillary functions example:
class Date
{
public:
void OutputMonth(); //Auxillary
int GetMonth(); // Accessor
int GetDay(); // Accessor
int GetYear(); // Accessor
void SetMonth(int m); // Mutator
void SetDay (int d); // Mutator
void SetYear (int y); // Mutator
private:
int m_month;
int m_day;
int m_year;
};
this pointer in C++
Lets have a discussion on
how objects look at functions
and data members of a class.
class sample
type data1;
type data2;
type function1();
type function2();
type function3();
type function4();
type function();
Note:
The operator (−>) is a special operator that is the combination of
the indirection operator and the member operation.
• Ex: The following code shows setRadius() for the Circle class
without and with the use of the this pointer.
this pointer example:
#include <iostream>
using namespace std;
class Employee {
public:
int id; //data member (also instance variable)
string name; //data member(also instance variable)
float salary;
Employee(int id, string name, float salary)
{
this->id = id;
this->name = name;
this->salary = salary;
}
void display(string name)
{ cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void) {
Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee
Employee e2=Employee(102, "Nakul", 59000); //creating an object of Employee
e1.display("jeena");
e2.display();
return 0;
}
struct vs class
In c++, a structure is the same as a class except for a few
differences.
Structure Class
A Structure is not secure while a class is secure and
and cannot hide its can hide its programming
implementation details and designing details
from the end user
struct vs class
Members of a class are private by default
Members of a struct are public by default.