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.
June 5th, 2021 at 2:24 pm
Run this and it will works all cool!
Struct support data visiblity and (hiding) (encapsulation) just like class! Also the same for methods!
It was clearly stated by many comments! the difference is in the the visibility defaulting only one default to private the other to public!
Run the code bellow! Tested with gcc!
March 9th, 2015 at 7:54 am
In the struct we can not* declare a function but in the class we can declare function …..
June 5th, 2013 at 11:01 am
class= structure+function
June 5th, 2013 at 10:59 am
class access specifier (private public protected)
February 19th, 2013 at 7:00 am
I think there is only the difference is the member of a class are private by default and member of a structure are public by default. Also in a structure struct keyword is used and in a class the keyword class is used.
January 5th, 2013 at 11:09 am
@Sap: This is true for C# but not for C++
In C++ there is no “Reference” or “Value Type”. Your text belongs to C#.
September 9th, 2012 at 10:15 am
what are three ways that classes can relate to each other??
August 18th, 2012 at 11:48 am
Differences between classes and structs:-
Although classes and structures are similar in both the way they are declared and how they are used, there are some significant differences. Classes are reference types and structs value types. A structure is allocated on the stack when it is declared and the variable is bound to its address. It directly contains the value. Classes are different because the memory is allocated as objects on the heap. Variables are rather managed pointers on the stack which point to the objects. They are references.
Structures require some more than classes. For example, you need to explicitly create a default constructor which takes no arguments to initialize the struct and its members. The compiler will create a default one for classes. All fields and properties of a struct must have been initialized before an instance is created. Structs do not have finalizers and cannot inherit from another class like classes do. However, they inherit from System.ValueType, that inherits from System.Object. Structs are more suitable for smaller constructs of data.
May 11th, 2012 at 6:08 am
class A
{
public:
int a;
};
struct B : A { };
struct C
{
int c;
}; —————————-> Note semicolon here.
class D : public C { }; —–> Note public here.
int main()
{
B b;
D d;
b.a = 1;
d.c = 2;
}
The above one perfectly works fine for me on gcc – linux.
May 11th, 2012 at 6:06 am
class A
{
public:
int a;
};
struct B : A { };
struct C
{
int c;
} ————-> U forgot a semicolon here,
struct or class inheritance is not a problem.
class D : C { };
int main()
{
B b;
D d;
b.a = 1;
d.c = 2;
}