The difference between the keyword Struct and Class in C++

  • 2020-04-02 02:46:50
  • OfStack

The difference between a Struct and a Class

Today's post will focus on the difference between the keyword struct and class in C++. This blog will systematically explain the different aspects of these two keywords in detail.

Syntactically, there are only two differences between class and struct when it comes to type definition:

1. Default inheritance authority. If not specified, inheritance from class is handled according to private inheritance, while inheritance from struct is handled according to public inheritance;

2. Default access for members. The members of class default to private permissions, and struct defaults to public permissions. The above two points are also the most basic difference between struct and class, and also the most essential difference;

But in C++, struct is extended, and now it's more than just a data structure with different data types, it includes more functionality.

Can structs contain member functions?

Yes, the answer is yes. Now let me write a piece of code to verify it:


/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to //www.jb51.net
*/
 
#include <iostream>
using namespace std;
 
struct Test
{
    int a;
    int getA()
    {
        return a;
    }
 
    void setA(int temp)
    {
        a = temp;
    }
};
 
int main(int argc, char* argv[])
{
    Test testStruct;
    testStruct.setA(10);
    cout<<"Get the value from struct:"<<testStruct.getA()<<endl;
 
    Test *testStructPointer = new Test;
    testStructPointer->setA(20);
    cout<<"Get the value from struct again:"<<testStructPointer->getA()<<endl;
    delete testStructPointer;
 
    return 0;
}

The above code will work correctly, yes; Yes, structs can contain member functions.

Does a Struct have its own constructor?

Yes, you can. See the following test code:


/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to //www.jb51.net
*/
 
#include <iostream>
using namespace std;
 
struct Test
{
    int a;
 
    Test()
    {
        a = 100;
    }
 
    int getA()
    {
        return a;
    }
 
    void setA(int temp)
    {
        a = temp;
    }
};
 
int main(int argc, char* argv[])
{
    Test testStruct;
    testStruct.setA(10);
    cout<<"Get the value from struct:"<<testStruct.getA()<<endl;    
        Test *testStructPointer = new Test;
    testStructPointer->setA(20);
    cout<<"Get the value from struct again:"<<testStruct.getA()<<endl;
    delete testStructPointer;
 
    // test the constructor
    Test testConstructor;
    cout<<"Set the value by the construct and get it:"<<testConstructor.getA()<<endl;
 
    return 0;
}

Can a Struct have a destructor?

Let me verify this:


/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to //www.jb51.net
*/
 
#include <iostream>
using namespace std;
 
struct Test
{
    int a;
 
    Test()
    {
        a = 100;
    }
 
    int getA()
    {
        return a;
    }
 
    void setA(int temp)
    {
        a = temp;
    }
 
    ~Test()
    {
        cout<<"Destructor function called."<<endl;
    }
};
 
int main(int argc, char* argv[])
{
    Test testStruct;
    testStruct.setA(10);
    cout<<"Get the value from struct:"<<testStruct.getA()<<endl;    
        Test *testStructPointer = new Test;    
        testStructPointer->setA(20);
    cout<<"Get the value from struct again:"<<testStruct.getA()<<endl;
    delete testStructPointer;
 
    // test the constructor
    Test testConstructor;
    cout<<"Set the value by the construct and get it:"<<testConstructor.getA()<<endl;
 
    return 0;
}

Yes, destructors are fully supported.

Does Struct support inheritance?

Let me write code to verify it:


/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to //www.jb51.net
*/
 
#include <iostream>
using namespace std;
 
struct A
{
    int a;
    A()
    {
        a = 10;
    }
    void print()
    {
        cout<<"I am from A"<<endl;
    }
};
 
struct B : A
{
    int b;
    B()
    {
        a = 30; // set a to 30
        b = 20;
    }
    /*void print()
    {
    cout<<"I am from B"<<endl;
    }*/
};
 
int main(int argc, char* argv[])
{
    B b1;
    cout<<b1.a<<endl;
    cout<<b1.b<<endl;
    b1.print();
 
    A a1;
    cout<<a1.a<<endl;
    a1.print();
 
    return 0;
}

Running the above code, struct supports inheritance.

Does Struct support polymorphism?

Write code to test it:


/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to //www.jb51.net
*/
 
#include <iostream>
using namespace std;
 
struct A
{
    virtual void print() = 0;
};
 
struct B : A
{
    void print()
    {
        cout<<"I am from B"<<endl;
    }
};
 
struct C : A
{
    void print()
    {
        cout<<"I am from C"<<endl;
    }
};
 
int main(int argc, char* argv[])
{    
    A *a1;    
    B *b1 = new B;    
    C *c1 = new C;    
    a1 = b1;    
    a1->print(); // call B, not A
 
    a1 = c1;
    a1->print(); // call C, not A
 
    return 0;
}

Does Struct support Private, Protected, and Public keywords?


/*
** FileName     : StructAndClassDiffDemo
** Author       : Jelly Young
** Date         : 2013/12/7
** Description  : More information, please go to //www.jb51.net
*/
 
#include <iostream>
using namespace std;
 
struct A
{
private:
    int b;
 
protected:
    int c;
 
public:
    A()
    {
        b = 10;
        c = 20;
        d = 30;
    }
 
    int d;
};
 
struct B : A
{
    void printA_C()
    {
        cout<<A::c<<endl;
    };
 
    // private member can not see
    /*void printA_B()
    {
    cout<<A::b<<endl;
    }*/
 
    void printA_D()
    {
        cout<<A::d<<endl;
    }
};
 
int main(int argc, char* argv[])
{
 
    A a1;
    B b1;
 
    // private member can not see
    //cout<<a1.b<<endl;
 
    // protected member can not see
    //cout<<a1.c<<endl;
 
    // public member can see
    cout<<a1.d<<endl;
 
    return 0;
}

So if I write this much, then there's a situation where if the parent of the class is described by the struct keyword, what is the default access property?

When this happens, it is up to the subclass, not the base class, to decide whether to default to public or private. A class can inherit from a class decorated with struct; At the same time, struct can also inherit from the class modified class. The inheritance property is described as follows:


class B:A{}; //Private inherits
 
class A{};
struct B:A{}; //Public inherits

Finally, is it better to use structs or classes? It depends on personal preference, but there's a programming specification problem here, when you feel like what you're doing is more like a data structure, then use struct, if you feel like what you're doing is more like an object, then use class.


Related articles: