Phantom Programming

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Phantom Programming

Post by Don Pwnious »

Can someone explain "accessors method"?
1/8th time- 14secs
1/8th speed - 110mph
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Phantom Programming

Post by Falco Girgis »

The Phantom wrote:Can someone explain "accessors method"?
Ah, so you're already to the object-oriented goodness.

Sure, I'll explain:

Okay, first you must realize that there are private and public members of classes in C++. It is ALWAYS good to have variables and stuff private. (Don't ask me why, I never do it. But then again, I'm not exactly the greatest programmer :? )

Let's say that we had this here cat class:

Code: Select all

class Cat {
private:
    int age;
};
Okay, that is a prefectly good function. The member variable of class: age, is private. But then how do you access it? You do that through an accessor method.

Code: Select all

class Cat {
public:
    int GetAge() {
        return age;
    }
private:
    int age = 4;
};
That is how you are supposed to access the variable. Now, if you wanted to know what the cat's age was, you'd do this:

Code: Select all

#include<iostream>
class Cat {
public:
    int GetAge() {
        return age;
    }
private:
    int age = 4;
};

void main() {
    Cat Frisky; //Creating a new object of the cat class named "Frisky"
    std::cout << "Frisky is a cat who is " << Frisky.GetAge() << " years old.\n";
}
Now, an accessor method is simply a public function in a class that has access to private variables. The same should be done when you're setting the age of an object. Anything involving a class with its own variables should generally use accessor methods (don't feel bad if you don't, I myself just make everything public =P But hey, MarauderIIC does that religiously. It's a good thing to do).

If there is anything you want me to clarify, please ask.
Last edited by Falco Girgis on Tue Sep 07, 2004 8:04 pm, edited 3 times in total.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

I think it is good to keep things private because you don't have to worry about those evil bugs that arise from abusing the variables.... The ones tht are so stinken hard to find. :evil:
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post by MarauderIIC »

There's a couple ideas behind it

1) A good rule to follow with object-oriented programming is to program by modeling real things.

2) The functions are so it becomes impossible to set variables to incorrect values, esp. if you're using user-input for it. You can't error check with a basic assignment operator. Basically, the concept is that a class should be in control of all of its own stuff, instead of spreading the control for that around the program, or not having any control (model real things).
There. That's almost what I want to say.

3) It's consistent. You'll definitely need to error-check at least some of your variables. This saves you from having to go, "does this variable need value checking, or is there a function somewhere...?"

4) Besides any super-important reason I may have missed, I find it's just a good, easy convention to follow. Esp. since I do easy-to-remember things such as:

Code: Select all

class TheClass {
    int [u]theSuperVariable[/u];

    public:
    void set[u]TheSuperVariable[/u](int value);
    int get[u]TheSuperVariable[/u]();
};
(Eventually! I'll get to the code-formatting eventually! Hold your horses... at least until the end of the week. :) )
Last edited by MarauderIIC on Tue Sep 07, 2004 8:27 pm, edited 1 time in total.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

ohhhh thanks all for explaining i shall continue knowing this
1/8th time- 14secs
1/8th speed - 110mph
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

UMMMM.... I am starting to learn SDL and openGL(after)and i will get milkshape 3D and i dont know how to use it so is there a web site or a book that i can learn how to use milkshape 3D???[/u]
1/8th time- 14secs
1/8th speed - 110mph
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post by MarauderIIC »

try google
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

does anyone know the websites to download SDL.h include for C++??
1/8th time- 14secs
1/8th speed - 110mph
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

What? :shock:

Your book didn't come with the SDL libraries on a CD or something.... I'll find it though, don't worry...
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

I didnt buy it yet i would have to order it or what for a week or two to get it for a better price.
1/8th time- 14secs
1/8th speed - 110mph
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

Seriously, just type SDL in google and download from the main site.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

thanks but i tried that and it didn't have it
1/8th time- 14secs
1/8th speed - 110mph
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

Thats funny, I just did it on my first try. Anyway, here's the site. http://www.libsdl.org/index.php

Just scroll down to downloads.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

oh i got it from a diifferent website, but thank, and i got that one
how do you use it??
1/8th time- 14secs
1/8th speed - 110mph
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

The Phantom wrote:oh i got it from a diifferent website, but thank, and i got that one
how do you use it??
You sound like you are trying to get different versions to work? Why, just use the latest.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
Post Reply