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.hppWe 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.hppcontains a base class for geometric elements defined by a set of vertices calledElement. This is mostly complete - add access specifiers to the class definition.- Implement the
Element2DandElement3Dclasses inelement.hpp. These should both inherit fromElement. - Implement the
Triangleclass intriangle.hpp. This inherits fromElement2D. - Implement the
Tetrahedronclass intetrahedron.hpp. This inherits fromElement3D. - Test your implementation using
main.cpp. For each of the member functions ofTriangleandTetrahedron, 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- Copy your completed versions of
element.hpp,main.cpp,tetrahedron.hppandtriangle.hppfrom part 1. - Implement the new
Materialclass inmaterial.hpp. A material should be defined by its name and density, and contain a single methoddescribe()that runs a string. - Update the
Tetrahedronclass to inherit from bothElement3DandMaterial. - Update
main.cppto print the new material properties of a tetrahedron using thedescribe()method.
List the files in part3:
$ cd archer2-cpp/exercises/5-templates/part3
$ ls
Makefile element.hpp main.cpp tetrahedron.hppWe 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.
- Run
main.cpp. Make a note of the output. - Change the argument type of the
print()function inmain.cpptoElement3D. - Run
main.cppagain and compare to the previous output. Is the output what you expected? Can you explain why this happened? - Update
ElementandElement3Dto use virtual functions where necessary. Use theoverridekeyword for functions overriding virtual functions.
Lookup "pure virtual functions". Can you use these to convert
ElementandElement3Dinto abstract base classes? Which member functions should be pure virtual functions?