---
title: "Array class template with max, min, sort"
subject: "Object Oriented Programming"
chapter: 9
key: "semester_3/object_oriented_programming:class-template-array-max-min-sort"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/class-template-array-max-min-sort
site: BCT Engineering
---

# Array class template with max, min, sort

Object Oriented Programming · Chapter 9: Templates

- Asked in: 2068 Baisakh, 6 marks
- Video: [C++ Program - class template in c++ | templates in c++](https://www.youtube.com/watch?v=Y_SXYKgPuMg) — MicroNG
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/class-template-array-max-min-sort
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-9

## Question

Write a program with a class template to represent array and add member functions to find maximum, minimum and sort the generic array.

## Short answer

Define a class template Array with dynamic array. Member functions maximum() and minimum() iterate to find extremes; sort() uses bubble sort. Works for any type supporting > and <.

## Answer

```cpp
#include <iostream>
using namespace std;

template <class T>
class Array {
    T *a;
    int n;
public:
    Array(int size) {
        n = size;
        a = new T[n];
    }
    ~Array() { delete[] a; }

    void read() {
        cout << "Enter " << n << " elements: ";
        for (int i = 0; i < n; i++) cin >> a[i];
    }
    T maximum() {
        T m = a[0];
        for (int i = 1; i < n; i++)
            if (a[i] > m) m = a[i];
        return m;
    }
    T minimum() {
        T m = a[0];
        for (int i = 1; i < n; i++)
            if (a[i] < m) m = a[i];
        return m;
    }
    void sort() {                         // ascending bubble sort
        for (int i = 0; i < n - 1; i++)
            for (int j = 0; j < n - 1 - i; j++)
                if (a[j] > a[j + 1]) {
                    T t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;
                }
    }
    void display() {
        for (int i = 0; i < n; i++) cout << a[i] << " ";
        cout << endl;
    }
};

int main() {
    Array<int> x(5);
    x.read();
    cout << "Max = " << x.maximum() << ", Min = " << x.minimum() << endl;
    x.sort();
    cout << "Sorted: "; x.display();

    Array<float> y(4);
    y.read();
    cout << "Max = " << y.maximum() << ", Min = " << y.minimum() << endl;
    y.sort();
    cout << "Sorted: "; y.display();
    return 0;
}
```

The same class works for `int` and `float` (and any type that supports `>` and `<`).

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How does the Array class template store its elements?
   A. A fixed int array
   B. T *a allocated with new T[n] and freed with delete[] in the destructor
   C. A global array
   D. A static string
   Answer: B. The constructor allocates new T[n] and the destructor releases it. (Key point: Dynamic storage)

2. How is the maximum found?
   A. Sort and take the middle
   B. Add all elements
   C. Start with a[0] and update m whenever a[i] > m
   D. Compare only the first two
   Answer: C. Initialise to the first element and replace it whenever a larger element is found. (Key point: Maximum logic)

3. What must a type support to be used with this class template?
   A. Only the + operator
   B. A string conversion
   C. Nothing at all
   D. The > and < comparison operators
   Answer: D. maximum, minimum and sort use > and <, so the type must support them. (Key point: Required operators)

## Related in Object Oriented Programming

- [Time class: add two objects](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Atime-class-add-objects) — Object Oriented Programming, chapter 1
- [Class and function template with syntax](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-function-template-syntax) — Object Oriented Programming, chapter 9, asked 4×
- [Class template with multiple template types](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-multiple-types) — Object Oriented Programming, chapter 9
- [Class template with suitable example](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-example) — Object Oriented Programming, chapter 9
- [Default arguments in class template](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-default-args) — Object Oriented Programming, chapter 9
- [Function template: sum and average of array](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-template-sum-average) — Object Oriented Programming, chapter 9

## Related in other subjects

- [Array overflow/underflow; add two arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-overflow-underflow-add) — Computer Programming, chapter 7
- [Sort array ascending in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-sort-ascending) — Computer Programming, chapter 11, asked 2×
- [Sort array using a function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-sort-function) — Computer Programming, chapter 7
- [Correct program: case array and average](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-case-array) — Computer Programming, chapter 3
