Archived

This topic is now archived and is closed to further replies.

Gothy

classes?

Recommended Posts

hi everybody, I''m reading cpp in 21 days and began recently on the subject classes. I just can''t figure out why to use classes i''ll give an example from the book an try to explain why i don''t get it. 1: // Demonstrates declaration of a class and 2: // definition of class methods, 3: 4: #include <iostream.h> // for cout 5: 6: class Cat // begin declaration of the class 7: { 8: public: // begin public section 9: int GetAge(); // accessor function 10: void SetAge (int age); // accessor function 11: void Meow(); // general function 12: private: // begin private section 13: int itsAge; // member variable 14: }; 15: 16: // GetAge, Public accessor function 17: // returns value of itsAge member 18: int Cat::GetAge() 19: { 20: return itsAge; 21: } 22: 23: // definition of SetAge, public 24: // accessor function 25: // returns sets itsAge member 26: void Cat::SetAge(int age) 27: { 28: // set member variable its age to 29: // value passed in by parameter age 30: itsAge = age; 31: } 32: 33: // definition of Meow method 34: // returns: void 35: // parameters: None 36: // action: Prints "meow" to screen 37: void Cat::Meow() 38: { 39: cout << "Meow.\n"; 40: } 41: 42: // create a cat, set its age, have it 43: // meow, tell us its age, then meow again. 44: int main() 45: { 46: Cat Frisky; 47: Frisky.SetAge(5); 48: Frisky.Meow(); 49: cout << "Frisky is a cat who is " ; 50: cout << Frisky.GetAge() << " years old.\n"; 51: Frisky.Meow(); 52; return 0; 53: } this is one of the examples and has the output: Output: Meow. Frisky is a cat who is 5 years old. Meow. so they need 53 lines only for that output. you can also program it as: #include void main() { int age=5; cout << "Meow" << endl; cout << " Frisky is a cat who is " << age << " years old " << endl; cin.get(); cin.get(); } that is 11 lines of code with the same result and much easier to understand. So can somebody please point me why classes are such a great thing in c++ or can somebody show me some understandable source of a program that uses classes in a logical way?

Share this post


Link to post
Share on other sites
The point of the program isn't to output "Meow" or whatever.

The point of the program is to get you to understand that c++ uses classes as templates for user-defined data types.

Like structs, but with member functions and such.

We think of the world in terms of objects and actions that are performed on objects.

You don't need to know how an internal combustion engine works to drive a car.

*EDIT* Oh yeah, I personally wouldn't recommend any of those "21 days" or "Dummies" books to anyone who is serious about learning to program.

Edited by - daerid on February 18, 2002 5:45:43 PM

Share this post


Link to post
Share on other sites
Is that book written by Jesse Liberty? He gets some flak, and you''ve just provided an example of why. The Cat class is a completely awful demonstration of why you would write a class.
I believe you have to see the need for OO, and understand what problems it solves before you can really "get it". It might be worth your while searching for a better book.

--
The Dilbert Principle: People are idiots.

Share this post


Link to post
Share on other sites
allright, i think i'll have to assume that classes are better in big programs but i notice that's much more harder for me to understand things if i can't see the advantage of it.

btw why wouldn't you recommend those books to me, i already bought a book about c++ of about 600 pages to learn from in my home language (dutch) but i found it much easier to understand that 21 days book because all the names are in Englisch so i can understand tutorials about opengl or win32 programming much better.

Oh yes sabreman that's the problem i'm having, i can't see the use of it so i can't understand so i can't learn it

Edited by - Gothy on February 18, 2002 6:03:17 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ok basic example of when I would use a class.

Say you have a game with race cars, other than the number on the cars the abilities are the same. So instead of writing a function for each car I would make a single class and create an individual object for each car.

Share this post


Link to post
Share on other sites
I wouldn''t recommend Jesse Liberty''s books to you because he doesn''t have a reputation as a good author. The example you showed with his idiotic Cat class is a reason why. Nobody writes classes to make Cats say "Meow", so how are you supposed to understand why you would want to write a class from the example?

Check out www.accu.org for book reviews.

--
The Dilbert Principle: People are idiots.

Share this post


Link to post
Share on other sites
I have teach yourself c++ in 21 days myself and the Cat class is lame but its the first thing he teaches about classes and has to start somewhere he then goes onto operator overloading and demonstrates your own data types etc and it gets better...

Share this post


Link to post
Share on other sites
so it''s ok to stay with this book? if you think another book would be much better could you please give me name of it because i don''t dislike this book, i actually heard a lot of positive things about this book.

I also want to ask that if i finished reading this book, do i have a solid understanding of c++ or are there alot of other things to learn then? and can i continue with opengl or directx then?

Share this post


