Computer Science 102

Lab 8

In this lab you will you will implement some vector library functions as overloaded C++ operators. Some will be class methods others will be friend functions. You are not required to convert the vector components of your raytracer to C++ but you are free to do so if you wish. A sample main.cpp, vec.h and sample input and output files lab8.* are provided for you. These are not guaranteed to represent a complete test of the components.

Your mission is to write a module named vec.cpp which will contain the implementations of the following class methods and friend functions. The prototypes shown in blue should be written as vector class methods. The others should be written as C language friend functions. DO NOT MODIFY vec.h in any way. If you are confused regarding the difference in implmenting class methods versus friend functions, see the class notes and/or consult the lab TA.

class vec_t

{

public:

vec_t(); // set vec to (0, 0, 0)

vec_t(double, double, double); // set vec to values provided

vec_t operator+(vec_t &rhs); // this + rhs

vec_t operator-(vec_t &rhs); // this - rhs

double operator|(vec_t &rhs); // dot product of this and rhs

double operator!(void); // length of this vector

/* Output or input of an instance of a complete vec_t */

friend ostream & operator<<(ostream &out, const vec_t &pvec);

friend istream & operator>>(istream &in, vec_t &ivec);

/* More stuff on the next page! */

/* Product of vector and scalar */

friend vec_t operator*(double val, const vec_t &rhs);

friend vec_t operator*(const vec_t &lhs, const double val);

/* Componentwise product of two vectors */

friend vec_t operator*(const vec_t &lhs, const vec_t &rhs);

/* Vector divided by scalar */

friend vec_t operator/(const vec_t &lhs, const double val);

/* Componentwise lhs / rhs */

friend vec_t operator/(const vec_t &lhs, const vec_t &rhs);

private:

double x;

double y;

double z;

};

In this lab you will submit a single file, vec.cpp that includes the new class methods and friend functions constructed as part of this lab.

sendlab.102.labsection# lab# vec.cpp

Mike Westall

2007-01-17T09:19:32

Mike Westall