Amazon.com Widgets setters, getters and variable scope oh my!
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 18, 2013, 06:09:55 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 4 - Data Types and Expressions
| | | |-+  setters, getters and variable scope oh my!
Pages: [1]   Go Down
Print
Author Topic: setters, getters and variable scope oh my!  (Read 519 times)
MarkCooperstein
Newbie
*
Posts: 19


Email




« on: April 07, 2011, 03:04:07 PM »

Ok, i have multiple things that are confounding me. I think I understand the concepts of the latter part of the webinar on 4/6/11 so I’ll list them here. Just tell me if I’m correct in my understanding, or enlighten me please.

Variable scope: in Fraction2.h you have 2 ‘getter’ methods, one called numerator (returning an int) and another called denominator (also returning an int). In the code section for these two methods you simply return ‘numerator’ in one, and ‘denominator’ in the other respectively.

-(int) numerator
{
    return numerator;
}

 Since the ‘method’ numerator had no function arguments, I assume that what it is returning is the Fraction object’s instance variables: numerator, and denominator in the denominator method’s getter. This suggests that the instance variables from the object Fraction have scope outside of where they were declared, which was in Fraction.h. Or, is the scope *not* global program wide, but accessible to the numerator and denominator method in Fraction.m only because you #import Fraction.h at the top of the file??

Also, am I correct that the ‘getters' numerator and denominator (as defined in Fraction.h) really are methods? Is this normal or good programming practice to have method names that are also the same as the object’s instance variables?

No real problems with the ‘setter’ implementation, it seems straight forward, however the combination of the two methods into the syntax:

-(void) setNumerator (int) n andSetDenominator (int) d;

takes a bit of getting used to. Obviously combining the two, they should return the same thing, (void). Are they generally used for ‘setter’ methods? Can they be written to return something other then void, say, they both return (int), and for that matter can they have more then one argument and can there be more then two strung together as was in your example?

 Is this legal:

-(int) someThang: (int) n, (float) d andSomeSecondThang: (int) x andSomeThirdThang: (int) y;

Cheers, Mark

(verbosity off!)
« Last Edit: April 07, 2011, 03:06:56 PM by Markcooperstein » Logged
MarkCooperstein
Newbie
*
Posts: 19


Email




« Reply #1 on: April 08, 2011, 03:40:53 PM »

Did some experimentation. It seems that this is legal:

-(int) someThang: (int) n andSomeSecondThang: (int) x andSomeThirdThang: (int) y;

but the example I used in the post above is not. IOW, you are not limited to just two arguments in this funky method (?) implementation! Tongue
Logged
MarkCooperstein
Newbie
*
Posts: 19


Email




« Reply #2 on: April 09, 2011, 06:45:08 AM »

OK, now i’ve confused myself.  If:
-(int) SomeThang: (int) x andSomeOtherThang: (int) y;

is a method declaration, why is it done this way, and not like the C function way for two arguments, ie:

(int) SomeFunction((int) x, (int) y);

?

Aren’t they both just two argument function declarations? I guess I need to go back and read the book Sad
Logged
MarkCooperstein
Newbie
*
Posts: 19


Email




« Reply #3 on: April 09, 2011, 07:02:15 AM »

obviously my C is a bit rusty... the correct C function declaration in post above should have been:

int SomeFunction( int x, int y);

but I’m sure y’all get the idea...

This whole issue begs the question that has been mulling in my mind..how much OO is required to write a good application? I have looked through some code in the Linux OS that ostensibly is written in C++, but see very little that looks like what we’re learning here (Objects, Classes, methods, inheritance, polymorphism, etc). Is this a pitfall of adding OO onto a procedural language?
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.