Note: The following content may not be reproduced without the consent of the blogger.
Analytical:
Here are the differences between the two cases.
(1) The difference between C's struct and C + + class.
(2) The difference between struct and class in C + +.
In the first case, there is a very clear distinction between struct and class. C is a procedural language, a struct is simply defined as a complex data type, only member variables can be defined in a struct, and member functions cannot be defined. For example, the following C code fragment:
1 struct Point2 {3 intX//Legal4 intY//Legal5 voidprint ()6 {7printf"Point print\n");//Compile Error8 };9 };Ten
The 7th line here appears with a compilation error, prompting the following error message: "The function cannot be a member of the point struct." So you see in the first case that a struct is just a data type and cannot be programmed using object-oriented programming. Now look at the second case. First, take a look at the following code:
#include <iostream>using namespacestd;classcpoint{intX//default is Private intY//default is Private voidPrint ()//default is Private{cout<<"CPoint: ("<< x <<", "<< y <<")"<<Endl; } Public: CPoint (intXintY//constructor, specified as public { This->x =x; This->y =y; } voidPrint1 ()// Public{cout<<"CPoint: ("<< x <<", "<< y <<")"<<Endl; }};structspoint{intX//Default to public intY//Default to public voidPrint ()//Default to public{cout<<"Spoint: ("<< x <<", "<< y <<")"<<Endl; } spoint (intXintY//constructors, default to public { This->x =x; This->y =y; } Private: voidPrint1 ()//member functions of private type{cout<<"Spoint: ("<< x <<", "<< y <<")"<<Endl; }};intMainvoid) {CPoint cpt (1,2);//call the constructor with the CPoint parameterSpoint SPT (3,4);//call the constructor with the Spoint parametercout<< Cpt.x <<" "<< Cpt.y << Endl;//Compile ErrorCpt.print ();//Compile ErrorCpt.print1 ();//LegalSpt.print (); //LegalSpt.print1 ();//Compile Errorcout << spt.x <<" "<< Spt.y << Endl;//Legal return 0;}
In the above program, struct also has constructors and member functions, in fact, it also has other characteristics of class, such as inheritance, virtual functions and so on. As a result, structs in C + + augment the struct function of a. What difference do they have?
Compilation errors within the main function are all generated by accessing private members. So we can see that the default member access rights in class are private, and the struct is public. What is the difference between a struct and a class in the way classes are inherited? Take a look at the following program:
1#include <iostream>2 using namespacestd;3 classCBase4 {5 Public:6 voidPrint ()//Public member functions7 {8cout <<"cbase:print () ..."<<Endl;9 }Ten }; One classCderived1:cbase//Default Private Inheritance A { - }; - the classCDERIVED2: PublicCbase//Specify public inheritance - { - }; - + structSderived1:cbase//Default public inheritance - { + }; A at structSDERIVED2:PrivateCbase//Specify public inheritance - { - }; - - intMain () - { in CDerived1 CD1; - CDerived2 CD2; to SDerived1 SD1; + SDerived2 SD2; - theCd1.print ();//Compile Error * cd2.print (); $ sd1.print ();Panax NotoginsengSd2.print ();//Compile Error - the return 0; +}
As you can see, the child class object that inherits the parent class as private cannot access the public members of the parent class. class inheritance defaults to private inheritance, while struct inheritance defaults to public inheritance.
In addition, in C + + templates, the type parameter can be preceded by class or TypeName, and if a struct is used, the meaning is different, and the struct is followed by "Non-type template parameter". The type parameter is followed by class or TypeName.
In fact, the key to preserving structs in C + + is to enable C + + compilers to be compatible with C-developed programs.
Answer:
Two cases are shown below.
C's struct differs from the C + + class: struct is only defined as a complex data type and cannot be used for object-oriented programming.
The difference between struct and class in C + +: For member access and inheritance, the default in class is private, and the struct is public. Class can also be used to represent template types, but not for structs.
The difference between struct and class (in C and C + +, respectively)