Introduction
C++ is a versatile programming language that allows developers to work with various data types, including those that can hold multiple values. These compound types are crucial for handling complex data structures efficiently. In this chapter, we will explore the fundamental compound types in C++ and understand their applications. Compound data types allow developers to organize and manipulate data in a structured manner, enabling the creation of complex data structures and facilitating the implementation of various algorithms and functionalities.
Compound Data Type (Composite data type)
It is a data type in programming that is composed of other simpler data types. In other words, it is a data type that can hold multiple values of different or same types within a single entity.
1 Arrays
- Arrays in C++ are contiguous collections of elements of the same data type.
Arrays in C++ provide a way to store multiple elements of the same data type under a single identifier. Here are the key points:
Declaration and Initialization:
- Arrays are declared with a specific data type and size.
- Example:
int numbers[5];
Accessing Elements:
- Elements are accessed using an index (starting from 0).
- Example:
numbers[2] = 42;
Multi-dimensional Arrays:
- Arrays can have multiple dimensions.
- Example:
int matrix[3][3];
2 Structures (struct)
- Structures allow bundling multiple variables of different types into a single entity.
- They facilitate creating complex data structures and organizing related data.
Structures allow the grouping of different data types under a single name, creating a user-defined composite data type. Key points:
Declaration and Initialization:
- Declared using the
struct
keyword. - Example:
struct Person {
char name[50];
int age;
};
Person individual;
Accessing Structure Members:
- Accessed using the (
.
) operator. - Example:
individual.age = 25;
3 Classes:
Classes in C++ provide a way to implement object-oriented programming concepts, encapsulating data and methods into a single unit. Key concepts include:
Declaration and Definition:
- Declared using the
class
keyword. - Example:
class Rectangle {
public:
int length;
int width;
int calculateArea() { return length * width; }
};
Rectangle box;
4 Pointers:
Pointers are variables that store memory addresses, allowing dynamic memory allocation and manipulation. Key concepts include:
Declaration and Initialization:
- Declared with the data type they point to.
- Example:
int *ptr;
Address-of Operator (&
):
- Obtains the memory address of a variable.
- Example:
int x = 10; int *ptr = &x;
Dereference Operator (*
):
- Accesses the value stored at a memory address.
- Example:
int y = *ptr;
Note: Pointers and references are often considered compound types because they enable indirect access to other data types, allowing for more complex and flexible data structures and programming paradigms.
5 References
- References provide an alias to an existing variable.
- They offer a convenient way to work with variables without directly manipulating memory addresses.
Example:
int x = 10;
int& ref = x;
Arrays of Structures/Classes:
Combining arrays with structures or classes allows the creation of more complex data structures. For example:
struct Point {
int x;
int y;
};
Point coordinates[10];
This creates an array of 10 points, each with x and y coordinates.