What is the Difference Between Struct and Class?
Page 1 of 1
Abiguous
12 Replies - 20487 Views - Last Post: 12 May 2010 - 08:57 AM
#1
What is the Difference Between Struct and Class?
Posted 09 May 2010 - 05:50 PM
Between C and C++, what is the difference between Structures and Classes? Google said that they are nearly the same. Then why did Bjarne Strostrup (did I spell that correctly?) bother with C++?
THANKS!
-Masoug
Replies To: What is the Difference Between Struct and Class?
#2
Re: What is the Difference Between Struct and Class?
Posted 09 May 2010 - 05:59 PM
Lets play the class/struct game!
1) Object such as a vehicle with the ability to drive, honk the horn, and multiple attributes like color, model, make and the properties to get/set these values. This would be a class.
2) An object for storing a color. This would be best served by a struct.
3) A person. Probably would be a class because there is a lot of info stored about a person... name, age, height, weight and lots of actions they can do.
4) An object describing a web site's total and daily hits. Probably a struct since all you really have is a few integers to keep a tally.
5) Object to represent a file. Most likely a class since there is quite a bit of info you can use to describe a file. Size, name, location and plus may have lots of functions to manipulate that file like read(), write(), copy() etc.
Generally if the object is going to contain a lot of data and actions, go with a class. I personally always suggest a class because you never know what you might need later. Your struct may need to be upgraded and thus will eventually become a class anyways.
Hope this helps!
This post has been edited by Martyr2: 09 May 2010 - 06:01 PM
#3
Re: What is the Difference Between Struct and Class?
Posted 09 May 2010 - 06:18 PM
Quote
Quote
C does not have any OO constructs. It also lacks everything else in C++ like RTTI, exceptions, templates, better type safety, the larger standard library, and with C++0x, auto variables, movable rvalues, etc.
In C++, the only difference between classes and structs is that structs assume public while classes assume private access. For both members and inheritance from a base class.
#4
Re: What is the Difference Between Struct and Class?
Posted 09 May 2010 - 06:21 PM
There is very very little differnce between a struct and class in C++. struct is merely an alias for a class that defaults to public access.
i.e these two constructs are *exactly* the same:
struct MyStuct { int x, y; MyStruct(int x, int y) : x(x), y(y) { } }; class MyClass { public: int x, y; MyClass (int x, int y) : x(x), y(y) { } };
Structs can have constructors, deconstructors, they can have virtual members, they can inherit from classes, they can have public, private, protected members -- the ONLY difference is the class members are private by default, and struct members are public by default.
Sorry Martyr2 you are not a struct vs. class ninja today
#5
Re: What is the Difference Between Struct and Class?
Posted 09 May 2010 - 06:28 PM
Quote
In review:
C++ class vs C++ struct -- same thing different default access modifier.
C++ struct vs C struct -- upwardly compatible, a C struct works just fine in C++, but C++ adds features that are not in C. For example a C++ struct can contain code.
It IS interesting that they are so close... turns out that they are closer than you might think, every C++ program can be compiled into a C progrom. All OOP really is, is a set of rules, you can use OOP programming in non-OOP languages but you have to enforce the rules yourself. OOP languages are much more structured and the language itself enforces the rules for you.
#6
Re: What is the Difference Between Struct and Class?
Posted 09 May 2010 - 06:39 PM
#7
Re: What is the Difference Between Struct and Class?
Posted 11 May 2010 - 08:00 PM
Can you place a class on the stack? Sure. Do you want to 100% of the time? NO! I was not commenting on C having classes or not, I was addressing what is the difference between a class and a struct in C++. And if you people think how they are used is virtually equivalent and placing your heavy objects on the stack, boy, you really need to review your program's performance. C# just expanded on this philosophy and which is why it did create the value type/reference type and introduced garbage collection.
Next because a class is generally carrying around methods and tend to be bigger, this is the reason we have pointers. We do not want to be passing around objects without pointers in C++. Can you without pointers? Again yes, but it is bad practice and bad design to do so. Without pointers, we would be copying objects which again is a horrible deal.
This post has been edited by Martyr2: 11 May 2010 - 08:02 PM
#8
Re: What is the Difference Between Struct and Class?
Posted 11 May 2010 - 08:22 PM
Quote
That just sounds like personal preference. I feel that memory should only be allocated on the heap if it is necessary for the memory to be dynamic. If its known memory there not reason not to allocate it to the stack
#9
Re: What is the Difference Between Struct and Class?
Posted 11 May 2010 - 09:01 PM
Quote
Quote
Quote
Acquiring from the heap is a penalty. When you design types that need to manage large resources, your types should use handles to that resource, as opposed to reserving space on the stack. Which of course is not a concern if resource size is not even known at compile time. But it's a penalty nonetheless.
Quote
#include <iostream> class A { int a; int b; int c; }; class B { int a; int b; int c; public: void doSomething() { c = a + b; } }; int main() { A a; std::cout << sizeof(A) << ',' << sizeof(B)/> << '\n'; }
If you want, I can also pull out elements from the standard (or, you can read the standard yourself), that indicate that functions do not increase class size.
Quote
Martyr2, your entire post is inexplicable. There is absolutely no way you can reach these conclusions based on a reading of any proper C++ text and the standard throughly contradicts you. Furthermore, your statements are disproved by code like above.
This post has been edited by Oler1s: 11 May 2010 - 09:02 PM
#10
Re: What is the Difference Between Struct and Class?
Posted 11 May 2010 - 11:35 PM
Quote
Never did I argue this fact.
Quote
What part of typically do you not understand? Again I said that in the majority of cases, specially when the object is a heavy object you should be using a pointer to it. End of story dude. Any time you declare a "heavy" object and not reference it using a pointer and then pass it around to functions you are recopying it. Again, not an issue if the class is small, but in most situations either a class is heavier than a struct or may eventually be modified to become heavier.
Quote
Hence the reason you should limit what you do put on the stack. The stack is no place for heavy objects. There is a reason we have a heap.
Quote
Of course it is going to cost, but its savings over moving around large objects and making unnecessary copies will be greatly realized. I don't know why you seem to be against initiating a class on the heap with a new keyword and a pointer. That is C++ memory management at its best. This is a GOOD thing.
Quote
Yes a class carries around methods with it. It wraps up functions. You know, the reason we call them methods instead of functions. And when I say they are bigger I am saying that your typical function isn't just going to add 2 + 2, instead they will declare their own variables, make other calls, and consume their own resources.
OBJECTS ARE CONSIDERED HEAVY COMPARED TO STRUCTS! Again, another reason why in C# they end up making a clear distinction between what is a value type and what is a reference type. This came from C++ and the direction it was going.
I am not here to argue the finer details of C++ internals. I was answering the original posters question about structs and classes. I think you just took something personally and think I am out attacking everything you are saying. You claimed I was talking from a C# stand point and while my statements do act similar in C#, the ideas in C# when it comes to structs and classes has its roots in the very fact that structs are considered lighter than classes.
So chill out and be merry.
This post has been edited by Martyr2: 11 May 2010 - 11:37 PM
#11
Re: What is the Difference Between Struct and Class?
Posted 12 May 2010 - 01:45 AM
Quote
From what I have learned till now, the copying happens when you pass objects by value. In C++, you don't Have to use pointers to avoid the copy when using pass by value, instead you can use references:
class heavy { //codes of heavy object }; void test_function(heavy& heavy_object) { //... }
In the above code, I could use a pointer in test_function's argument. But using a reference solves the copying problem and also, avoids using a pointer(which helps safety).
More Information About References
This post has been edited by Anarion: 12 May 2010 - 02:23 AM
#12
Re: What is the Difference Between Struct and Class?
Posted 12 May 2010 - 08:47 AM
Quote
Quote
you are aware that you are allowed to have a pointer pointing to memory located on the stack right. Just because something is a pointer doesnt mean it has to be allocated on the heap.
class Heavy { /*some stuff*/ }; void passObject(Heavy *passObject) { } int main() { Heavy myObject; passObject(&myObject);
there is no requirement that "classes" must be dynamically allocated, either that say that structs must be allocated on the stack.
Quote
which one you thinks bigger?
class Heavy { int a; public: void declare(void) { int b,c,d,e,f,g,h,i,j,k,l; } }; class Lite { int a; public: void declare(void) { } }; int main() { Heavy h_obj; Lite l_obj; if(sizeof(h_obj) == sizeof(l_obj)) cout<<"THERE THE SAME SIZE"<<endl; return 0; }
This post has been edited by ImaSexy: 12 May 2010 - 09:00 AM
#13
Re: What is the Difference Between Struct and Class?
Posted 12 May 2010 - 08:57 AM
Quote
Quote
If you were writing code, how would identify a class as being one you could allocate on the stack vs on the heap? In fact, I have trouble understanding this. You talk about heavy objects. Why don't you write code that demonstrates a "heavy" class? Show me code.
Quote
Quote
Quote
Quote
Quote
Quote
Quote
Quote
Literally, write code. You want to claim something about C++ that others disagree with? Show code or point to standard.
This post has been edited by Oler1s: 12 May 2010 - 08:59 AM