Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Learning Aids/Tools 2 years ago
Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
I have thous objects:
Polygon p1, p2;
And I have an inheriting class of Polygon called Triangle, and I try to do:
Polygon
Triangle
p1 = Triangle(temp1, temp2, temp3); // temp 1,2,3 are lengths of sides
But for some reason the destructor for Triangle is called at the end of construction.
Rectangle::~Rectangle(void) { Polygon::~Polygon(); } Polygon::~Polygon(void) { if (sides != NULL) { delete [] sides; sides = NULL; } }
Then it runs the destructor for Polygon a second time.
so after the code ends this is what the debugger says about p1( n is number of sides):
p1
n
p1 {n=3 sides=0x0062c070 {-17891602} } Polygon
Questions:
EDIT: as requested:
/*returns true if for the polygons A and B: (a) for each side on polygon A there is an equal side on polygon B (b) the number of sides in polygons A and B are equal (c) sum of sides of A equals the sum of sides of B */ bool Polygon::operator==(const Polygon& p) const { if (n != p.n) return false; if(circumference() != p.circumference()) return false; for (int i=0; i < n; i++) if (!doesSideHasEqual(sides[i], p, n)) return false; return true; }
Also, thanks for the explanation why it ran ~Polygon, will take into account.
~Polygon
Polygon p1; p1 = Triangle(temp1, temp2, temp3); // temp 1,2,3 are lengths of sides
is in fact giving you result that is out of your expectation.
What you want to do is something like
Polygon& p1; Triangle t(temp1, temp2, temp3); p1 = t;
or
Polygon* p1 = new Triangle(temp1, temp2, temp3);
The problem in
p1 = Triangle(temp1, temp2, temp3);
is not only the Triangle object is temporary and destroyed right after the statement, but, more seriously, the copy from the Triangle object to p1 which is the Polygon, is only copying as is a Polygon. Therefore, the "copied" p1 is not containing any com/tag/information">information of Triangle.
Another problem is the way you write the destructor. Destructor in C++ is automatically chained (I think you are coming from Java com/tag/background">background which we need to explicitly invoke super class' finalizer)
Rectangle::~Rectangle(void) { Polygon::~Polygon(); // can be removed }
Therefore you shouldn't call Polygon's destructor.
Another thing about destructor is, if your class is supposed to be inherited (your Polygon is a good example), you should declare your destructor to be virtual. For the reason behind, search for "virtual destructor".
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.