Table of Contents
Previous Section Next Section

struct

The struct keyword is used to create an aggregate data type called a structure. In C++, a structure can contain both function and data members. It has the same capabilities as a class except that, by default, its members are public rather than private. The general form of a C++ structure is

struct class-name : inheritance-list {
    // public members by default 
protected:
    // private members that can be inherited
private:
    // private members
} object-list;

The class-name is the type name of the structure, which is a class type. The individual members are referenced using the dot operator when acting on an object or by using the arrow operator when acting through a pointer to the object. The object-list and inheritance-list are optional.

In C, structures must contain only data members, the private, public, and protected specifiers are not allowed, and no inheritance list is allowed.

The following C-style structure contains a string called name and two integers called high and low. It also declares one variable called my_var.

struct my_struct {
  char name[80];
  int high;
  int low;
} my_var;
Note 

Chapter 1 covers structures in more detail.


Table of Contents
Previous Section Next Section