Monday, July 22, 2024

Program 10 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 10 Define a class template representing a single-dimensional array. Implement a function to sort the array elements. Include a mechanism to detect and throw an exception for array-bound violations.

 

BCA

Object Oriented Programming using C++ Practical

Madras University 

Program 10

Define a class template representing a single-dimensional array. Implement a function to sort the array elements. Include a mechanism to detect and throw an exception for array-bound violations.

SOURCE CODE:
#include <iostream>
#include <stdexcept> // For std::out_of_range
#include <algorithm> // For std::sort
using namespace std;

// Class template for a single-dimensional array
template <typename T>
class Array {
private:
    T* data;
    size_t size;

public:
    // Constructor
    Array(size_t size) : size(size) {
        data = new T[size];
    }

    // Destructor
    ~Array() {
        delete[] data;
    }

    // Copy constructor
    Array(const Array& other) : size(other.size) {
        data = new T[size];
        copy(other.data, other.data + size, data);
    }

    // Copy assignment operator
    Array& operator=(const Array& other) {
        if (this == &other) return *this; // Self-assignment check

        delete[] data; // Free the existing resource
        size = other.size;
        data = new T[size];
        copy(other.data, other.data + size, data);
        return *this;
    }

    // Move constructor
    Array(Array&& other) noexcept : data(other.data), size(other.size) {
        other.data = nullptr;
        other.size = 0;
    }

    // Move assignment operator
    Array& operator=(Array&& other) noexcept {
        if (this == &other) return *this; 

        delete[] data; // Free the existing resource
        data = other.data;
        size = other.size;
        other.data = nullptr;
        other.size = 0;
        return *this;
    }

    // Access element at index
    T& operator[](size_t index) {
        if (index >= size) {
            throw out_of_range("Index out of bounds");
        }
        return data[index];
    }

    const T& operator[](size_t index) const {
        if (index >= size) {
            throw out_of_range("Index out of bounds");
        }
        return data[index];
    }

    // Get the size of the array
    size_t getSize() const {
        return size;
    }

    // Sort the array using std::sort
    void sort() {
        std::sort(data, data + size);
    }

    // Print array elements
    void print() const {
        for (size_t i = 0; i < size; ++i) {
            cout << data[i] << ' ';
        }
        cout << endl;
    }
};


int main() {
    try {
        Array<int> arr(5);

        for (size_t i = 0; i < arr.getSize(); ++i) {
            arr[i] = 5 - i; // Fill array with values
        }

        cout << "Original array: ";
        arr.print();

        arr.sort();

        cout << "Sorted array: ";
        arr.print();

        // exception handling
        cout << "Trying to access out-of-bounds index: ";
        cout << "Trying to print 10 th index element: "<<endl;
        cout << arr[10] << endl; // This will throw an exception
    }
    catch (const out_of_range& e) {
        cerr << "Exception: " << e.what() << endl;
    }

    return 0;
}

OUTPUT:
Original array: 5 4 3 2 1 
Sorted array: 1 2 3 4 5
Trying to access out-of-bounds index: Trying to print 10 th index element:
Exception: Index out of bounds

Explanation:

  1. Class Template Definition:

    • Array(size_t size): Constructor that initializes the array with the given size.
    • ~Array(): Destructor that frees allocated memory.
    • Copy and move constructors/operators are provided to handle deep copying and move semantics.
    • operator[]: Provides access to array elements and throws an std::out_of_range exception if an invalid index is accessed.
  2. Sorting:

    • sort(): Uses std::sort from the <algorithm> header to sort the array elements.
  3. Exception Handling:

    • Out-of-bounds access is handled by throwing std::out_of_range if an invalid index is used.

No comments:

Post a Comment

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...