Starting from:
$35

$29

Short Exercise #1 Creating a Custom Type in C++ Solution




In this exercise you will be creating a custom 3-dimensional vector type made of 32 bit oats. Later we expand on its capabilities with templates. You will not be provided with any skeleton code however you MUST follow the given guidelines on folder structure and le naming in order to make it easier to grade. Points will be deducted if you do not do this.




The main folder of your solution should be named se1 (note capitalization) and should follow the follwing structure :




se1




src




vector3.h




vector3.cc




CMakeLists.txt




main.cc







vector3.h and vector3.cc will be where you implement your vector class. main.cc is where you will write code to test your vector class and CMakeList.txt will be used to help build your solution. This assignment is short enough where we won't be submitting jobs to the cluster but will just be developing and debugging on the login node so there is no .pbs le.




You will be making your custom vector3 type as a struct. Note that c++ supports classes and structs and the only di erence between the two, as far as the language is concerned, is that by default the members of a struct are public while the members of a class are private by default. Otherwise they are equivalent. Since we're going to make the coordinates (x, y, z) of our vector public, we'll use a struct.




You should start your vector3.h le like this :




struct Vector3 {




float x;




float y;




float z;




//methods go here




};







You now need to implement the following methods for your custom type :




Vector3() = default;




Vector3(float xyz);




Vector3(float x, float y, float z);




Vector3 operator+(const Vector3& rhs);




Vector3 operator-(const Vector3& rhs);

Vector3 operator*(const Vector3& rhs);

Vector3 operator/(const Vector3& rhs);




Vector3 operator+(float rhs);




Vector3 operator-(float rhs);

Vector3 operator*(float rhs);

Vector3 operator/(float rhs);




float operator|(const Vector3& rhs); Vector3 operatorˆ(const Vector3& rhs);




Vector3& operator+=(const Vector3& rhs);




Vector3& operator-=(const Vector3& rhs);

Vector3& operator*=(const Vector3& rhs);

Vector3& operator/=(const Vector3& rhs);




//default constructor




//set x, y, and z to xyz




//set component by name




//component-wise add




//component-wise subtract




//component-wise multiplication




//component-wise division




//add rhs to each component //subtract rhs from each component //multiply each component by rhs //divide each component by rhs




 
dot product




 
cross product




//component-wise add




//component-wise subtract




//component-wise multiplication




//component-wise division



 
Vector3++ and ++Vector3 rotate xyz to the right




 
i.e. make x = z, y = x, z = y




 
Make sure they function correctly ++v vs v++ Vector3& operator++();






1


Friday, September 14, 2018 { Short Exercise #1
2






Vector3 operator++(int __unused);




 
Vector3-- and --Vector3 rotate xyz to the left




 
i.e. make x = y, y = z, z = x




Vector3& operator--();




Vector3 operator--(int __unused);




bool
operator==(const
Vector3&
rhs);
//component-wise
equality
bool
operator!=(const
Vector3&
rhs);
//component-wise
inequality















Your CMakeLists.txt le should contain the following :




cmake_minimum_required(VERSION 3.8)




project(se1)




set(CMAKE_CXX_STANDARD 17)




set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")







include_directories(




src




)




set(SOURCE_FILES




src/vector3.h




src/vector3.cc




)




add_executable(${PROJECT_NAME} ${SOURCE_FILES} main.cc)







Your main.cc le should contain code you use to test your implementation to verify it is working correctly. We will be using another main.cc when we test your solutions.




To build your solution, make sure you load the following modules on pace-ice




module load gcc/7.3.0




module load cmake/3.9.1







To test your implementation, create a build folder in your project directory :




se1




build




src




vector3.h




vector3.cc




CMakeLists.txt




main.cc







From within your build folder, type cmake .. . Assuming this command completes successfully, your build folder will contain a Make le. Type make to build your project. All projects must compile without any warnings or errors to receive full credit. To execute your program type ./se1 from within the build directory.




For submission, compress your solution into se1.zip (NOT .TAR.GZ, or .RAR or anything else). zip is installed on the login node. Submit via canvas.




GRAD STUDENTS ONLY : You must correctly mark the methods in Vector3 const if they do not change the internals of the struct. You should assume operators arithmetic operators work as they would with int types.

More products