Question: TriangleHeap.h / . cpp create triangle class where the members are stored on the heap ( dynamic float ) the triangle should have private mBase

TriangleHeap.h/.cpp create triangle class where the members are stored on the heap(dynamic float) the triangle should have private mBase and mHeight members, two public set methods to allow main to change each of these members' values. These should NOT receive float, just float parameters for the data to set your datas to. Public method GetArea that returns the area, this does not return a float*, just a float data result. Remember, the dynamic Triangle must fully implement the rule of 3. Because the copy constructor and default constructor are exclusive (only one is called), you must new your members in the dynamic triangle either in both constructors or in the member declaration area of your class.
Then in main declare two TriangleHeap variables and set their memebers to whatever two different values you want. Then declare a vector and add the two triangles to is using its push_back() method. Finally loop thru the vector and print each triangle's area to the screen. The vectors should NOT contain pointers
the getArea is not returning the correct area for the given height and base...
please look and tell me where I am going wrong so that I can get the program to print the correct area... it is all about pointers... perhaps Im not using them/understanding them correctly
class TriangleHeap
{
private:
float* mBase;
float* mHeight;
public:
TriangleHeap(float base =0.0, float height =0.0);
TriangleHeap(const TriangleHeap& other);
~TriangleHeap();
TriangleHeap& operator=(const TriangleHeap& other);
void SetBase(float base);
void SetHeight(float height);
float GetArea() const;
};
#include "TriangleHeap.h"
TriangleHeap::TriangleHeap(float base, float height) :mBase(new float(base)), mHeight(new float(height))
{
}
TriangleHeap::TriangleHeap(const TriangleHeap& other):mBase(new float(*other.mBase)), mHeight(new float(*other.mHeight))
{
}
TriangleHeap::~TriangleHeap()
{
delete mBase;
delete mHeight;
}
TriangleHeap& TriangleHeap::operator=(const TriangleHeap& other){
if (this != &other){
*mBase =*other.mBase;
*mHeight =*other.mHeight;
}
return *this;
}
void TriangleHeap::SetBase(float base)
{
*mBase = base;
}
void TriangleHeap::SetHeight(float height)
{
*mHeight = height;
}
float TriangleHeap::GetArea() const
{
float area =(*mBase)*(*mHeight)/2;
return area;
}
TriangleHeap heap1, heap2;
std::vector heapVector;
float base1, height1, base2, height2;
std::cout << "Enter base for first TriangleHeap: ";
std::cin >> base1;
std::cout << "Enter Height for first TriangleHeap: ";
std::cin >> height1;
heap1.SetBase(base1);
heap1.SetHeight(height1);
std::cout << "Enter base for second TriangleHeap: ";
std::cin >> base2;
std::cout << "Enter height for second TriangleHeap: ";
std::cin >> height2;
heap2.SetBase(base2);
heap2.SetHeight(height2);
heapVector.push_back(heap1);
heapVector.push_back(heap2);
std::cout <<"
Areas of TriangleHeap:" << std::endl;
for (size_t i =0; i < heapVector.size(); ++i)
{
std::cout << "Triangle "<< i+1<<":
\tArea: "<< heapVector[i].GetArea()<< std::endl;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!