CHAPTER 19

image

Struct and Union

Struct

A struct in C++ is equivalent to a class, except that members of a struct default to public access, instead of private access as in classes. By convention, structs are used instead of classes to represent simple data structures that mainly contain public fields.

struct Point{  int x, y; // public};class Point{  int x, y; // private};

Declarator list

To declare objects of a struct the normal declaration syntax can be used.

Point p, q; // object declarations

Another alternative syntax commonly used with structs is to declare the objects when the struct is defined by placing the object names before the final semicolon. ...

Get C++ Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.