Vector

C++OOPs

Jul 7, 2024 at 21:30 pm

Abstract

A C++ library to perform basic operations on vectors such as :

  • vector addition
  • vector subtraction
  • dot product
  • cross product
  • distance between two points (2D/3D)

Background

Last sem, I took a course on Object Oriented Programming in C++ at my university. So I created this library with the objective of revising :

  • Constructors
  • operator overloading
  • template functions and classes
  • friend functions
  • C++ build system using Makefiles

How to use?

To use the library, copy the vector.h header file in your project and include it in your source files.

  1. Initialize Vector
#include<iostream>
#include "vector.h"
int main(){
  Vector<float> o; // Origin : (0,0,0)
  Vector<int> v(13,14,25);
  return 0;
}
  1. Basic operations: Vector addition, subtraction, increment and decrement
blog image
#include<iostream>
#include "vector.h"
int main(){
  Vector<float> o; // Origin : (0,0,0)
  Vector<int> v(13,14,25);
  return 0;
}
  1. Cross Product of two vectors
blog image
#include<iostream>
#include "vector.h"
 
int main(){
  Vector<int> vectors[5];
 
  for(int i=0;i<5;i++){
    Vector<int> temp(i,i,i);
    vectors[i] = temp;
  }
 
  // print all vectors
  for(auto x :vectors){
    std::cout << x << std::endl;
  }
  // OUTPUT
  /*
    (0,0,0)
    (1,1,1)
    (2,2,2)
    (3,3,3)
    (4,4,4)
   */
  return 0;
}

Advanced usage and more examples are provided in the GitHub repository.

References

  1. Why template class definition and declaration can't be separated
  2. Object Oriented Programming by Robert Lafore
  3. Caltech C++ operator overloading guide