Definition Method and Initialization of Structural Type Variables
First declare that the structure type defines the variable name
#include<iostream>
using namespace std;
int main(){
struct Student{ //Declare a structure type Student, which is a type, just like an int type.
int num; //Declare an integer num
char name[20]; //Declare a character array name
char sex; //Declare a character variable sex
int age; //Declare a deformation variable age
float score; //Declare a single-precision variable
char addr[30]; //Declare a character array addr
};
Student student1,student2;// Define structure type variables student1 and student2. Memory is allocated
cout<<sizeof(Student)<<endl;//sizeof(Student) is like sizeof(int).
cout<<sizeof(student1)<<endl;
cout<<sizeof(student2)<<endl;
return 0;
}
Output results
After defining structure variables, the system allocates memory units for them. (You can use the sizeof function to see the number of bytes allocated. Different Compiler Systems differ.)
Define variables while declaring types
#include<iostream>
using namespace std;
int main(){
struct Student{ //Declare a structure type Student
int num; //Declare an integer num
char name[20]; //Declare a character array name
char sex; //Declare a character variable sex
int age; //Declare a deformation variable age
float score; //Declare a single-precision variable
char addr[30]; //Declare a character array addr
}student1,student2;//Declare variables student1 and student2
cout<<sizeof(Student)<<endl;
cout<<sizeof(student1)<<endl;
cout<<sizeof(student2)<<endl;
return 0;
}
Direct Definition of Structural Type Variables
#include<iostream>
using namespace std;
int main(){
struct { //Declare a struct type Student with no type name
int num; //Declare an integer num
char name[20]; //Declare a character array name
char sex; //Declare a character variable sex
int age; //Declare a deformation variable age
float score; //Declare a single-precision variable
char addr[30]; //Declare a character array addr
}student1,student2;//Declare variables student1 and student2
cout<<sizeof(student1)<<endl;
cout<<sizeof(student2)<<endl;
return 0;
}
Initialization of Structural Variables
For structure
struct a {
int b;
int c;
}
There are several ways to initialize:
struct a a1 = {
.b = 1,
.c = 2
};
perhaps
struct a a1 = {
b:1,
c:2
}
perhaps
struct a a1 = { 1, 2};
Differences between Structures and Classes in C++ (Differences between Structures and Classes)
Reproduction sources: http://blog.sina.com.cn/s/blog_48f587a80100k630.html
The struct in C++ extends the struct in C+. It is no longer just a data structure with different data types. It has acquired too many functions.
Can struct contain member functions? Yes!
Can struct inherit? Yes!!
Can struct achieve polymorphism? Yes!!!
Since all of these can be implemented, what difference can it make from class?
The most essential difference is the default access control: the default inheritance access, struct is public, class is private.
You can write the following code:
struct A
{
char a;
};
struct B : A
{
char b;
};
At this point B is inherited from A by public.
If the above struct is changed to class, then B inherits A from private. This is the default inheritance access.
So when we usually write class inheritance, we usually write as follows:
class B : public A
This is to specify that it is public inheritance, not private inheritance by default.
Of course, whether public inheritance or private inheritance is the default depends on subclasses rather than base classes.
I mean, struct can inherit classes, and classes can inherit struct, so the default inheritance access is to see whether the subclasses are struct or class. As follows:
struct A{};class B : A{}; //private Inheritance
struct C : B{}; //public inheritance
struct is the implementation of data structure. Its default data access control is public, while class is the implementation of object. Its default member variable access control is private.
I still emphasize that struct is an implementation of a data structure, although it can be used as a class. I still call variables in struct data and variables in class members, although they are not different.
Whether you use struct or class depends entirely on your personal preferences. You can replace all classes in the program with struct. It still works well. But the best advice I can give is: when you think you're going to do something more like a data structure, use struct, and if you're going to do something more like an object, use class.