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.

79 Responses to “Differences between C++ Classes and Structs”

  1. Mohamed Lamine Allal Says:

    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!

    ================
    #include <iostream>
    
    using namespace std;
    
    int main(int argv, char * argc[]) {
        struct {
            private:
                bool _iamSuperPrivate = true;
                void _sayHallo() {
                    cout << "Hallo mein Bruder!" << endl;
                }
            public:
                string helloAddress = "";
                void sayHellow() {
                    cout << "Hellow!" << helloAddress != "") {
                        cout << helloAddress << _sayHallo();
                }
                bool isSuperPrivateWorking() {
                    return this->_iamSuperPrivate;
                }
        } testStruct;
    
        testStruct.helloAddress = "my Friend!";
        testStruct.sayHellow();
    
        if (testStruct.isSuperPrivateWorking()) {
            cout << "Super private is working all well!" << endl;
        } else {
            cout << "Super private not working LOL !!!" << endl;
        }
    
        return 0;
    }
    ===============================
    
  2. varsha patil Says:

    In the struct we can not* declare a function but in the class we can declare function …..

  3. vips Says:

    class= structure+function

  4. vips Says:

    class access specifier (private public protected)

  5. Neetu Says:

    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.

  6. YuriOh Says:

    @Sap: This is true for C# but not for C++
    In C++ there is no “Reference” or “Value Type”. Your text belongs to C#.

  7. Iliana Carr Says:

    what are three ways that classes can relate to each other??

  8. Sap Says:

    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.

  9. Anonymous Says:

    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.

  10. Aijaz Says:

    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;
    }

Leave a Reply