blog icon indicating copy to clipboard operation
blog copied to clipboard

C++ Structured Data

Open qingquan-li opened this issue 2 years ago • 0 comments

Contents:

  1. Abstract Data Types

  2. Structures

  3. Accessing Structure Members

  4. Initializing a Structure

  5. Arrays of Structures

  6. Structures as Function Arguments

    6.1 Constant Reference Parameters

  7. Pointers to Structures

    7.1 Dynamically Allocating a Structure

1. Abstract Data Types

Concept: Abstract data types (ADTs) are data types created by the programmer. ADTs have their own range (or domain) of data and their own sets of operations that may be performed on them.

Abstraction

An abstraction is a general model of something. It is a definition that includes only the general characteristics of an object. For example, the term "dog" is an abstraction. It defines a general type of animal. The term captures the essence of what all dogs are without specifying the detailed characteristics of any particular type of dog.

Data Types

C++ has several primitive data types, or data types that are defined as a basic part of the language. For example, bool, char, double, float, int ...

Abstract Data Types

An abstract data type (ADT) is a data type created by the programmer and is composed of one or more primitive data types.

2. Structures

C++ allows you to group several variables together into a single item known as a structure.

Example:

struct Employee {
    int id;
    string name;
    double salary;
};

The tag (Employee) is the name of the structure. We begin the names of structure tags with an uppercase letter.

The variable declarations that appear inside the braces declare members of the structure.

It's important to note the structure declaration in the example does not define a variable. It simply tells the compile what a Employee structure is made of. In essence, it creates a new data type name Employee. You can define variables of this type with simple definition statements, just as you would with any other data type. For example, to defines a variable named dept_head:

Employee dept_head;

Remember that structure variables are actually made up of other variables knowns as members. Because dept_head is a Employee structure, it contains the following members:

id, an int
name, a string object
salary, a double

It's possible to define multiple structure variables in a program. For example, to define three Employee variables:

Employee dept_head, developer, salesperson;

Each of the variables defined in this example is a separate instance of the Employee structure and contains its own members. An instance of a structure is a variable that exists in memory,

3. Accessing Structure Members

Concept: The dot operator (.) allows you to access structure members in a program.

Example:

dept_head.id = 123;

cout << dept_head.id << endl;

4. Initializing a Structure

Concept: The members of a structure variable may be initialized with starting values when the structure variable is defined.

Example:

Employee dept_head = {123, "Smith", 234567.89};

5. Arrays of Structures

Example:

// Define an array, sales_team, that has 20 elements.
// Each element is a Employee structure.
Employee sales_team[10];

// Each element of the array may be accessed through a subscript.
sales_team[0].id;

Initializing a Structure Array

Example:

Employee sales_team[3] = {{1000, "Alice", 19000},
                          {1001, "Bob", 18500},
                          {1002, "Jobs", 18000}
                         };

6. Structures as Function Arguments

Example:

// void printEmployeeInfo(Employee emp) {
// Warning: The parameter 'emp' is copied for each invocation
// but only used as a const reference;
// consider making it a const reference.
void printEmployeeInfo(const Employee& emp) {
    cout << "Employee ID: " << emp.id << endl;
    cout << "Employee Name: " << emp.name << endl;
    cout << "Employee Salary: " << emp.salary << endl;
}
// Function prototype
printEmployeeInfo(const Employee&);
// Call the function
printEmployeeInfo(dept_head);

6.1 Constant Reference Parameters

Sometimes structures can be quite large. Passing large structures by value can decrease a program's performance because a copy of the structure has to be created. When a structure is passed by reference, however, it isn't copied. A reference that points to the original argument is passed instead. So, it's often preferable to pass large objects such as structures by reference.

The parameter is defined as a constant, the function cannot accidentally corrupt the value of the argument.

7. Pointers to Structures

Concept: You may take the address of a structure variable and create variables that are pointers to structures.

Example:

Employee* emp_ptr = nullptr;
emp_ptr = &dept_head;

(*emp_ptr).id = 123;
(*emp_ptr).name = "Smith";
(*emp_ptr).salary = 234567.89;

The dot operator has higher precedence than the indirection operator (*), so to dereference the empty_prt pointer, a set of parentheses must used.

C++ has a special operator for dereferencing structure pointers. It's callled the structure pointer operator, and it consists of a hyphen (-) followed by the greater-than symbol (>).

emp_ptr->id = 123;
emp_ptr)->name = "Smith";
emp_ptr)->salary = 234567.89;

7.1 Dynamically Allocating a Structure

You can use a structure pointer and the new operator to dynamically allocate a structure.

For example, the following code defines a Employee pointer named emp_ptr and dynamically allocates a Employee structure. Values are then stored in the dynamically allocated structure's members.

Employee* emp_ptr = nullptr;  // Define a Employee pointer
emp_ptr = new Employee;       // Dynamically allocate a Employee structure
emp_ptr->id = 123;            // Store a value in the id member
emp_ptr)->name = "Smith";     // Store a value in the name member
emp_ptr)->salary = 234567.89; // Store a value in the salary member

You can also dynamically allocate an array of structures. The following code shows an array of five Employee structures being allocated:

Employee* emp_ptrs = nullptr; 
emp_ptrs = new Employee[5];
for (int i = 0; i < 5; i++) {
    cout << "Please enter the name for employee "
         << (i + 1) << ": ";
    cin >> emp_ptrs[i].name;
}

qingquan-li avatar Jul 10 '22 20:07 qingquan-li