Link to post
Share on other sites
I'll probably get flamed for this, but if you don't see the point of classes, I think it's best you skip them for a while. Just make sure you understand the basics really well: for-loops, if, functions, pointers, structs, seperate sourcefiles, etc. It probably won't hurt to know a little bit about templates either. Once you've written a few larger programs that actually do something useful you'll probably see the use of encapsulation and modularity, and classes will suddenly start making sense. You don't actually need classes to write decent programs, it's just a different (and usually better) approach to solve a problem and organize the code.

Note that I'm not saying you should use C instead C++. And I'm not saying you shouldn't USE classes either, just that you don't have to WRITE them yourself. In fact, I think it's a good idea to start using fstreams, string, vector, list etc. right from the beginning.

btw: You should really check out the book reviews at www.accu.org, as SabreMan told you. I've heard good things about 'Accelerated C++'. If you're just starting out, it's very important to pick a book that teaches good techniques and style, rather than a book that includes a lot of detailed language information.

EDIT:
About Teach Yourself ANSI C++ in 21 Days:
quote:
Francis Glassborow on ACCU
Nobody should be under any illusion that working through this book will turn them into anything
more than a C++ novice.



What dutch book are you reading?

Edited by - kvh on February 19, 2002 12:11:35 PM

Share this post


Link to post
Share on other sites
The dutch book i have is called: "Aan de slag met C++ derde herziende uitgave" writed by Gertjan Laan and Publiced by Acadamic service.
ISBN 9039510849.

I think i'm going to continue with this dutch book.I saw that the next chapters in cpp in 21 days also contain classes so it would also harder for me to understand it. I stopped reading that dutch book because i thought the English names differ much from the dutch names but i found out that the names are much te same.

I must say that Gertjan laan explains all the things including classes explain much better(im not saying cpp in 21 days is bad)I Just looked a couple of seconds at the subject.
Gertjan Laan tells about Classes later in the book (i'm not at that point yet) because he says it's a more advanced subject than pointers and structures.

Edited by - Gothy on February 19, 2002 12:18:48 PM

Share this post


Link to post
Share on other sites
Looks like a decent book to start with. It doesn''t cover any of the STL however, and that''s a pretty big minus IMO. (STL contains most of the pre-written classes I mentioned im my previous post) You can download a free version of ''Thinking in C++'' at Bruce Eckel''s website, it''s supposed to be pretty good so you might try that one.

I''m sure the english terminology won''t be much of a problem, you''ll get used to it while reading english forums such as this one.

Share this post


Link to post
Share on other sites
thanx for al the posts.

I''ll just stick to the dutch book and when i''m done with it i''ll go to the book that kvk mentioned for the STL stuff.

btw kvk do you also know that book "aan de slag met c++?" i just looked in the book and i indeed can''t find something about STL. I''ll keep the book you mentioned in mind for it

Share this post


Link to post
Share on other sites
Actually I only have one dutch C++ book (my first), ''Basiscursus C++'' by Leen Ammeraal, which is pretty crappy IMO. I just skimmed through the (online) table of contents of ''Aan de slag met C++''.

Share this post


Link to post
Share on other sites
I actually had the same book as you did but i just didn''t like the way the book was writed and because i just got it out of the local library i brought the book back and thought ill wait with it a little while. A month later i ordered this book at bol.com and i found it much easier to read

What are you learning now or aren''t you finished c++ yet and wat kind of programs would you think are useful for me to understand classes better.

Share this post


Link to post
Share on other sites
Right now I''m reading ''Modern C++ design'' (again), which is mostly about funky stuff you can do with templates. But you''re probably not ready for that yet. I don''t think anyone can really say they ''finished'' C++. The more you learn, the more you realize how much there''s still left for you to discover. And most programs need additional libraries, because C++ doesn''t do graphics, networking, GUI''s, etc all by itself. You need to get used to those too. Just don''t try rushing things and take one step at a time. It''ll be worth it in the end (I hope ).

Classes are especially useful in GUI frameworks, components like windows and buttons translate nicely into the OO model. But that would probably mean you''d have to learn windows programming, and I think that would only confuse you even more at this time. Maybe you could try to implement some data structures like a linked list and a stack or something, just as an exercise.

If that''s too boring for you then some simple network programming (Beej''s Guide to Network Programming) or graphics with OpenGL and GLUT might keep you motivated. Those aren''t necessarily good for learning classes, and they might be a bit too much for now, but they won''t look as alien as windows code. Like I said before, it''ll click at some point.

Share this post


Link to post
Share on other sites
I''ve already taken a look windows programming (because of the nehe lessons on nehe.gamedev.net) and i indeed didn''t understand much of it. I now know how to make a triangle or a squad in opengl but that''s all. I just thought lets begin with an English c++ book and maybe then i can understand windows programming better.(because the possible confusion of dutch an English words)
I got to classes and didn''t understand it really because i couldn''t see the use of it but that''s where this whole topic is about but i think you alrady noticed that

I think i finish this book first and then look at that STL you mentioned and ofcourse OpenGl.

Thanx for the posts, they really helped to get some things out of the way

Share this post


Link to post
Share on other sites