A brief analysis of the difference between c and c++ struct

  • 2020-04-02 01:11:34
  • OfStack

There are two differences here.
(1) the difference between C struct and C++ class.
(2) difference between struct and class in C++.
In the first case, a struct is very different from a class. C is a procedural language, struct is only defined as a complex data type, and only member variables can be defined in struct, not member functions (in pure C language, struct cannot define member functions, but only variables). For example, the following C code snippet:


        struct Point
        {
                int x; //legal
                int y; //legal
                void print()
                {
                        printf("Point printn"); //Compile error
                };
}9        ;

There is a compilation error in line 7 with the following error message: "the function cannot be a member of the Point structure." So you can see that in the first case a struct is just a data type, you can't use object-oriented programming.

Now let's look at the second case. First look at the following code:


       #include <iostream>
        using namespace std;
        class CPoint
        {
                int x;                  //The default is private
                int y;                  //The default is private
                void print()             //The default is private
                {
                        cout << "CPoint: (" << x << ", " << y << ")" << endl;
                }
        public:
                CPoint(int x, int y)      //Constructor, specified as public
                {
                        this->x = x;
                        this->y = y;
                }
                void print1() //public
                {
                        cout << "CPoint: (" << x << ", " << y << ")" << endl;
                }
        };

        struct SPoint
        {
                int x;              //The default for the public
                int y;              //The default for the public
                void print()         //The default for the public
                {
                        cout << "SPoint: (" << x << ", " << y << ")" << endl;
                }
                SPoint(int x, int y)  // The constructor ,The default for the public
                {
                        this->x = x;
                        this->y = y;
                }
        private:
                void print1()      //A member function of type private
                {
                        cout << "SPoint: (" << x << ", " << y << ")" << endl;
                }
        };

        int main(void)
        {
                CPoint cpt(1, 2);  //Call the CPoint constructor with arguments
                SPoint spt(3, 4);  //Call the SPoint constructor with arguments

                cout << cpt.x << " " << cpt.y << endl;  //Compile error
                cpt.print();       //Compile error
                cpt.print1();      //legal

                spt.print();      //legal
                spt.print1();     //Compile error
                cout << spt.x << " " << spt.y << endl;  //legal

                return 0;
        }

In the above program, struct also has constructors and member functions, but it also has other features of class, such as inheritance, virtual functions, and so on. Therefore, structs in C++ extend the struct function of C. So what's the difference?

Compilation errors in the main function are all due to accessing the private member. So we can see that the default member access in class is private, while in struct it is public. What's the difference between a struct and a class in the way a class inherits? See the following program:


       #include <iostream>
        using namespace std;
        class CBase
        {
        public:
                void print()                //Public member function
                {
                        cout << "CBase: print()..." << endl;
                }
        };
        class CDerived1 : CBase        //Default private inheritance
        {
        };

        class CDerived2 : public Cbase   //Specify public inheritance
        {
        };

        struct SDerived1 : Cbase        //Default public inheritance
        {
        };

        struct SDerived2 : private Cbase  //Specify public inheritance
        {
        };

        int main()
        {
                CDerived1 cd1;
                CDerived2 cd2;
                SDerived1 sd1;
                SDerived2 sd2;

                cd1.print();    //Compile error
                cd2.print();
                sd1.print();
                sd2.print();    //Compile error

                return 0;
        }

As you can see, a subclass object that inherits its parent class privately cannot access the public member of its parent class. Class inheritance defaults to private inheritance, while struct inheritance defaults to public inheritance. In addition, in C++ templates, type parameters can be preceded by class or typename, but struct has a different meaning. Struct is followed by "non-type template parameter", while class or typename is followed by type parameters.
In fact, the key to preserving structs in C++ is to make the C++ compiler compatible with programs developed in C.

The answer:
Here are two cases.
C structs differ from C++ classes: structs are only defined as complex data types and cannot be used in object-oriented programming.
Differences between structs and classes in C++ : for member access and inheritance, the default in class is private, while structs are public. Classes can also be used to represent template types, not structs.


Related articles: