Jump to content
  • Advertisement

Archived

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

Gothy

classes?

This topic is 6058 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!