I am not new to C++, but I have a hard time understanding classes. Yes, I know how they work, I know how they function, and how to use them. But what are best used for? Why/when should I use them? How long are variables declared in classes stored? Why/when should I declarea function or variable private, public, or protected? What properties do protected functions/variables have?
I have tried tutorials/etc.. but they just don't explain it well enough for me to understand. I would appreciate it if someone could help me out.
This is not homework, and im learning this on my own.
They exist to help you think about the problem and help you organise the code. As a rule of thumb, if thinking about something as an actual self-contained object helps you think about the problem, make it a class.
So, if i understand correctly, all variables within a class (aside from variable initialized within a class function) are not "deleted" (for lack of a better term) when called again? And variables declared private/public within a class can be used in class functions just as if i had declared them in thos class functions (and they keep their data that has been modified by other function)?
If this is true, I could make my programs so much more efficient by creating a class that loads all of the data i need into public variables, and can use them on a global scale in my program to minmalize file-reading. That would be quite awsome.
The variables declared in a class exist independently on a per-instance basis, and have the same life span as the instance they are in.
You can access those instance variables from the instance member functions because they are both within the class' scope.
They don't "keep" their data, the data just doesn't get changed or reset between member function calls. Can you imagine the variable "a" getting reset across each loop in this example?
1 2 3 4 5
int a = 1;
for(int i = 0; i < 24; ++i)
{
a *= 2;
}
It's the same variable "a" every time, why wouldn't it retain its value?
@guestgulkan
Not exactly. The problem is i dont know some properties of the variables within a class function. Ex: the life span. I have seen lots of stuff that may suggest what i proposed.
@L B
Oh. So, if i declared "classhandle whatever" in one function, it would be like recalling a fuction if i declared that handle in another function (reset variables etc...)?
class MyCass
{
int x, y;
public:
MyClass() : x(0), y(0) //default constructor
{
std::cout << "Created MyClass with x=" << x << " and y=" << y << std::endl;
}
MyClass(int ValueOfX, int ValueOfY) : x(ValueOfX), y(ValueOfY) //another constructor
{
std::cout << "Created MyClass with x=" << x << " and y=" << y << std::endl;
}
int X() const //getter for x
{
return x;
}
int X(int NewValue) //setter for x
{
x = NewValue;
}
int Y() const //getter for y
{
return y;
}
int Y(int NewValue) //setter for y
{
y = NewValue;
}
~MyClass() //destructor
{
std::cout << "MyClass destroyed" << std::endl;
}
};
Just as a note, the getter/setter oesn't have to have the same name as the variable, nor does it have to have the word "get" or "set" in it. It's just a matter of preference or enforced coding convention.
I will get more confortable with data structures before moving onto classes. They operate the same way, just without functions and the complications (from my perspective, anyway, lol.) that come with them. Once i get used to data structures, i will move onto classes. Thank you though.