The C-language struct defines a set of variables, which the C compiler does not consider to be a new type.
A struct in C + + is a new type of definition declaration.
struct Student
{
Char name[100];
int age;
};
void Main ()
{
Student s1={"Wang", 1};
Student s2={"Wang", 2};
}
Above program we use. c file, compile error.
At this point the C compiler does not consider student to be a new type, and we must add the struct keyword in front of the student!
struct Student
{
Char name[100];
int age;
};
void Main ()
{
struct Student s1={"Wang", 1};
struct Student s2={"Wang", 2};
}
C + + provides enhanced functionality for struct keywords.
We put the same program that cannot be compiled under the C compiler into the. cpp file, and found that it can be compiled through! That is, in C + + it is assumed that a struct defines a new type, and this new type can be used to define a new variable.
#include <iostream>
using namespace Std;
struct Student
{
Char name[100];
int age;
};
void Main ()
{
Student s1={"Wang", 1};
Student s2={"Wang", 2};
System ("pause");
}
In addition, C + + not only for the struct keyword type enhancement, the struct keyword and the class keyword to complete the function is the same, of course, there are different places, the difference is again.
In the structure can also add access to data permissions: public, protected and so on.
#include <iostream>
using namespace Std;
struct Student
{
Public
Char name[100];
int age;
Private
int A;
};
void Main ()
{
struct Student s1
System ("pause");
}
Long Press to unlock
Unlock more Insider Stories
Legal programming
: Lightspeed-tech
Technology-driven Life
C + + Note 12: Extended--struct keyword type enhancement for C + +