Home > Articles > Software Development & Management > Object Technology

  • Print
  • + Share This
From the author of What's the Point of Accessor Methods?

What's the Point of Accessor Methods?

With all the extra work (and extra code) needed to implement the accessor methods, what's the point? Let's take a look at the getter:

int Person::getAge()
{
      return (age);
}

We've created an entirely new method called getAge(),and all it does is return the value of age. You can argue that we have an entire method without any executable code! Again, what's the point?

To illustrate, let's consider the setter called setAge():

void Person::setAge(int a)
{
      age = a;
}

In its current state, any method that invokes setAge () can change the age. Now, that in itself should get you thinking why we need accessor methods. What happens if someone enters an obviously incorrect age, such as -4? Do we want just anyone to have access to the age—either to inspect it or change it—without the option of providing some error checking or security? Obviously not. Think about what should happen when you enter an obviously incorrect age: The system performs a range check on the user input.

What's the importance of accessor methods? In a nutshell, an accessor method controls access to its attribute by providing a single location to inspect and/or modify that attribute. Code can also be added to perform operations such as range checks. To see this operation in action, take a look at the updated code in Listing 4.

Listing 4—Code with built-in range checking.

// CPPLicense02.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <iostream>

class Person {
      private:
            int age;
      public:
            Person();
            void setAge (int);
            int getAge ();
};

int main()
{
      Person joe;

      joe.setAge(16);

      printf("age = %d\n", joe.getAge());

      system("pause");

      return 0;
}

// Person.cpp
//

#include "stdafx.h"
#include <stdio.h>

// Constructor (to set initial age)
Person::Person() {
      age = 0;
}

// setter
void Person::setAge(int a)
{
      if (a >= 0) {
            age = a;
      } else {
            printf("Invalid age\n");
      }
}

// getter
int Person::getAge()
{
      return (age);
}

The setter method setAge() now has some range checking (a condition statement) built right into it. Whenever anyone invokes the method to set the age, the method has some bit of protection that the age entered by the user is 0 or greater.

// setter
void Person::setAge(int a)
{
      if (a >= 0) {
            age = a;
      } else {
            printf("Invalid age\n");
      }
}

Notice that I've added a constructor so we can set the initial age:

// Constructor (to set initial age)
Person::Person() {
      age = 0;
}

The design of this application is obviously very simple (if a bit contrived) for illustration purposes. In fact, the getter may eventually need to be redesigned. You might want additional security to control even the inspection of the value of age.

  • + Share This
  • 🔖 Save To Your Account