There are two different situations.
(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, struct only as a complex data type definition, struct can only define member variables, can not define member functions (in pure C language, struct cannot define member functions, can only define variables). For example, the following C code fragment:
Copy Code code as follows:
struct POINT
{
int x; Legal
int y; Legal
void print ()
{
printf ("Point print\n"); Compile error
};
}9;
There is a compilation error at line 7th here, prompting the following error message: "The function cannot be a member of the point structure." So you see in the first case struct is just a data type and you can't use object-oriented programming.
Now look at the second case. First, look at the following code:
Copy Code code as follows:
#include <iostream>
using namespace Std;
Class CPoint
{
int x; Default is Private
int y; Default is Private
void print ()//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; Default is Public
int y; Default is Public
void print ()//default is public
{
cout << "Spoint: (" << x << "," << y << ")" << Endl;
}
Spoint (int x, int y)//constructor, default to Public
{
this->x = x;
This->y = y;
}
Private
void Print1 () member function of//private type
{
cout << "Spoint: (" << x << "," << y << ")" << Endl;
}
};
int main (void)
{
CPoint CPT (1, 2); Calling a constructor with CPoint parameters
Spoint SPT (3, 4); Calling a constructor with Spoint parameters
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 attributes of class, such as inheritance, virtual functions, and so on. Therefore, struct in C + + expands the struct function of C. What's different about them?
The compilation errors in the main function are all caused by accessing private members. So we can see that the default member access in class is private, and struct is public. What's the difference between struct and class on how classes are inherited? Take a look at the following program:
Copy Code code as follows:
#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 the parent class in private does not have access to the public members of the parent class. class inheritance defaults to private inheritance, and struct inheritance defaults to public inheritance. In addition, in C + + templates, class or typename can be used before type parameters, and if struct is used, the meaning is different, struct followed by "non-type template parameter", The class or TypeName is followed by the type parameter.
In fact, the struct keyword in C + + is reserved for the C + + compiler to be compatible with the programs developed by C.
Answer:
Two cases are shown below.
The struct of C 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 struct is public. Class can also be used to represent template types, and struct is not.