Jump to content
  • Advertisement
Sign in to follow this  
bknouse1344

Classes VS Functions

This topic is 4963 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

I read the Cone3D classes descriptions, along with some other tutorials, and for the most part it feels like classes are just a way of organizing fucntions! Is this all that they are for? Originally I had figured that was what different files were for, grouping functions.

Share this post


Link to post
Share on other sites
Advertisement
classes hold variables and functions. and you can have more than one instance of a class eg

class animal
{
private:
int legs;
public:
void set_legs(int num)
{
legs = num;
}
}

...


animal cat;
animal snake;


cat.set_legs(4);
snake.set_legs(0);

this way you don't have to create a variable for snae legs and one for cat legs. (this is helpfull when you have a lot of variables and a lot of instances. of the class).

Share this post


Link to post
Share on other sites
Quote:
Original post by bknouse1344
I read the Cone3D classes descriptions, along with some other tutorials, and for the most part it feels like classes are just a way of organizing fucntions! Is this all that they are for? Originally I had figured that was what different files were for, grouping functions.


In a way you're right... but if you think that is all classes are for you are missing the point.

C++ in particular allows mixing paradigms, so there is no real requirement to have any classes at all... In Java, C#, etc it's all very centric.

I could post a long reply saying why you would want to organise data with functions, go on about encapsulation, applying patterns, etc. But that would be no good, because really you need to read a decent book about it. Not necessarily a game programming book either, often those demostrate graphical tricks without care to construction of the whole code.

If you are too cheap/skint to buy a book and can't be bothered to go to a library try a Bruce Eckel book for starters, available legitimately free at http://mindview.net/Books

Share this post


Link to post
Share on other sites
A class represents a type of object and the operations that can be performed on it. It should not be a group of vaguely related functions (I think Java's Math class is a travesty), but a collection of data and a public interface which provides suitable operations in such a fashion as to maintain necessary invariants.

Edit: Here is a nice interview with Bjarne Stroustrup which touches on the subject.

Share this post


Link to post
Share on other sites
Basically, classes are blueprints for constructing objects. They are a way to clump your data and the routines (functions) that operate on them into a single packet that models some aspect of the problem at hand, and hides (encapsulates) its implementation, exposing only a constrained set of routines (its public interface) so that its users don't need to worry (or even know anything) about what happens under the surface.

In a more abstract sense, procedural languages (such as C) emphasize procedures, that is, functions, that operate on some dumb lumps of data, whereas object-oriented languages instead marry functions and data into active, "smart" entities (objects) that co-operate by using each others' services and take care of their own data, maintaining their state and preventing others from fiddling with their intestines.

Objects can contain other objects, and classes can inherit from other classes, so that a derived class is everything its base class is, and possibly more. This makes it possible to use an object of a derived class in a context where a base class object is expected! The most important thing is, however, that a derived class can rewrite (override) some of the routines of the base class, thus yielding different (but analoguous) behaviour depending on the type of the object used. This feature of dynamically selecting which routine to run is called polymorphism and is very much the essence of object-oriented programming.

Share this post


Link to post
Share on other sites
Also, this is more like classes vs structures, but i noticed that marius in the cone3d tutorials uses structures too...he said mostly they are the same thing as classes except classes can hold functions?

Share this post


Link to post
Share on other sites
In C, which does not have any concept of classes, a struct is simply a structure holding a number of variables (no functions).

C++ introduces the concept of classes, and adds the ability for such to hold functions. A struct is a class, though the new keyword class is generally preferred. Most likely for the sake of backwards compatibility, all members of a struct are public by default (while class members default to private); perhaps for consistency, structs also default to public inheritance.

All the same, in C++, a struct and a class are the exact same thing except for access modifiers, and whatever the one can do, so can the other (this includes functions, inheritance, polymorphism, et cetera).

Many C++ programmers use structs only as "POD structures" (Plain Old Data) that hold data but have no functions, thus resembling classic C structs. This has unfortunately lead to a misconceptions that structs cannot hold functions in C++—they can, it is only a matter of convention. (For the record, I occasionally like structs with functions, e.g. for interface inheritance where I want everything to be public, or simple utility classes not intended to be used except in the defining file.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!