Public Group

# C++, structs vs classes/objects

This topic is 4871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Im a student in 2nd year university comp sci degree, we have mostly looked at java when it came to programming but we've done a few weeks of C++ translating java to C++ mostly. Structs were never taught to us in class, but I have been doing my own reading and they seem fairly common in many sample codes I have seen. Every example code I have seen I could program with classes/objects instead, does anyone know the advantages/disadvtanges of using classes vs structs? Structs seem a little easier to code for a lot of easy examples so far, but thats about all ive noticed. but its a very new idea to me so Im sure im missing something obvious :D

##### Share on other sites
I'm no expert, but it seems to me that each should be used where applicable. If you have some data that is related, like the color values of a pixel, maybe 'unsigned short r, g, b;', then maybe you hsould take it, and put it into a structure. Now, if you have a few functions that work explicitly on pixel color data, then you would probably want to put those variables into a class, along with the functions that work on them. It's all a matter of what is more logical. Since structs cannot contain functions (can they? I'm fairly certain they can't, although I may well be wrong here), they should be used for sets of data, while classes should be used for grouping data and the functions that act on them, in logical formations.

Please keep in mind that again, I'm no expert, so don't take my word for this, rather, hope someone who's more experienced then me replies here [smile]

[BTW, I don't use structs at all, except when dealing with things like the Win32 API, it just doesn't seem necessary to me, when my classes illustrate the way things work better.]

[EDIT] Curses, I forgot, classes should also be used to keep data private from things that shouldn't be messing with it, because in structs, everything is public to anyone that wants to mess with it.

##### Share on other sites
In C++ Structs are exactly the same as classes, with one minor difference -
with structs default visability is public, with classes default visability is private.

##### Share on other sites
Structs are generally for holding groups of variables / information to make passing like information throughout your program easier (ie. you don't want to pass 80 variables to a function when you can pass 1 structure holding them). They are a nice way of simplifying code when your problem doesn't merit a class.

I'm still kinda newb but i think that's right...

##### Share on other sites
One other difference that no one has meantioned and is ESSENTIAL to understanding them is that Classes have FUNCTIONS!!! (wooo! yeaaa.. (ok I love classes and functions,))

When C was made there needed to be a way to make structures with more then just the base data types (back then there's was JUST the basic data types) So they made structs. Structs contain information on stuff that need to stay together. Very useful at the time, but as time progressed people found it less useful then originally thought as you can edit it with any thing that has access to the struct and that gets confusing, as well as the fact you still had millions of functions and all of them were called about the same way.

When C++ came around they thought they needed more work to make Structs into actual objects, and thus came the ideas of private and public variables as well as functions. This allowed the structs to no longer have everyone able to access the innermost workings of it, and allowed a VERY specific ways to work with classes as objects instead of just data lumped together.

Basically If your going to use C++ and no fear of porting you can always use classes. If you want to know when to use each, Think of it as this "do I want to have anything hidden or do I want to have any specializaed functions lumped with it?" If so use a class, if you don't mind everything being public, a struct will do the same thing as a Class, though a class does allow those wonderful functions to be added on to the end.

Now you want a brain killer, look up Unions (a C object). That's an intense concept when you first read that one.

##### Share on other sites
The difference between structs and classes in c++ has already been mentioned. However, in c, they cannot contain functions. Another cool thing about structs is that if you don't have any functions/constructors/etc and no dynamic variables, you can do some quick easy storage of a struct to a file. Maybe like this:

#include <fstream>#include <iostream>using namespace std;struct RGB_pixel{  float r,g,b;};int main(){  RGB_pixel pixel;  pixel.r = 1;  pixel.g = .5;  pixel.b = 0;    ofstream out;  out.open("file.dat", ios::binary);  out.write((char*)&pixel, sizeof(pixel));  out.close();  RGB_pixel pixel2;  ifstream in;  in.open("file.dat", ios::binary );  in.read((char*)&pixel2, sizeof(pixel2));  in.close();  cout << "Pixel 2 values" << endl;  cout << "R: " << pixel2.r << " G: " << pixel2.g << " B: " << pixel2.b << endl;  return 0;}

Not 100% sure if you cannot do that with a struct/class with functions in it, testing right now.

edit: Made the code actually work. :)

edit2: It seems to me that functions don't interfere with doing raw writes and reads of the struct. Very interesting, and handy.

##### Share on other sites
Quote:
 Original post by kinglinkOne other difference that no one has meantioned and is ESSENTIAL to understanding them is that Classes have FUNCTIONS!!! (wooo! yeaaa.. (ok I love classes and functions,))

No one else mentioned functions as part of classes? Did you not read my masterful post? [tears] I've found it helps to read the replies aleady posted before making assumptions [grin]

##### Share on other sites
Structs can also have functions however. As far as the language is concerned they are exactly the same apart from as someone has already said, structs have default public visibility and classes have private. Generally use structs for data only structures and classes when the structure is going to have methods.

##### Share on other sites
Quote:
 Original post by Rebooted Generally use structs for data only structures and classes when the structure is going to have methods.

why?
what would it be the advantage???

##### Share on other sites
Yes, that's a common misunderstanding between C and C++. When C++ came about, structs were also changed into classes, just with public as default. They aren't the same structs as in C.

• 35
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631358
• Total Posts
2999531
×