I'm blockheaded.

Everything todo with programming goes HERE.
Post Reply
User avatar
delinquent
Match Winner
Posts: 760
Joined: Sat Jul 07, 2012 3:07 am

I'm blockheaded.

Post by delinquent »

I've had it explained a few ways, but for the life of me I can't wrap my head around it. Maybe someone here has a better way of explaining it.

I don't understand the point of accessors (get/set). Why bother?
Word
Reverse Adjust Outside Corner Grinder
Posts: 4258
Joined: Wed Jan 07, 2009 6:13 pm

Re: I'm blockheaded.

Post by Word »

I can only guess what you're talking about - I'm not a programmer. However, there's Google.
User avatar
delinquent
Match Winner
Posts: 760
Joined: Sat Jul 07, 2012 3:07 am

Re: I'm blockheaded.

Post by delinquent »

I still don't really understand the point, I've seen those posts before. Take the second one, for example.

I create an int (number) and I call it "Age":

Code: Select all

public int Age;
The example in the second post calls accessors a "layer of protection". Well, I can immediately set the value of Age to 0(zero) anyway:

Code: Select all

public int Age = 0;
Instead of doing the following:

Code: Select all

private int age;

public int Age()
{
        if (age == null)
        {
              age = 1
              MessageBox.Show("1 added to age because it didn't get assigned a value")
        }
        else
        {
              continue;
        }     

        get{return age};
        set{age = value};
}
Or something to that effect. But... why bother?

Note that in my pseudo example Age and age are two separate variables.
User avatar
takburger
Match Winner
Posts: 600
Joined: Tue Jun 04, 2013 9:34 pm

Re: I'm blockheaded.

Post by takburger »

it is a process that ensure additional safety. Very useful on big programs.
Image
Word
Reverse Adjust Outside Corner Grinder
Posts: 4258
Joined: Wed Jan 07, 2009 6:13 pm

Re: I'm blockheaded.

Post by Word »

Just a text interpretation: The range checking part of the second text (the condition statement thing) sounds to me like it's meant to drastically reduce the time/attempts needed to find a correct entry and sort out/skip the incorrect ones that would otherwise affect your program in some way - like a warehouse detective who has a secret list of the only 10 shoplifters in his town.
User avatar
kyle
Reverse Outside Corner Grinder
Posts: 1876
Joined: Thu Jun 08, 2006 3:33 pm
Location: Indiana, USA, Earth, Milky Way Galaxy, Universe, Multiverse
Contact:

Re: I'm blockheaded.

Post by kyle »

delinquent wrote: The example in the second post calls accessors a "layer of protection". Well, I can immediately set the value of Age to 0(zero) anyway:

Code: Select all

public int Age = 0;
You may be able to do that in the case of primitive types, but lists say you want to make in and array of variable length. You don't know the length until it is initialized.


again with this second one you are limiting the value of the age variable. If you make it public any other code that uses it can manipulate it, potentially making it a negative value.

If age were public

Code: Select all

int main()
{
      Person joe;

      joe.age = -1; //therefore bypassing the protection in the Person class to Greentree that age is positive.

      return 0;
}

Also if you are using objects within the object, you may want to return a deep copy instead of a pointer to the object.
Image
User avatar
/dev/null
Shutout Match Winner
Posts: 819
Joined: Sat Sep 04, 2004 6:28 pm
Location: Chicago-ish

Re: I'm blockheaded.

Post by /dev/null »

Dont feel bad, OOP has never made much sense to me. I do itertative or functional.
Post Reply