6.1 Introduction to Structures in C++


Objectives:

At the end of this lesson you will be able to:

Describe Structures

Structures form a very large building block with which to collect one or more variables, possibly of different types, grouped together into one collective unit using a single name for easy handling. They are a versatile data structure in which to clump data together in convenient little packages. Variables can be of any type: int, float, char or other struct etc.

Structure is commonly used to define records to be stored in files.

Structure, combined with pointers, can create linked lists, stacks, queues, and trees.

The main difference between structure and array is that arrays are collections of the same data type and structure is a collection of variables under a single name.


6.1 Introduction to Structures in C++


Contrast between arrays and structures


Structures and classes are syntactically identical, except in the default accessibility of their members and their inheritance. By default, members of structures have public accessibility and public inheritance from their parent(s), while members of classes are private and inherit privately from their parent(s). Individual members' accessibility can be specified by using the public, protected and private keywords.


6.1 Introduction to Structures in C++


Following the conventions of object-oriented programming, class is the more commonly used term for referring to these object data types in C++. The semantics of classes, by making members private by default, encourages encapsulation.

They are essentially classes with all members defined as public access with no private or protected access modes available. They are most commonly used for conglomerating data and also support member functions, but that attribute is rarely used. The reason is because when multiple functions are involved, the need for private and protected variables and functions increases.