Vector Implementation Basics
- CMPT 225
- Fall 2021
- Lecture 4
Example Using a Simple “Vector” Class
Take a look at the simple_vector.cpp file.
Name | Type | Value |
---|---|---|
y | float | (empty) |
x | int | 5 |
… | … | … |
p | int * | address |
g | int * | p+1 |
What is *g?
Implementation in IVector.h
See IVector.h
Templates
- Often, we have algorithms that will work on many data types, with few or no changes.
- In strongly typed languages, we need a way to produce “generic” code–code that can work on different types in different places.
- In C++, templates let us write generic code.
- A template function or class defineition has a placeholder for one or more data types that is instanciated at compile time.
- The instanciation may be different at different places in the same code.
Test Program
See test_program.cpp
TVector is a templated version of IVector
See TVector.h
See Vector.h
See TestVector.cpp