Question:
I need to make comparison between Struct and Class.?
Ashfaq
2012-11-10 00:07:21 UTC
I have been searching for the differences between struct and class but at the end what i got is that they have just difference of only their default access. By default class has private access and struct has public. but we can make it it private by using keyword private: in it body. Both show similar kind of operations and similar kind of inheritance. Then what was the need of making class in OOP yep if we can deal with struct then why did "Bjarne stroustrup" made this syntax.??
Five answers:
anonymous
2012-11-10 00:32:18 UTC
Structs are made to hold data. They are basically simple data structures.



Classes are meant to be fully functional modules.



What's the word I'm looking for..."programmically", the MAIN (and pretty much only except for the member access specifiers as you mentioned) difference is that classes have the expanded capability of having functions (called methods in OOP talk) inside them, AS WELL as data.



You typically use structs as nodes for more advanced stuff...like creating a binary tree, stack, queue, or heap.



When I was taking mainframe assembly programming my professor told me this...



My professor said that an object file was like a...god what do you call it...um...oh yea, a carburetor. What a carburetor does is actually not important, but (I am no automotive expert) it's supposed to mix fuel with air at a certain ratio so you get the highest percentage of combustion possible.



My point is a carburetor is an object, yes? It is encapsulated in the fact that it has methods, and data (like the fuel to air ratio) and it is fully functional on it's own, but it is useless until you put it in a car. The car doesn't care what the carburetor does, it just cares that it is there...the car does it's business, while the carburetor does it's business.



If you don't understand the concept of modularization, then you should probably look what paradigm means in terms of programming...there are two main versions, top-down (which break down complex problems into smaller manageable functions) and object oriented design (in which modules are designed after real world objects).



Before I go, if you look up object oriented design, make sure you also look up the 3 main characteristics of OOD, which are inheritance, polymorphism, and encapsulation.



Wikipedia has some pretty good stuff on this, look into it. Do the work.
Jim B
2012-11-10 01:35:18 UTC
Sorry Bon Jo, but most of what you say is wrong...



A struct is a data type (like an in), while a class is a data type with associated methods.



struct { int a;}

class A { int a; }



look the same.



However, with a class, you can write a constructor that initializes everything. And the class' data members are local variables to the methods (though its always safer to call them as this.data).



We actually implement C code to be object oriented.



In a file, we would have something like:



struct A { int a; }



void AConstructor( A *this );



void ASomeMethod( A *this, int someData );



which would be functionally equivalent to something like:



public class A

{

int a;

A(){}

someMethod( int someData ){}

}

}



Of course, with classes, you have hierarchy, inheritance, polymorphism, interfaces, virtuals and all of that good higher level stuff. You could keep extending structs like that (void * and static function pointers will serve you well), but at that point, it will feel like you are implementing C++ in your C code.



Each iteration of langauge is just making improvements on previous languages. The same could be said for the upgrade from struct to class, since classes automate a lot of the stuct boilerplate code.



Yes, in their basic functionality they are practically identical; classes, however let us do really cool high level stuff
elijahthecat
2012-11-10 01:48:47 UTC
A struct must provide a constructor to initialize its state and does not need the new operator to be created; a Class can defer an explicit constructor to the compiler and must always use the new operator to create an instance.



Not that your statement that both share similar inheritance properties is not correct; inheritance is only a feature of a Class; structs cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class object.



A struct can however implement interfaces,
Bon Jo
2012-11-10 00:15:27 UTC
Structures are values type. Classes are reference type.



Structure stores in memory via stack. Classes stored in memory via heap.



Structure doesn’t support inheritance. Classes support inheritance.



Constructor works in different way.



‘new’ operator works in different way.



Allocating memory for structure is very fast because this takes place inline or on the stack.
?
2016-12-26 16:08:04 UTC
C++ instructions are prefer to Java instructions in lots of procedures, yet there additionally are substantial distinction. right it is an occasion of a C++ class named int for use to symbolize a itemizing of integers; operations to function a fee to the tip of the checklist and to print the checklist are presented


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...