Homework #9#
Important
The file submission requirements are different than previous homeworks.
For each problem below, you need to submit a header file (.H) implementing
the class described in the problem and a source file (.cpp) containing
the main() function and any tests that the problem asks for.
Important
All work must be your own.
You may not use generative AI / large-language models for any part of this assignment.
A simple class : Create a class called
Rectanglethat describes a rectangle. This should be done in a header file. Be sure to include a header guard. Your class should:Have member data for the
widthandheightof the rectangleImplement two constructors for the class, one that takes a width and height, like:
Rectangle r(1.0, 2.0);
and one that is appropriate for a square, which just takes a single length and initializes both the width and height to that value, e.g.,
Rectangle r(1.5);
Have member functions,
perimeter(), that returns the perimeter of the rectangle andarea(), that returns the area.Have a third member function,
is_square()that returnstrueif theRectangleis a square.
Next create a source file containing the
mainand show how to create:a rectangle
a square
and for each, output their properties (perimeter,area, and the result of the test if it is square) to the terminal.
3D vectors : Starting with our
vector2d.Himplementation (Example: Mathematical Vectors), extend it to three-dimensions (call your new implementationvector3d.H).You should:
Add
zas member data.Add a
set_zfunctionExtend the addition (
+), subtraction (-) and unary minus (the other-) operators to work for a 3D vector.Update the output stream operator
<<
Don’t worry about the additional operators we looked at in class—just focus on our original implementation linked above.
Now write a
main()function that exercises each of these operators—use the driver we created for the 2D implementation as the starting point.Temperature logger : Let’s write a class called
TemperatureLogthat stores measurements of temperature at different times. Here’s how you should construct it:The member data will be a
vectorofTemperatureReading, which is defined as:struct TemperatureReading { double time; double temp; };
You can create this member data as:
std::vector<TemperatureReading> log;
The constructor will take no arguments. You would create a log just as:
TemperatureLog tlog;
and it will hold no data initially.
A member function
add_datawill be used to add a (time, temperature) pair. You can have it take each quantity as a separate argument or write it to take aTemperatureReadingobject.
Next you will add 3 member functions that act of the data:
The function
mean()will return the average temperature.The function
max()will return the maximum temperature.The function
time_of_max()will return the time when the maximum temperature was reached.
For these, you can use the ranges libraries or explicitly loop over the elements of
log.Finally, write a driver /
main()that creates aTemperatureLogand adds the following and adds the following data:time
temperature
10
75
15
68
22
79
40
85
49
96
55
88
62
78
70
71
76
62
and uses your functions to output:
the average temperature
the maximum temperature
the time of the maximum temperature