Accessor function in a CLASS

What is the purpose of this line of code

Page 1 of 1

4 Replies - 646 Views - Last Post: 20 September 2009 - 10:42 AM Rate Topic: -----

#1 pesadilla143  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-May 08

Accessor function in a CLASS

Posted 20 September 2009 - 10:20 AM

This is a working program with no bugs that I wrote. I am just curious. What is the purpose of the last two getInspector -accessor functions- lines of code? I am confused on WHY we need those two lines. I understand it is to "get" some private information in the class.

Do I NEED those two getInspector lines?
What would happen if I don't use those two lines?
Doesn't accessor functions defeat the purpose of having 'private' information?

I am just trying to understand the purpose of accessor functions.

#include <iostream>
using namespace std;
class Point
{
public:
	void input();
	void output();
	void movePoint();
	void rotatePoint();
	int getInspectorXPoint();
	int getInspectorYPoint();
private:
	int x;
	int y;
};
int main()
{
	Point graph;
	graph.input();
	cout << "Starting graph points:" << endl;
	graph.output();
	graph.movePoint();
	cout << "Graph points after move:" << endl;
	graph.output();
	graph.rotatePoint();
	cout << "Graph points after 90 degree clockwise rotation:" << endl;
	graph.output();
	system("pause");
	return 0;
}

void Point::input()
{
	cout << "Enter x coordinate: ";
	cin >> x;
	cout << "Enter y coordinate: ";
	cin >> y;
}
void Point::output()
{
	cout << "x Coordinate: ";
	cout << getInspectorXPoint() << endl;
	cout << "y Coordinate: ";
	cout << getInspectorYPoint() << endl;
}
void Point::movePoint()
{
	int tempX, tempY;
	cout << "How far do you want to move the x-Coordinate: ";
	cin >> tempX;
	cout << "How far do you want to move the y-Coordinate: ";
	cin >> tempY;
	x = x + tempX;
	y = y + tempY;
}
void Point::rotatePoint()
{
	int tempY;
	tempY = x;
	x = y;
	y = -tempY;
}
int Point::getInspectorXPoint()
{
	return x;
}
int Point::getInspectorYPoint()
{
	return y;
}


This post has been edited by pesadilla143: 20 September 2009 - 10:38 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Accessor function in a CLASS

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Accessor function in a CLASS

Posted 20 September 2009 - 10:23 AM

No. The point of private is that it makes it harder to modify the variables, not access them.
After setting them as private, you can access them from within your class, or using friends, or using set methods (setters)

If you're not very confident about them, simply do a search for getters and setters. You'll likely get results for all sorts of languages, but the principal is still the same. :)

Also, for future reference, please :code:

This post has been edited by gabehabe: 20 September 2009 - 10:38 AM

Was This Post Helpful? 0
  • +
  • -

#3 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Accessor function in a CLASS

Posted 20 September 2009 - 10:37 AM

After declaring an instance of Point class, you can call both getInspectorXPoint() and getInspectorYPoint() to get the coordinates of x and y of the object.
Was This Post Helpful? 0
  • +
  • -

#4 pesadilla143  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-May 08

Re: Accessor function in a CLASS

Posted 20 September 2009 - 10:40 AM

View Postgabehabe, on 20 Sep, 2009 - 09:23 AM, said:

No. The point of private is that it makes it harder to modify the variables, not access them.
After setting them as private, you can access them from within your class, or using friends, or using set methods (setters)

If you're not very confident about them, simply do a search for getters and setters. You'll likely get results for all sorts of languages, but the principal is still the same. :)

Also, for future reference, please :code:


Thanks for the insight on the |code| |/code| format. I am new to the site and new to programming.

This post has been edited by pesadilla143: 20 September 2009 - 10:41 AM

Was This Post Helpful? 0
  • +
  • -

#5 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Accessor function in a CLASS

Posted 20 September 2009 - 10:42 AM

This is called encapsulation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1