Sign in to follow this  
burgert

classes

Recommended Posts

im currently in the process of learning c++ im learning about classes im a littel confused i know classes are objects so does that mean if you have a game and there is a good guy and a bad guy would you make a class goodguy and badbuy with there x y coordinates or something is that what classes are used for?

Share this post


Link to post
Share on other sites
Classes are a tool to create organized code. They allow you to think of groups of data as objects. We think in reality in terms of objects (I see a table, not a collection of molecules) and so when we're coding, we can create a class that reflects what we think naturally.

You're right that you could have a GoodGuy class and a BadGuy class. You might even have a Guy class that GoodGuy and BadGuy derive from. (Google inheritance.)

You should watch out though. A lot of beginners have one class, then three that derive from that one class. The base sets up what data members and functions this type has, then the children differ in the default values of the data members. This is wrong because the types are in no way different. Differing in the initial values isn't a good justification for creating a new class. You may find a Guy class is all you need.

Share this post


Link to post
Share on other sites
Quote:
Original post by burgert
im currently in the process of learning c++ im learning about classes im a littel confused i know classes are objects so does that mean if you have a game and there is a good guy and a bad guy would you make a class goodguy and badbuy with there x y coordinates or something is that what classes are used for?

No, a class is not an object itself (at least, not in C++). It's a blueprint for objects. It's like having a drawing for a house - you don't yet have a house, but with that drawing, you could build any number of houses.

Quote:
Original post by Splinter of Chaos
You should watch out though. A lot of beginners have one class, then three that derive from that one class. The base sets up what data members and functions this type has, then the children differ in the default values of the data members. This is wrong because the types are in no way different. Differing in the initial values isn't a good justification for creating a new class. You may find a Guy class is all you need.

Quoted for emphasis.

A while ago I worked on a turn-based strategy game, where each side had about 10 different unit types. You could create a class for each of these types, but what we did was simply writing one Unit class. Different types just had different values for movement speed, range, damage, and so on. Actually, we stored that data somewhere else, because it would always be the same for the same type, (the Unit class only contained information like position, current hitpoints, ammo left, etc.) but that's besides the point here.

Share this post


Link to post
Share on other sites
As Captian P said classes are not objects, classes are more like specific types which you make objects from.

For example suppose we have a class "Human". You and I will be objects from that class. The class Human has different properties like name,age,height,hair color... . You and I who are different objects from the class have are own values for those properties. You have your own name,age and height and I have my own. Every other person is also an object from the Human class (Every other person is a human).
So you see classes are just theoretical and don't exist as a 'thing'. You can't say "I talked to Human" because Human is a class and doesn't point to anybody but you can say "I talked to John" because John is an object and specifies someone in reality.

I hope I've managed to help

Share this post


Link to post
Share on other sites
Quote:
Original post by burgert
i get it know i think so classes hold objects and each object has variables and functions that manipulate the variables


Actually, classes hold no objects whatsoever. Objects however can hold objects.

As said before: A class in a blueprint for a house, a boat, or perhaps a human. If you look at the human genome, you could say that's the class for a human, because it describes how a human works.

If you're an architect, you make blueprints of houses. And in this blue prints, you use other blueprints, such as a kitchen, or a bathroom, or even bricks. But none of these are objects yet, they're all blueprints.

Once you got the blueprints done, you bring them to a construction worker(We as programmers are bit odd, we're both architects and construction workers at the same) who will turn them into an object(For instance, you call new on the object, and this create a new object).

The best way to look at classes is to see them as little black boxes wrapping real world objects. A good class would be House, or Horse, or Rocket or Cellar Door(Beautiful word btw). And a door could have a Knob.

As for your good initial question: You could make a class named Guy which have the x/y coordinates, and perhaps a little flag stating if they're a good guy, or a bad guy, or perhaps even neutral.

Toolmaker

Share this post


Link to post
Share on other sites
im geting a error two or more data types in declaration of main of line 10
#include <iostream>
using namespace std;
class Cat
{
public:
unsigned int age;
unsigned int weight;

}
int main() // line 10
{

Cat bob;
bob.age = 4;

}

Share this post


Link to post
Share on other sites
Quote:
Original post by Alastair Gould
Unless you get into objective c and smalltalk, then classes are in fact objects as well.

Or any language that supports reflection, generally. Classes are typically an instance of the class Class, then.

@burgert:
Classes describe the objects created from them. That's pretty much it. Learning to use them effectively takes a little time - and they're not always appropriate - but they're a powerful and useful tool.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this