struct structure_name
{
// member declarations.
}
struct Student
{
char name[20];
int id;
int age;
}
s.id = 4;
#include <iostream> using namespace std; struct Rectangle { int width, height; }; int main(void) { struct Rectangle rec; rec.width=8; rec.height=5; cout<<"Area of Rectangle is: "<<(rec.width * rec.height)<<endl; return 0; }
Area of Rectangle is: 40
#include <iostream> using namespace std; struct Rectangle { int width, height; Rectangle(int w, int h) { width = w; height = h; } void areaOfRectangle() { cout<<"Area of Rectangle is: "<<(width*height); } }; int main(void) { struct Rectangle rec=Rectangle(4,6); rec.areaOfRectangle(); return 0; }
Area of Rectangle is: 24
| Structs | Class |
| 如果未明确声明访问说明符,则默认情况下,访问说明符将是公共的。 | 如果未明确声明访问说明符,则默认情况下,访问说明符将为私有。 |
| Structs的语法: struct structure_name { //Structs体。 } |
Class的语法: 类class_name { //类的主体。 } |
| 该Structs的实例称为"Structs变量"。 | 该类的实例称为"该类的对象"。 |