ok, first i would like to express my appreciation to the forums and the rest of the website (mainly the tutorials). theres a wealth of information here that is really helpful.
but i have a few questions about classes:
i understand the _concepts_ of classes as explained at cprogramming.com and at http://www.cplusplus.com/doc/tutorial/classes/ but im in need of a bit of hand-holding when it comes to the _way_ they work. i dont fully understand how variable within classes are dealt with or processed.
i will describe the way classes work as far i understand; please correct me if im wrong and clarify if i dont understand completely or properly.
// classes example
#include <iostream>
usingnamespace std;
class CRectangle { // declares class type
int x, y; // set two private int variables
public: // set following functions as public
void set_values (int,int); // a public function that returns void and required two int arguments
int area () {return (x*y);} // a public function that returns an int value and requires two int arguments
};
void CRectangle::set_values (int a, int b) { // this _defines_ the set_values function within the class CRectangle,
// requiring the function to take ints a and b and assigns their values to x and y respectively
x = a; // passed value of a gets assigned to x (the private variable within the class)
y = b; // passed value of b gets assigned to y (the private variable within the class)
}
int main () {
CRectangle rect; // creates an object called rect as defined by the class CRectangle
rect.set_values (3,4); // this function passes (a,b) and assigns values to the private ints (x,y) within the class
// this is possible because it is a public function within the class
cout << "area: " << rect.area(); // outputs the calculated area
return 0;
}
ok, as i started to explain all this to myself in the form of comments, i think i can wrap my head around the logistics as far as how values are passed. am i doin it right? its still a pretty lengthy concept to grasp, i understand all the ideas expressed as individual concepts, putting them all together (and making it work in my head) is fucking my brain up.
You seem to understand everything spot on. Or at least you do a good job of echoing the textbook description of what's happening. It's hard to tell which. XD
How I like to think about it, is that classes are nouns/things. You can then create an "object" of that class which is like a specific instance of that thing.
But I don't know if that clarifies anything for you or not XD
actually, i dont have a textbook. all i have is codeblocks and my two reference sites (cprogramming.com and cplusplus.com)
the post ^above^ about took me an hour and a half to write up because most of the time i was digesting the code line-by-line, then writing the comments associated with each line.
How I like to think about it, is that classes are nouns/things. You can then create an "object" of that class which is like a specific instance of that thing.
for example:
defining a class is like drafting a blueprint of a building
creating an object is like building that building, perhaps even multiple times, at different locations or with different values, whatever you want!
Yes you can. That is known as "implicitly inlining".
Inlining functions is an optimization thing. If a function is inlined the compiler can sort of copy/paste the function body into code that calls it. This removes some overhead involved with calling the function and also allows for further optimizations within the calling function. The tradeoff is that the generated code might be larger.
There are other tradoffs, too. If the class is defined in a header file and your functions are inlined, you would need to recompile every cpp file that #includes it whenever any of the code for those functions changes, which increases compile time.
ok, it looks like i have a bunch more learning to do (which is NOT a bad thing :)
i have a very vague sense of what exactly you are saying, but im catching on with the whole c++ bit pretty well. it just takes time for me to "absorb" it.
well, no sense hiding it from me, its good to know its possible. but for the wellfare of my brain, i will not worry about inlinging for right now, and i'll stick to the tutorials and vast amounts of exercises across the net.