» Quick Introduction to C++ » 1. Basics » 1.3 Compound Types

Compound Types

Compound types are types that are derived from other types. They include arrays, pointers, references, and user-defined types such as structures and classes.

Arrays

An array is a collection of items stored at contiguous memory locations. Elements in an array can be accessed randomly using indexes.

#include <iostream>

int main() {
    // Declaration and initialization of an integer array
    int numbers[5] = {1, 2, 3, 4, 5};

    // Accessing elements of the array
    std::cout << "Third element: " << numbers[2] << std::endl;

    return 0;
}

Pointers

A pointer is a variable that stores the memory address of another variable. It allows you to indirectly access the value of the variable it points to.

#include <iostream>

int main() {
    // Declaration and initialization of a pointer
    int value = 42;
    int* ptr = &value;

    // Dereferencing the pointer
    std::cout << "Value pointed to by ptr: " << *ptr << std::endl;

    return 0;
}

References

A reference is an alias for an existing variable. It provides an alternative name for the same memory location, making it a convenient way to work with variables without involving pointers.

#include <iostream>

int main() {
    // Declaration and initialization of a reference
    int value = 42;
    int& ref = value;

    // Using the reference
    std::cout << "Value through ref: " << ref << std::endl;

    return 0;
}

Enumerations

An enum (enumeration) is a user-defined type consisting of a set of named integral constants. It provides a way to create symbolic names for values, making the code more readable and less error-prone.

#include <iostream>

enum Color {
    RED,
    GREEN,
    BLUE
};

int main() {
    Color selectedColor = GREEN;

    if (selectedColor == RED) {
        std::cout << "Selected color is Red." << std::endl;
    } else if (selectedColor == GREEN) {
        std::cout << "Selected color is Green." << std::endl;
    } else if (selectedColor == BLUE) {
        std::cout << "Selected color is Blue." << std::endl;
    }

    return 0;
}

Structures

Structs (structures) allow you to group variables of different types under a single name. They are user defined data storage objects containing public members by default.

#include <iostream>

struct Person {
    std::string name;
    int age;
};

int main() {
    // Creating an instance of the structure
    Person person1 = {"John", 25};

    // Accessing structure members
    std::cout << "Name: " << person1.name << ", Age: " << person1.age << std::endl;

    return 0;
}

Class

A class is a user-defined data type that serves as a blueprint for creating objects. Objects are instances of a class, and they encapsulate data (attributes) and functions (methods) that operate on that data. Classes provide a way to model and organize code in an object-oriented manner, promoting encapsulation, abstraction, and code reuse.

#include <iostream>

// Declaration of a class
class Rectangle {
public:
    int length;
    int width;

    // Member function to calculate area
    int calculateArea() {
        return length * width;
    }
};

int main() {
    // Creating an instance of the class
    Rectangle rectangle1;
    rectangle1.length = 5;
    rectangle1.width = 3;

    // Using a member function
    std::cout << "Area of the rectangle: " << rectangle1.calculateArea() << std::endl;

    return 0;
}

Union

A union is a compound type that allows you to store different types of data in the same memory location. All members of a union share the same memory space, and the size of the union is determined by the size of its largest member.

#include <iostream>

union MyUnion {
    int intValue;
    float floatValue;
    char charValue;
};

int main() {
    MyUnion myUnion;
    myUnion.intValue = 42;

    std::cout << "Value as int: " << myUnion.intValue << std::endl;
    std::cout << "Value as float: " << myUnion.floatValue << std::endl;
    std::cout << "Value as char: " << myUnion.charValue << std::endl;

    return 0;
}

Code Challenge

Write a C++ program that uses a union to determine the size of various data types (int, float, double, char) without using the sizeof operator.

Loading...
> code result goes here
Prev
Next