Starting from:

$30

Lab 16 Solution

Objectives:







Implement a templated function



Implement a class that uses a templated variable






Question 1:




The product of this question will be the kernel of the second, before starting read through the entire assignment. You will begin by defining two classes, Rectangle and Triangle. Their declarations are given below.




class Rectangle {




public:




Rectangle(float length = 0, float width = 0); void set(float length, float width);




void get(float &length, float &width); string getType(); // Returns “RECTANGLE” float area();




private:




float len, wid;




};




class Triangle {




public:




Triangle(float base = 0, float height = 0); void set(float base, float height);




void get(float &base, float &height); string getType(); // Returns “TRIANGLE” float area();




private:




float b, h;




};




Provide the definitions for both classes as Rectangle.cpp and Triangle.cpp.




You will then write a main.cpp program that produces the following output based on the input provided by the user.

Enter the
base and height for a triangle: 3 4
Enter the
length and width for a rectangle: 2 3
Area of
a
RECTANGLE is 6
Area of
a
RIGHT TRIANGLE is 6
The two
objects are of equal area
Press any
key to continue . . .



However, the main program must define two functions that use templated class types C and D.




/*




Produces the output:



Area of a <TYPE is <AREA



*/




void display(C obj);




/*




Returns true if the area of obj1 and obj2 are equal,



false otherwise.



*/




bool equalArea(C obj1, D obj2);




These two functions must be used in the main program when producing the output above. Submit this question separately (without the content from question 2).







Question 2:




For this project, you will extend the Rectangle and Triangle classes with a templated method that calculates the maximum number of other objects that will fit within the same area. For example, if the area of a Rectangle I is 18 and the area of a triangle t is 3, then r.maxFill(t) will return 6.




The following method declaration needs to be added to both the Rectangle and Triangle classes, the token C is the templated class type




/*




Returns the maximum number of other objects that would fit



into the area of this object.



*/




float maxFill(C &other);

Add the declaration to Rectangle.h and Triangle.h and provide definitions for the method. Then modify the main program to produce the following output.

Enter the base and height for a triangle: 2 3




Enter the length and width for a rectangle: 4 3




Area of a RECTANGLE is 12




Area of a RIGHT TRIANGLE is 3




The two objects are of unequal area




There are 4 triangles that will fit in the rectangle




There are 0.25 rectangles that will fit in the triangle




There are 1 rectangles that will fit into the rectangle




Press any key to continue . . .

More products