Friday, 25 February 2011

Structures vs Classes

After my last post where I stated I don't know an easy way to emulate C++ classes in C, I think I read a subject in my book that will allow me to do so.


Please note I am not posting this as a tutorial, just a thought I have had that may be right or may not be right at all. It helps me to write things down.

I know, why don't I just learn C++ instead of C?

I explained in my last post, I want to get my head around a non-object orientated programming language first, then I will move on to C++.

Anyway, today I read a chapter on structures which, as far as I can tell from my limited reading of C++ material, is pretty much the same thing as classes.

Let me try to explain, first what I understand of classes;

A class allows the programmer to say, I have a class named bubble, which has properties named colour and pointValue.

Now, anytime the programmer wants to add an instance of the bubble object in the program he just calls the class and assigns a it a name he can then edit the values of that particular instance. Like so:



class bubble //creates a class named bubble
{
char[] colour
int pointValue
}

int main()

{

bubble redBubble; //creates an instance of bubble

redBubble.colour ( red ); //assigns red to the property colour

redBubble.pointValue ( 10 ); //assigns value of 10 to pointValue

}


Another note, I am inexperienced when it comes to any sort of programming so please be aware that these examples are likely to be incorrect and I am totally aware of that.

This allows me to create new bubble objects easily as they have predefined properties that are easy to edit. It also allows me to reuse the objects easily without having to code all the properties over and over.

As for structures, they seem to work in pretty much the same way, except the separate versions of each structure are created as variables and have other variables within them.

You create a structure and define the 'layout' by adding members, which are pretty much properties.

You then create 'instances' of the structure and edit the members within it. Like so:



struct bubble {
char[] colour;
int pointValue;
};

int main()

{
bubble redBubble; //creates a structure variable named redBubble.

redBubble.colour = red; //assigns red to the colour property.

redBubble.pointValue; //assigns 10 to the pointValue.

}


As you can see, classes and structures look pretty much the same although, both can have very different uses. However, I can see no reason why neither of them would work as a bubble object in my app.

I need to get a bit of practice and testing under my belt before I can confirm this, but I can see no reason to disbelieve.

Hopefully someone who knows a bit more about programming will come along and confirm or otherwise.

Disclaimer: I am deeply sorry if any of my code or theories offend you in any way. I understand how you feel, I play guitar and get deeply offended if I hear an out of tune guitar.








No comments:

Post a Comment