Page 1 of 3
Phantom Programming
Posted: Tue Sep 07, 2004 7:01 pm
by Don Pwnious
Can someone explain "accessors method"?
Phantom Programming
Posted: Tue Sep 07, 2004 8:04 pm
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:
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.
Posted: Tue Sep 07, 2004 8:17 pm
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.
Posted: Tue Sep 07, 2004 8:27 pm
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. :) )
Posted: Thu Sep 09, 2004 2:51 pm
by Don Pwnious
ohhhh thanks all for explaining i shall continue knowing this
Posted: Thu Sep 09, 2004 4:58 pm
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]
Posted: Thu Sep 09, 2004 7:07 pm
by MarauderIIC
try google
Posted: Fri Sep 10, 2004 8:29 pm
by Don Pwnious
does anyone know the websites to download SDL.h include for C++??
Posted: Fri Sep 10, 2004 8:46 pm
by Falco Girgis
What?
Your book didn't come with the SDL libraries on a CD or something.... I'll find it though, don't worry...
Posted: Fri Sep 10, 2004 9:22 pm
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.
Posted: Sat Sep 11, 2004 11:39 am
by JS Lemming
Seriously, just type SDL in google and download from the main site.
Posted: Sat Sep 11, 2004 11:57 am
by Don Pwnious
thanks but i tried that and it didn't have it
Posted: Sat Sep 11, 2004 12:17 pm
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.
Posted: Sat Sep 11, 2004 12:29 pm
by Don Pwnious
oh i got it from a diifferent website, but thank, and i got that one
how do you use it??
Posted: Sat Sep 11, 2004 12:45 pm
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.