473,799 Members | 3,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessor Methods

Can someone explain what these really are for example:

----------------------------------------------------------

void SetFrameRate(in t iFrameRate) { m_iFrameDelay = 1000 / iFrameRate; };

----------------------------------------------------------

I know what functions are and i know what void is and stuff but i dont get
the whole `Accessor Methods` term i guess.

Regards,
Carl
Jul 22 '05 #1
3 4551
LuCk wrote:
Can someone explain what these really are for example:

----------------------------------------------------------

void SetFrameRate(in t iFrameRate) { m_iFrameDelay = 1000 / iFrameRate; };

----------------------------------------------------------

I know what functions are and i know what void is and stuff but i dont get
the whole `Accessor Methods` term i guess.

Here's what

http://cplus.about.com/library/gloss...ssormethod.htm

has to say:

An accessor method is a method used to examine or modify the members
of a class. Variables declared as private as accessed indirectly via
these methods.

Also Known As: accessor

Examples:

class Square {
private:
int side;
public:
void set_side(int length) {side = length};
int get_side() const;
int calc_area() const;
};

In the above example, set_side and get_side are accessor methods.

I would add that there are often good reasons to use accessors, rather
than making data public. The accessors give you a chance to make sure
data are valid before actually modifying the object, as well as a chance
to log changes.

Suppose, in the above example, that "side" takes on a value you do not
intend. You can validate changes to the variable, and log failures,
from the accessor:

void set_side( int length )
{
if( length > 0 )
side = length;
else
std::cerr << "warning: can't set_side( " << length << " )\n";
};

Hth,
Jeff

Jul 22 '05 #2
"LuCk" <Lu**@insight.r r.com> wrote...
Can someone explain what these really are for example:

----------------------------------------------------------

void SetFrameRate(in t iFrameRate) { m_iFrameDelay = 1000 / iFrameRate; };

----------------------------------------------------------

I know what functions are and i know what void is and stuff but i dont get
the whole `Accessor Methods` term i guess.


The term has nothing really with the language per se. It stems from
the notion of a "property" or a "trait" of a particular "object" in
Object-Oriented terminology.

It is said that objects may exhibit certain quantifiable traits, which
in programming can be represented by member functions called "accessors" .
Those traits can be implemented as values stored directly or calculated
using some kind of specific to the object algorithm.

In your example, the "frame rate" characteristic is _set_ by the function
but in the implementation we can see that the "frame rate" is not stored
directly but rather "frame delay" is calculated and stored.

The usual way to name those functions is "set..." or "get..." where the
ellipsis represent the trait being set or obtained. For example, class
Car could have a characteristic called "Speed", and the accessor
functions for it would be "setSpeed" and "getSpeed". However, it would
probably not have "speed" as a stored value, but rather the current gear
engaged and the current degree of the gas pedal depression, which
influences how fast the car is going. The implementation of "getSpeed"
would query the "speedomete r" [sub]object to obtain the reading, and the
implementation of "setSpeed" would depend on the current speed at which
the car is moving to direct some force to either the gas pedal or to the
brake pedal while the reading of the speedometer is observed until the
speed reaches the required value. That's called "algorithm" . However,
to the observer of the car it needn't be known. The observer just calls
"setSpeed" with a value and expects the car to respond somehow.

Well, you probably would benefit from a decent book on OOD. Ask in the
comp.object newsgroup what they recommend for beginners.

Victor
Jul 22 '05 #3
LuCk wrote:
Can someone explain what these really are for example:

----------------------------------------------------------

void SetFrameRate(in t iFrameRate) { m_iFrameDelay = 1000 /
iFrameRate; };

----------------------------------------------------------

I know what functions are and i know what void is and stuff but
i dont get the whole `Accessor Methods` term i guess.

Regards,
Carl


Accessors decouple the interface of a class from the way its objects
store their state. In your example, the class which setFrameRate
belongs to communicates with the outside world in terms of frame rate
but actually stores another quantity (that is presumably more useful
to other members). We can also update multiple member variables or
check for invalid parameters in a setter function.

Going a step further, a class attribute needn't be backed by a member
variable at all:

#include <os_stuff.h>

class File {
public:
File(char* filename)
{ handle_ = os_openFile(fil ename); }

// all the usual file operations...

date getDate()
{ return os_getFileDate( handle_); }

private:
os_Handle handle_;
};

All names are made up, there's no error
handling, etc., but you get the idea.
Have a good New Year's Eve,
Martin
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1998
by: jerrygarciuh | last post by:
Hello, I have been working for some time now on a PHP OOP database abstraction layer. Yes I know there are others out there which are maturing but I like reinventing this wheel. The task I have at hand is that I want to specify 'has a' and 'has many' relationships in an array for each class and use these to automagically create the accessor methods and perhaps mutator methods as well.
22
6780
by: mirandacascade | last post by:
When I look at how classes are set up in other languages (e.g. C++), I often observe the following patterns: 1) for each data member, the class will have an accessor member function (a Get<whatever> function) 2) for each data member, the class will have a mutator member function (a Set<whatver> function) 3) data members are never referenced directly; they are always referenced with the accessor and mutator functions My questions are:
2
2068
by: Todd A. Anderson | last post by:
I've inherited two "C++" code bases that I have to tie together and both of them make frequest use of public member variables in spite of all the information saying this limits flexibility. Well, their lack of foresight has bit me of course! I'd like to start off by converting all their public member variable accesses to the use of accessor methods. Then, I can reimplement selected accessor methods to provide the necessary glue between...
22
12041
by: Generic Usenet Account | last post by:
A lot has been said in this newsgroup regarding the "evil" set/get accessor methods. Arthur Riel, (of Vanguard Training), in his class, "Heuristis for O-O Analysis & Design", says that there is almost never an excuse for accessor methods. Personally, I do not go that far. I do feel that they serve a useful purpose (albeit in a limited manner). Personally I prefer dropping the "set" and "get" prefixes from the method names altogether. ...
6
2974
by: Jason Shohet | last post by:
I have a class with protected variables and some accessor methods, , get, set ... Maybe I have a brain blockage today but I'm thinking, why not just make those variables public. After all, someone can do just as great harm by misusing a set { } accessor method as just doing myObj.lastname = "Big Bird"; which he could do in a second class, if the variable is public on the first class. IOTW, what does keeping with the protected /...
7
2365
by: Tenacious | last post by:
I have been programming using C# for over a year now and I still wonder what is the importance of using accessor methods when the property is read-write. It seems easier to just make it a public property of the class, but I have read in more than one book that this is a bad practice. The books did not mention the reason why this is a bad practice. Can anybody give me some insight on this?
8
1786
by: AAJ | last post by:
Hi all I would like to have a class that can set/return values of different datatype via a single accessor, i.e. overload the accessor i.e. something like DateTime m_DateValue; string m_StringValue;
8
8243
by: Tim Sprout | last post by:
Why is it considerd best practice to use Properties rather than Get and Set accessor methods? -Tim Sprout
2
1382
by: rbjorkquist | last post by:
This is my first attempt at writing/using web services, so any and all comments will be greatly appreciated. With that said, I am also by no means saying this is the correct either. I have noticed that when returning a new TPastePart, created and filled by the web service, that if the "PastePartID" and "PastePart" properties do not have "set" accessor methods, their respective data is not returned, even though the object has contains...
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10243
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9078
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.