could someone please clarify classes for me, i do have some understanding :
class myclass
{
public:
void print() {cout << "This is a class";
};
int main()
{
myclass print1
print1.print();
return 0;
}
C++ classes n00b
Re:C++ classes n00b
First of all, welcome to the board.
Then, c++ classes.
A class is a type of object, which can be combined to form complex structures of interaction, which abstract away complex algorithms through easy-to-understand object structures and associations.
The less formal definition, and one that's more practical, is:
A class is a bunch of functions and variables that are "tied together" with only the outside showing. So, instead of a "malloc" function, a "free" function and a "control" function, plus initialization and finishing functions, you get a class called Allocator. The Allocator class then has child functions (member functions) called "malloc", "free" and "control", plus a constructor and destructor. When the object is constructed, memory is allocated for it and the constructor is called. The constructor sets stuff up that the other functions in the class need (such as here, it'd set up the memory information and what's in use and what isn't).
The functions themselves receive an implicit pointer to the object they're to work on, which is the "this" pointer. So, you can make two allocators, one of which works on memory reserved for John and one of which on memory for Sally. That way, they can both be used at any time you like, and you can at run-time specify which should be used. Instead of swapping out a bunch of functions and so on, you just swap an object for another.
The destructor, finally, will free up any used resources (such as memory or control information) and clean up after itself (tell other things that it's no longer available).
This is a generic object. You can do this all in C as well, but you'd have to make the pointer to the object explicit. Also, C++ offers more syntactic sugar that you can use for your benefit, such as complex subaspects of objects. If you're interested in more of this, it's probably a lot easier to go to a library and to lend a book on C++ or on object-oriented design.
If you want to learn from the internet, here's a list of keywords you can start fishing for. When I call out more than one word, put them between quotes for better effect:
- Polymorphism
- Inheritance
- Multiple inheritance
- dynamic_cast
- design pattern
- tree
- standard template library
- iostream
- overloading
- operator overloading
These are just a random 1-minute brainstorm on generic object-oriented terms that you can search for to get specifics explained. If you have any more specific questions, please do come and ask. I think that you should read a lot more about it first, from tutorials to things that just try to do one of these things.
Finally, try to get some OO programs working first. Try to figure out why OO is being used, instead of forcing it upon your own programs. When you have a new hammer, everything looks like a nail. When you can't use the new hammer properly, nothing looks like a nail. First figure out what a nail looks like, then understand why the hammer works best.
Then, c++ classes.
A class is a type of object, which can be combined to form complex structures of interaction, which abstract away complex algorithms through easy-to-understand object structures and associations.
The less formal definition, and one that's more practical, is:
A class is a bunch of functions and variables that are "tied together" with only the outside showing. So, instead of a "malloc" function, a "free" function and a "control" function, plus initialization and finishing functions, you get a class called Allocator. The Allocator class then has child functions (member functions) called "malloc", "free" and "control", plus a constructor and destructor. When the object is constructed, memory is allocated for it and the constructor is called. The constructor sets stuff up that the other functions in the class need (such as here, it'd set up the memory information and what's in use and what isn't).
The functions themselves receive an implicit pointer to the object they're to work on, which is the "this" pointer. So, you can make two allocators, one of which works on memory reserved for John and one of which on memory for Sally. That way, they can both be used at any time you like, and you can at run-time specify which should be used. Instead of swapping out a bunch of functions and so on, you just swap an object for another.
The destructor, finally, will free up any used resources (such as memory or control information) and clean up after itself (tell other things that it's no longer available).
This is a generic object. You can do this all in C as well, but you'd have to make the pointer to the object explicit. Also, C++ offers more syntactic sugar that you can use for your benefit, such as complex subaspects of objects. If you're interested in more of this, it's probably a lot easier to go to a library and to lend a book on C++ or on object-oriented design.
If you want to learn from the internet, here's a list of keywords you can start fishing for. When I call out more than one word, put them between quotes for better effect:
- Polymorphism
- Inheritance
- Multiple inheritance
- dynamic_cast
- design pattern
- tree
- standard template library
- iostream
- overloading
- operator overloading
These are just a random 1-minute brainstorm on generic object-oriented terms that you can search for to get specifics explained. If you have any more specific questions, please do come and ask. I think that you should read a lot more about it first, from tutorials to things that just try to do one of these things.
Finally, try to get some OO programs working first. Try to figure out why OO is being used, instead of forcing it upon your own programs. When you have a new hammer, everything looks like a nail. When you can't use the new hammer properly, nothing looks like a nail. First figure out what a nail looks like, then understand why the hammer works best.
Re:C++ classes n00b
Thanks for the help, i will look up the key words, and try to wrap my head around it. It would be really nice if i could get good at it. Thanks again. Off to learn c++.
Re:C++ classes n00b
I would like someone to point me to some excercises, or some ideas, so i can learn c++ better. Please give me some ideahs of programs to write to practice with. Thanks.
P.S. The forum doesnt recognize my username, i registered yesterday, was able to log in, now it wont let me. oh well.
"Windows is like an overpatched balloon, ready to blow up in your face at any time"
-- Quote from SolarOS forum
P.S. The forum doesnt recognize my username, i registered yesterday, was able to log in, now it wont let me. oh well.
"Windows is like an overpatched balloon, ready to blow up in your face at any time"
-- Quote from SolarOS forum
Re:C++ classes n00b
I don't think you're in the "practice" stage yet. I'd recommend a good book on C++ first. Try cplusplus.com as a starting point.
Every good solution is obvious once you've found it.