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.

Leave a Reply