https://github.com/cypri1-dev/42_cpp07
This project introduces C++ templates with three tasks: implementing functions to swap values, return the smaller or larger of two, applying a function to each array element, and creating a dynamic array class with bounds checking, size retrieval, and safe memory management.
https://github.com/cypri1-dev/42_cpp07
Last synced: about 1 year ago
JSON representation
This project introduces C++ templates with three tasks: implementing functions to swap values, return the smaller or larger of two, applying a function to each array element, and creating a dynamic array class with bounds checking, size retrieval, and safe memory management.
- Host: GitHub
- URL: https://github.com/cypri1-dev/42_cpp07
- Owner: cypri1-dev
- Created: 2024-12-18T23:09:57.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-19T13:52:03.000Z (over 1 year ago)
- Last Synced: 2025-02-17T09:25:32.201Z (over 1 year ago)
- Language: C++
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README

## Table of Contents
- [Overview](#overview)
- [Exercises](#exercises)
- [Exercise 00: Generic Functions](#exercise-00-generic-functions)
- [Exercise 01: `iter` Function](#exercise-01-iter-function)
- [Exercise 02: Template Class `Array`](#exercise-02-template-class-array)
- [Requirements](#requirements)
- [Usage](#usage)
## Overview
This module explores C++ templates through practical exercises to enhance understanding and application. Each task focuses on creating flexible and reusable code.
## Exercises
### Exercise 00: Generic Functions
- **Objective**: Implement the following generic functions:
- `swap`: Exchanges the values of two arguments.
- `min`: Returns the smaller of two values. If equal, returns the second value.
- `max`: Returns the larger of two values. If equal, returns the second value.
- **Key Features**:
- Functions must work with any comparable types.
- Templates must be defined in header files.
### Exercise 01: `iter` Function
- **Objective**: Implement a template function `iter` that applies a given function to each element of an array.
- **Key Features**:
- Takes the address of an array, its length, and a function to apply.
- Works with any type of array.
### Exercise 02: Template Class `Array`
- **Objective**: Develop a template class `Array` to handle elements of type `T`.
- **Key Features**:
- Constructor without parameters: Creates an empty array.
- Constructor with `unsigned int n`: Creates an array of `n` elements initialized by default.
- Copy constructor and assignment operator: Ensure modifying one does not affect the other.
- Access elements via `[]` with bounds checking. Throws an exception if out of bounds.
- Member function `size()`: Returns the number of elements in the array.
## Requirements
- Templates must be fully implemented in header files.
- Memory management must use `new[]` without over-allocating.
- Code must not access unallocated memory.