473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Accessors "get & set"

Hello everybody,

I'm wondering about these accessors and what the benefit that I would get from them i understood what "set function" is for,

but i didn't get it with the "get function ".


even thought i searched about this topic in this site and one "Expert " tried to explain it through the car example and something with speed......
any way,


by the way some people even the author D.S Malik use at the end of heading of it the keyword "const" so what the benefit of it if it can't change the values

may be this example will deliver my point

Expand|Select|Wrap|Line Numbers
  1. consider this in the main function
  2. int main()
  3. {
  4.     clockType myclock;
  5.     clockType yourclock;
  6.  
  7.     int hours;
  8.     int minutes;
  9.     int seconds;
  10. .
  11. .
  12. .
  13. .
  14. }
  15.  
  16. void clockType::getTime (int &hours, int &minutes, int &seconds) const
  17. {
  18.     cout << "getTime is started at the body of it \n";
  19.     hours = hr;
  20.     minutes = min;
  21.     seconds = sec;
  22.     cout << "getTime is ended at the body of it \n";
  23. }
  24.  
** and how the compiler would know which object to deal with "myclock" or "yourclock" //consider the parameters list it is called by reffrence



I didn't get it, so please if any one explain it in the easiest way i would be greatful for him

********My english is not that good so please use simple terms*******************


thanks alot
Aug 10 '08 #1
1 1698
arnaudk
424 256MB
Consider the following version instead:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. Clock myclock;
  4. Clock yourclock;
  5.  
  6. int hours;
  7. int minutes;
  8. int seconds;
  9.  
  10. myclock.setTime(23,12,54);
  11. yourclock.setTime(12,0,0);
  12. // etc ...
  13.  
  14. std::cout << "The time of myclock is " 
  15.  
  16. myclock.getTime(hours,minutes,seconds);
  17.  
  18. std::cout << hours << ":" << minutes << ":"  seconds << std::endl;
  19. }
  20.  
Lines 3,4: myclock and yourclock are not clock types but are actually clocks themselves, so we call the class Clock not clockType.

Lines 10,11: The setTime member function sets the internal variables of the clock objects. When I call myclock.setTime(23,12,54); I will set the time of myclock to 23:12:54, when I call yourclock.setTime(12,0,0); I will set the time of yourclock to lunchtime.

Line 16: The getTime function reads the time of the clock object and writes it into the variables you supplied to it: hours, minutes seconds. It does not change the state of the clock object and we use the 'const' keyword to enforce this.
myclock.get(hours,minutes,seconds); gets the time of myclock, not yourclock.

Please, next time don't write your whole message in bold, and please use [code]...[/code] tags around your code.
Aug 11 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Me | last post by:
Hi, I would like learn from people with experience in C++, which of the following styles of way to construct "get/set" member functions would be the best in terms of usability, speed, et cetera. ...
11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
2
by: Eric Osman | last post by:
Hi, I'm looking for a javascript function that will convert input such as this: <CLUB Code=" into this: &lt;CLUB Code=&quot;
4
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following...
7
by: Javaman59 | last post by:
This is probably common knowledge to .Net gurus, but please bear with me while I share a discovery with the group... I needed to create a public lock on a class, as follows... class Locked {...
6
by: Rich | last post by:
Hello, I have to create a table in an Access mdb (remotely) on the fly. Create Table tbl1(fld1 Integer, fld2 varchar(10), fld3...) Then I have to insert data: Insert Into tbl1 Values(" &...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
3
by: divya | last post by:
Hi, I have a table tblbwday with 2 fields Name and Birthday.I have written this script for displaying evryday names of the people on that day. <% set objConn...
13
by: Ragnar | last post by:
Hi, 2 issues left with my tidy-work: 1) Tidy transforms a "&amp;" in the source-xml into a "&" in the tidied version. My XML-Importer cannot handle it 2) in a long <title>-string a wrap is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...

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.