In your clone of this repository, find the 7-inheritance exercise. It contains three sub-directories include, part1, part2 and part3.
The include sub-directory contains two files: vector.hpp and vertex.hpp. These contain pre-made Vector and Vertex classes that you will need for the exercise - you do not need to modify these files.
You can construct a Vertex as follows:
Vertex v1 {1.2, 5.4, 2.0};
Vertex v2 {3.5, 1.0, 0.0};
You can construct a Vector from two vertices. The resulting vector points from v1 to v2:
Vector AB {v1, v2};
Given two vectors AB and AC, you can perform mathematical operations:
AB.magnitude() // |AB|
AB.dot(AC) // AB . AC
AB.cross(AC) // AB x AC
AB.cross(AC).magnitude() // |AB x AC|
List the files in part1:
$ cd archer2-cpp/exercises/7-inheritance/part1
$ ls
Makefile element.hpp main.cpp tetrahedron.hpp triangle.hpp
We are using header files as these classes require minimal implemenation details, and to reduce the number of files you need to edit. It would still be best practise to use seperate .hpp and .cpp files for definition and implementation.
As before, you can compile with make.
element.hpp contains a base class for geometric elements defined by a set of vertices called Element. This is mostly complete - add access specifiers to the class definition.Element2D and Element3D classes in element.hpp. These should both inherit from Element.Triangle class in triangle.hpp. This inherits from Element2D.Tetrahedron class in tetrahedron.hpp. This inherits from Element3D.main.cpp. For each of the member functions of Triangle and Tetrahedron, which class in the inheritance stack originally defined the function? Which class is providing the implementation?List the files in part2:
$ cd archer2-cpp/exercises/7-inheritance/part2
$ ls
Makefile element.hpp main.cpp tetrahedron.hpp triangle.hpp
element.hpp, main.cpp, tetrahedron.hpp and triangle.hpp from part 1.Material class in material.hpp. A material should be defined by its name and density, and contain a single method describe() that runs a string.Tetrahedron class to inherit from both Element3D and Material.main.cpp to print the new material properties of a tetrahedron using the describe() method.List the files in part3:
$ cd archer2-cpp/exercises/5-templates/part3
$ ls
Makefile element.hpp main.cpp tetrahedron.hpp
We have provided completed versions of Element3D, Triangle and Tetrahedron in element.hpp, triangle.hpp and tetrahedron.hpp respectively, as well as a test program in main.cpp.
main.cpp. Make a note of the output.print() function in main.cpp to Element3D.main.cpp again and compare to the previous output. Is the output what you expected? Can you explain why this happened?Element and Element3D to use virtual functions where necessary. Use the override keyword for functions overriding virtual functions.Extension
Lookup “pure virtual functions”. Can you use these to convert
ElementandElement3Dinto abstract base classes? Which member functions should be pure virtual functions?