Archived

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

code_fx

need help with classes

Recommended Posts

code_fx    122
I am grasping bit by bit of the class concept but can some one explain in a nut shell what makes them so usefull... class Cat { public: //the programm doesnt come this far int GetAge(); //when i trace into...what is there void SetAge(int age);//purpose void Meow(); private: int itsage; }; int Cat::GetAge() // I know this is an acessor functions { // but can someone tell me how and why return itsAge; // its reached by the main() } void Cat::SetAge(int age) { itsAge = age; } etc

Share this post


Link to post
Share on other sites
Fruny    1658
Try with this.


void main()
{
Cat felix;
felix.SetAge( 2 );
int i;
for( i = 0; i < 20; ++i )
felix.Meow();

cout << "Damned " << felix.GetAge() << "-year old cats."
}


And in a nutshell what makes them so useful

- They are data types. And type checking is your friend. (your mileage may vary)
- You can hide implementation details behind an interface and enforce this interface. (Though it takes some work to really do it correctly).
- You can substitute one class for another, so long as they have something in common (e.g. Cats and Dogs are Pets) (restrictions may apply)
- They offer sweet syntactic sugar (operator overloading).
- You can reuse bits of code from one class in another... or mix and match (Caveat: Don't do it if you don't know what you're doing.)

- Read "Modern C++ Design" by Andrei Alexandrescu and get your mind blown away by what he does with classes and templates.

etc.

Edited by - Fruny on January 30, 2002 3:43:14 AM

Share this post


Link to post
Share on other sites
DirectAndy8    122
The main benefits of classes as I see it is that functions and data are wrapped up in the same object.

The standard example is that a car has data like Engine Capacity, Wheel Size, Fuel, and so on. But it also has behaviour like Accelerate, Brake, Turn, and so on.

All wrapped up together, this makes a neat "car" class. Without classes, you could have all the data in one place... but then the functions, which work on that data, would be ''floating in the air'' - they wouldn''t be linked with the car in any way, which might mean you wouldn''t know what the car can do, or you might try to Accelerate a house, or something. So conceptually they are very strong (they model the real world), and also help in large projects because you don''t have to worry about all data being global.

This then means that you can start doing clever stuff like hiding data, treating the object as a black box, and class hierarchies - for example, cars share a lot of traits with buses, motorbikes and even planes, so you might have an abstract class known as "vehicle" that holds a lot of the data and then make concrete classes based around the different types of vehicle you can get.

Share this post


Link to post
Share on other sites
code_fx    122
I finally understand simple classes and can program simple ones... Well back to the books and forward to the next chapter... thank you for your suggestions and... well thanks

Share this post


Link to post
Share on other sites