Sign in to follow this  

c++ classes

This topic is 3663 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'm not quite sure of how classes work. what's the point of them? r they necessary? they don't seem to effect a project i'm working on if i don't use them, so why r they there? i'm just curious cuz it bugs the heck out of me with it being there and me not knowing what it's for. thanks in advance for any feedback

Share this post


Link to post
Share on other sites
The most obvious reason that classes are useful is the fact that they allow you to couple the idea of an objects data with its implementation. Also more advanced features are available through classes. These features include polymorphism, encapsulation, abstraction and inheritence. Thoes happen to be the key components of object oriented programming.

If you are not using these features thats fine, because object oriented programming is entirely equivalent to imperative techniques -- that is to say they can both be used to write code with the same functionality. However depending on the situation, object oriented programming can save you a lot of headaches and you're not using the language to its full potential.

Share this post


Link to post
Share on other sites
The thing that's always stuck with me most is this: Classes were made for your convenience, not the computer's. Can't remember who said it, but know that the whole point is to simplify your life.

It's inefficient to have these variables:
car_color;
car_make;
car_model;

Why not have
class car
{
color;
make;
model;
};

Isn't that so much easier to think of?

Now what if I want many cars?

car1_color;
car1_make;
car1_model;

car2_color;
car2_make;
car2_model;

car3_color;
car3_make;
car3_model;

car4_color;
car4_make;
car4_model;

...

Or I could just do this:
car1;
car2;
car3;
car4;
...


Using object often seems pointless when you're just starting out, but the truth behind the simplicity of object is that you can automate the process of building an object. More reliable, more readable, better. What if you needed one thousand cars? Classes make complexity simple.

As for how they work:
Well, basically a class is a list of variables that make an object, and a list of implementations. I believe (but don't quote me) that c++ has the variable of a class instance point to the first variable in the memory. If you want to use a variable in the class, c++ returns the variable at (the starting location + the size of all the variables before it). Might be hard to visualize just by me saying it. Get a piece of paper (I'm serious, get it) and draw a big rectangle. Draw four horizontal lines and the four rectangles in the bigger one should represent an integer. Draw eight more lines and they should represent a long int. I believe that's how the computer views a class, but don't quote me.

Share this post


Link to post
Share on other sites

This topic is 3663 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.

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