Define and use composite data types

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – User‑defined Data Types

13.1 User‑defined Data Types

Objective

Define and use composite data types.

What is a Composite Data Type?

A composite (or user‑defined) data type is built from two or more primitive types, allowing a programmer to model real‑world entities more naturally.

Common Composite Types

  • Record / Structure – a collection of named fields.
  • Array – an indexed collection of elements of the same type.
  • Tuple – a fixed‑size, ordered collection of possibly different types.
  • Class / Object – combines data (attributes) and behaviour (methods).

Defining a Record (Structure)

In many languages a record is declared with the record or struct keyword.

struct Point {

int x;

int y;

};

Usage example:

struct Point p1 = {3, 4};

printf("(%d, %d)\n", p1.x, p1.y);

Defining an Array

An array groups a fixed number of elements of the same type.

LanguageDeclaration Syntax
C / C++int scores[10];
Javaint[] scores = new int[10];
Pythonscores = [0] * 10

Defining a Tuple (Python example)

Tuples are immutable ordered collections.

person = ("Alice", 23, True)   # name, age, isStudent

Defining a Class (Object‑Oriented Composite Type)

A class encapsulates data and related operations.

class Circle {

private double radius;

public Circle(double r) { radius = r; }

public double area() { return Math.PI * radius * radius; }

}

Using Type Definitions (type alias)

Many languages allow you to create a new name for an existing type, improving readability.

LanguageAlias Syntax
Ctypedef struct Point Point2D;
C++using Point2D = struct Point;
JavaNot applicable – use class names directly.
Pythonfrom typing import NamedTuple

Operations on Composite Types

  1. Creation / Instantiation – allocate memory and initialise fields.
  2. Access – use the dot operator (.) or arrow operator (->) for pointers.
  3. Assignment – whole‑type assignment copies all fields (shallow copy).
  4. Comparison – usually requires a custom function; equality of records checks each field.
  5. Pass‑by‑value vs. Pass‑by‑reference – important for performance and side effects.

Example: Managing a Student Record

The following pseudo‑code demonstrates defining a record, creating an array of records, and processing them.

// Define a composite type

record Student {

string id;

string name;

int age;

float GPA;

}

// Declare an array of 5 students

Student class[5];

// Initialise the first student

class[0] = {"S001", "Emma", 19, 3.7};

// Function to compute average GPA

float averageGPA(Student s[], int n) {

float total = 0;

for (int i = 0; i < n; i++) {

total = total + s[i].GPA;

}

return total / n;

}

Key Points to Remember

  • Composite types let you model complex data as a single logical unit.
  • Choose the most appropriate composite type for the problem domain.
  • Be aware of memory implications – arrays allocate contiguous memory, while linked structures may be fragmented.
  • When passing large composite types to functions, prefer passing by reference to avoid costly copies.
  • Encapsulation (using classes) helps maintain data integrity by restricting direct access.

Suggested diagram: A class diagram showing a Student record with fields id, name, age, GPA and a method averageGPA() operating on an array of Student objects.