C + + retains the struct of the language and expands it. In the C language, a struct can contain only data members and cannot contain member functions. In C + +, a struct is similar to class, and can contain both a data member and a member function.
In C + +, the C language of the struct keyword is not discarded, its significance lies in the C language Program developers have a sense of belonging, and can make the C + + compiler compatible with the previous development of the C language project.
Examples of C + + structs:
#include <iostream>using namespacestd;structstudent{Char*name; floatscore; Student (Char*,float); voidsay ();}; Student::student (Char*name,floatscore) { This->name =name; This->score =score;}voidStudent::say () {cout<<name<<"the results are"<<score<<Endl;}intMain () {Student stu ("Xiao Ming",95.5f); Stu.say (); return 0;}
The program compiles and runs normally, stating that the struct's default member is the public property, otherwise the member function cannot be accessed through the object. If you replace the struct keyword with a class, an error is compiled.
C + + Learning the difference between class 13 classes and struct structs