Question: Memory leaks.. c++ #ifndef SHAPE_H #define SHAPE_H #include #include using namespace std; class Shape{ protected : int x; int y; string name; public : Shape(

Memory leaks.. c++

#ifndef SHAPE_H

#define SHAPE_H

#include

#include

using namespace std;

class Shape{

protected:

int x;

int y;

string name;

public:

Shape(int centerX, int centerY, string name)

:x(centerX), y(centerY), name(name){}

virtual double area() = 0;

virtual void draw(){}

virtual ~Shape(){}

};

#endif

#include "Shape.h"

#include

#ifndef CIRCLE_H

#define CIRCLE_H

class Circle:public Shape{

private:

const double PI = 3.14159;

protected:

double radius;

public:

Circle(int centerX, int centerY, string name, double newRadius)

:Shape::Shape(centerX, centerY, name), radius(newRadius)

{

}

virtual double area(){

return radius*radius*PI;

}

virtual void draw(){

for(int i=0; i<=2*radius; ++i){

for(int j=0; j<=2*radius; ++j){

int distance = sqrt((i-radius)*(i-radius)+(j-radius)*(j-radius));

if(radius==distance){

cout << "*";

}else{

cout << " ";

}

}

cout << endl;

}

}

};

#endif

#include "Shape.h"

#ifndef TRIANGLE_H

#define TRIANGLE_H

class Triangle:public Shape{

protected:

double base;

double height;

public:

Triangle(int centerX, int centerY, string name, double newBase, double newHeight)

:Shape::Shape(centerX, centerY, name), base(newBase), height(newHeight)

{

}

virtual double area(){

return base*height/2;

}

virtual void draw(){

for(int i=0; i

for(int j=0; j

if(j==0 || i==height-1 ||j<=i){

cout << "*";

}else{

cout << " ";

}

}

cout << endl;

}

}

};

#endif

#include "Shape.h"

#ifndef RECTANGLE_H

#define RECTANGLE_H

class Rectangle:public Shape{

protected:

double base;

double height;

public:

Rectangle(int centerX, int centerY, string name, double newBase, double newHeight)

:Shape::Shape(centerX, centerY,name), base(newBase), height(newHeight)

{

}

virtual double area(){

return base*height;

}

virtual void draw(){

for(int i=0; i

for(int j=0; j

if(i==0||j==0||i==(height-1)||j==(base-1)){

cout << "*";

}else{

cout << " ";

}

}

cout << endl;

}

}

virtual ~Rectangle(){}

};

#endif

#include "Rectangle.h"

#ifndef SQUARE_H

#define SQUARE_H

class Square :public Rectangle{

public:

Square(int centerX, int centerY, string name, double newSide)

:Rectangle::Rectangle(centerX, centerY, name, newSide, newSide){}

virtual double area(){

Rectangle::area();

}

virtual void draw(){

Rectangle::draw();

}

};

#endif

#include "Shape.h"

class Picture

{

public:

Picture():head(nullptr)

{

}

void add(Shape *sp){

head = new ListNode(sp, head);

}

void drawAll(){

for(ListNode *p=head; p!=nullptr; p=p->next){

p->info->draw();

}

}

double totalArea(){

double total = 0.0;

for(ListNode *p=head; p!=nullptr; p=p->next){

total += p->info->area();

}

return total;

}

~Picture(){

ListNode::deleteList(head);

}

private:

struct ListNode{

Shape * info;

ListNode * next;

ListNode(Shape *sp, ListNode *newNext):

info(sp), next(newNext){

}

static void deleteList(ListNode *L){

if(!L){

deleteList(L->next);

delete L;

}

}

};

ListNode * head;

};

#include "Shape.h"

#include "Rectangle.h"

#include "Triangle.h"

#include "Circle.h"

#include "Square.h"

#include "Picture.h"

int main(){

Picture shapeList;

shapeList.add(new Triangle(1,1,"tri1",5,5));

shapeList.add(new Triangle(1,1,"tri1",3,4));

shapeList.add(new Circle(1,1,"cir1",5));

shapeList.add(new Circle(1,1,"cir2",10));

shapeList.add(new Square(1,1,"squ1",5));

shapeList.add(new Square(1,1,"squ2",10));

shapeList.add(new Rectangle(1,1,"rec1",4,8));

shapeList.add(new Rectangle(1,1,"rec2",8,4));

shapeList.drawAll();

cout << shapeList.totalArea() << endl;

return 0;

}

********

* *

* *

********

****

* *

* *

* *

* *

* *

* *

****

**********

* *

* *

* *

* *

* *

* *

* *

* *

**********

*****

* *

* *

* *

*****

*********

** **

** **

* *

** **

* *

* *

* *

* *

* *

* *

* *

* *

* *

* *

* *

** **

* *

** **

** **

*********

*******

** **

** **

* *

* *

* *

* *

* *

** **

** **

*******

*

**

***

***

*

**

***

****

*****

600.199

==15381== HEAP SUMMARY:

==15381== in use at exit: 73,344 bytes in 17 blocks

==15381== total heap usage: 17 allocs, 0 frees, 73,344 bytes allocated

==15381==

==15381== LEAK SUMMARY:

==15381== definitely lost: 16 bytes in 1 blocks

==15381== indirectly lost: 624 bytes in 15 blocks

==15381== possibly lost: 0 bytes in 0 blocks

==15381== still reachable: 72,704 bytes in 1 blocks

==15381== suppressed: 0 bytes in 0 blocks

==15381== Rerun with --leak-check=full to see details of leaked memory

==15381==

==15381== For counts of detected and suppressed errors, rerun with: -v

==15381== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

My codes work find but valgrind gives memory leaks.... Help me to remove the memory leaks..

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 Databases Questions!