0% found this document useful (0 votes)
219 views28 pages

Lecture - Accessor, Mutator

The document discusses different types of member functions in C++ - accessor member functions, mutator member functions, and auxiliary member functions. It provides examples of how each works, including how to define them and call them. It also discusses the this pointer, which is a pointer that is implicitly passed as a parameter to non-static member functions and refers to the object that calls the function.

Uploaded by

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

Lecture - Accessor, Mutator

The document discusses different types of member functions in C++ - accessor member functions, mutator member functions, and auxiliary member functions. It provides examples of how each works, including how to define them and call them. It also discusses the this pointer, which is a pointer that is implicitly passed as a parameter to non-static member functions and refers to the object that calls the function.

Uploaded by

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

Accessor ,Mutator and Auxillary

member Functions
Host Object

When a member function is being executed, there is


always a host object.

The host object is the object that the instance member


function is operating on at a given moment.
Member Functions –Types

1)Accessor Member Function

2) Mutator Member Function

3)Auxillary Member Function


Accessor Member Function

An accessor member function (getter) gets information from the


host object but does not change the state of the object.

In other words, it makes the host object a read-only object.

It gets the value of one or more data members but does not change
their values in the host object.

Accessors are member functions that allow access to data members.

An accessor is a member function that allows someone to retrieve


the contents of a private data member.
For an accessor to perform its function, the following
conditions must be met:

1) The accessor must have the same type as the returned


variable.

2) The accessor need not have arguments.

3) A naming convention must exist, and the name of the


accessor must begin with the “get" prefix.
The syntax of an accessor reduced to its simplest expression
looks like this:

class Myclass{
private :
type MyVariable;
public :
type getMyVariable(); // accessor
};

type Myclass :: getMyVariable() // accessor


{
return MyVariable;
}
mutator member function

Mutator member functions (setter ) are the functions


that can change the state of their host objects.

A mutator is a member function that allows for editing


of the contents of a private data member
For a mutator to accomplish its function, the
following conditions must be present:

1) As a parameter, it must have the value to be


assigned to the data member. The parameter must
be of the same type as the data member.

2) The mutator does not need to return a value.

3) A naming convention must exist, with the name of


the accessor beginning with the “set" prefix.
The syntax of a mutator reduced to its simplest expression looks
like this: [ passing parameter]

class MyClass{
private :
type MyVariable;

public :
void setMyVariable(type);

};

void MyClasse::setMyVariable(type MyValue)


{
MyVariable = MyValue;
}
The syntax of a mutator reduced to its simplest expression looks
like this: [ User Input]

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

• public if generally called outside function

• private/protected if only called by member


functions

• 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();

sample object1 ,object2 ,object3;


•Each object gets its own copy of the data member.

•But all objects share a single copy of member functions.


this pointer

 this is a keyword that refers to the current


instance of the class.

It can be used to pass current object as a


parameter to another method.

It can be used to refer current class instance


variable.
this pointer

C++ adds a pointer (a variable that holds the address of an


object) to each member function.

each member function has a hidden pointer named the this


pointer.
class sample
type data1;
type data2;

type function();

sample object1 ,object2 ,object3;


this pointer
How does an instance member function get a this pointer?

It is added as a hidden parameter to the instance member


function by the compiler as shown below:

Note:
The operator (−>) is a special operator that is the combination of
the indirection operator and the member operation.

this −> radius


When we use the member operator to call an instance
member function, the compiler changes the call statement
into two statements, as shown below:
Explicit Use of this Pointer
•We can use the this pointer in our program to refer to a data
member instead of using the data member itself.

•we can use the name of the data member as a function


parameter.

• 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.

You might also like