Differences between C++ Classes and Structs
Probably the most frequently asked interview question that I have received is one that explores the difference between C++ classes and structs. Such a question was asked by a Northrop Grumman recruiter during a career fair at UC Irvine. I was also asked this sort of question during an on-site interview at Heavy Iron Studios. Recently, it has been asked during a phone interview with Amazon.com.
My answer typically states that members of a class are private by default, whereas members of a struct are public by default. Inheritance between classes is also private by default, and inheritance between structs is public by default. The interviewer was usually satisfied with this answer, which Dr. Raymond Klefstad fed to my first computer science class. Lately, I was interested in the nontrivial cases that bring me uncertainty: a struct inheriting from a class and a class inheriting from a struct.
Code to test the behavior for these cases is presented below:
class A
{
public:
int a;
};
struct B : A { };
struct C
{
int c;
}
class D : C { };
int main()
{
B b;
D d;
b.a = 1;
d.c = 2;
}
Although a recent version of the GNU project C++ compiler treats the assignment of 2 into d.c in the above example as a compile-time error, a programmer who is more interested in standards compliance should refer to the C++ standard. After all, compilers do not determine standard behavior; standards prescribe standard behavior for compilers. The GNU project C++ compiler is consistent with 11.2.2 of ISO/IEC 14882-2003, which states that the kind of inheritance is determined by the derived class being declared as a class or struct when an access specificer for the base class is absent. The standard also clarifies the second part of the answer to the above interview question.
April 6th, 2007 at 6:16 am
Hi, in terms of language this satisfy my well, however in terms of memory bytes, is there or not a difference between a struct and a class?
what if I use a write() call passing a pointer to a struct, or if I pass a pointer to a class?
What if my class has virtual inheritance?
(Suppose I want to send my class over the network, and it contains no dynamic pointer ie: class a { int a,b,c; long d; char e[25]}; )
April 9th, 2007 at 1:07 am
There isn’t a difference in memory used between C++ structs and classes. Using write() on a C++ struct has the same effect as using write() on a C++ class. Structs can use virtual inheritance as well.
I really do not recommend sending objects with, I assume, write(). Serializing objects into components that allow the desired objects and their relationships to other objects to be reconstructed at the other side of the network connection is a preferred method.
June 6th, 2007 at 4:57 pm
Interesting, but if I may ask, how exactly does C++ struct inheritance work? I mean, if we were to use some generic code, like
struct A {
int x;
}
struct B : A {
int y;
}
How would the corresponding code appear in C? How does this C++ version look in memory compared to how the C version appears in memory?
June 7th, 2007 at 1:20 am
class D : C { };
this code has the class inheriting from a struct, in private access, since the default access specifier is private for classes. This code compiles well for unix CC complier, also no probs with vc++.
June 21st, 2007 at 11:51 pm
Structures Are Diffrent from Class in various ways :-
1. Structures didnot support data hiding, but classes can.
2. Priovate functions totally hidden in classes.
3. There is also differnce in memory allocation.
December 11th, 2007 at 10:39 am
Mahesh, that is wrong.
All 3 of your points are wrong when considering structs in C++.
It sounds like you are comparing C structs with C++ classes.
But in C++ struct’s and classes both support private member data, private member functions, and have no difference in memory allocation.
Try it if you don’t believe it. Or just look it up in the spec.
March 13th, 2008 at 10:16 am
IN C++ THE TECHNICAL DIFFERENCE BETWEEN CLASSES AND STRUCT IS THAT WITHOUT THE USE OF RESERVED WORD PUBLIC AND PRIVATE, MEMBER FUNCTION ARE PRIVATE BY CLASS AND PUBLIC BY STRUCT
April 2nd, 2008 at 6:16 am
in C++ between a struct and object (class) of same size and with same members, which is faster to run?
June 1st, 2008 at 11:54 am
what are uses of class in c++.because in structure do all work like class.so what is need of class.please tell the resion
August 14th, 2008 at 2:51 pm
Rupert: what do you mean by “which is faster to run?” Instances of classes and structs are all “objects.” Furthermore, as has been stated multiple times in this post, the only difference is the default visibility of members.
class C {
public:
int pub;
private:
int priv;
};
is identical in every respect (except the default behavior when they are inherited) to
struct S {
public:
int pub;
private:
int priv;
};
If you have a pointer ’s’ to an object of type S and a pointer ‘c’ to an object of type C then “s->pub = 0;” and “c->pub = 0;” then not only is the behavior identical, but the generated assembly is identical.
August 24th, 2008 at 10:43 am
Sir, Please send me technical differences between classes and structures
using c++.net
September 10th, 2008 at 9:33 pm
struct A
{
int x;
};
struct C : A {};
class D : public B{};
class B
{
public :
int y;
};
int main()
{
A a;
B b;
a.x = 10;
b.y = 20;
C c;
D d;
c.x = 40;
d.y =50;
}
What’s wrong in this code?
I am getting the following error.
#g++ struct.cpp
struct.cpp:7: error: expected class-name before ‘{’ token
struct.cpp: In function `int main()’:
struct.cpp:24: error: ‘class D’ has no member named ‘y’
September 10th, 2008 at 9:35 pm
Sorry. I found it. class D declaring has to come after class B.
November 7th, 2008 at 9:28 am
i didnt satisfied wiith the answer given…
it should be more technical.there isnt any difference on case of memory..
November 13th, 2008 at 11:46 am
what is the difference between a C++ structure and a class?
What comes to my mind are
1) structures in c++ doesnot provide datahiding where as a class provides datahiding
2)classes support polymorphism, whereas structures donot