Hello, This is about an assignment and the requirements (to pass) is as follow:

1-Abstract base Class Shape must have the following consturctors:
area()
print()
Recommended constructors:

    Point( double x, double y, double size)
    Circle( double x, double y, double radie)
    Rectangle( double x, double y, double width, double height)
    Polygon(double x, double y, Vertex *varr, int num)

2-Linked list ShapeList with following recommended methods:

    ShapeList()
    ShapeList( const ShapeList &shapes)
    ~ShapeList()
    add( const Shape& s )     // Add a copy of s in the list
    remove( const Vertex &v)  // remove all shapes "close to" v
    area()                    // return the total area of all shapes
    print()                   // print out the whole list

This assigment gave me this to work with as main:

#include <iostream>
using namespace std;
#include "shapelist.h"

int main()
{
  ShapeList list;
  Vertex varr[] = { Vertex(0,0), Vertex(10,0),
                    Vertex(5,2), Vertex(5,5) };
  list.add( Polygon( 1, 4, varr, 4 ) );
  list.add( Rectangle( 4, 10, 2, 4) );
  list.add( Circle( 5,5, 3)  );
  list.add( Point( 6, 7, 1 ) );
  list.print();
  cout << " Total area: " << list.area() << endl;

  ShapeList list2(list);

  list2.print();
  cout << " Total area: " << list2.area() << endl;
  list.remove( Vertex(5,5) );
  list.print();
  cout << " Total area: " << list.area() << endl;
  list2.print();
  cout << " Total area: " << list2.area() << endl;
  return 0;
}

The Program shall output:

>  POLYGON:(1,4) {(0,0) (10,0) (5,2) (5,5) }
>  RECTANGLE:(4,10) (2,4)
>  CIRCLE:(5,5) 3
>  POINT:(6,7) 1
>   Total area: 54.7743
>  POLYGON: (1,4) {(0,0) (10,0) (5,2) (5,5) }
>  RECTANGLE: (4,10) (2,4)
>  CIRCLE: (5,5) 3
>  POINT: (6,7) 1
>   Total area: 54.7743
>  POLYGON: (1,4) {(0,0) (10,0) (5,2) (5,5) }
>  RECTANGLE: (4,10) (2,4)
>  POINT: (6,7) 1
>   Total area: 26.5
>  POLYGON: (1,4) {(0,0) (10,0) (5,2) (5,5) }
>  RECTANGLE: (4,10) (2,4)
>  CIRCLE: (5,5) 3
>  POINT: (6,7) 1
>   Total area: 54.7743

I have solved everything but:

ShapeList list2(list);

I don't know how to solve that line of code.
I appriciate any help I gain with this.
Thanks in beforehand.

The constructor needs to duplicate the items in the parameter. Iterate through each of the shapes in list parameter and call list2.Add() method to add the shape to list2. Since you have already coded other functions that iterate through all the shapes this constructor should not be all that difficult for you.

Edited 2 Years Ago by Ancient Dragon

Hello, thanks for the fast reply.
I had the same thought but I got but got stucked on the code: How does the code look like?

probably very similar to the way you looped through them in the print function. Instead of printing, just call Add() method. I can't give you actual code because I have no idea how to iterate through the shapes.

Edited 2 Years Ago by Ancient Dragon

I was thinking of an example to the theory you giving me.

I could send all the code I have and show you exactly WHERE I am stuck. But looking at code like that takes too long and very clumps. But I am going to try to explain it thoroughly.

The problem I really have is that I need the right syntax to do it. But here is the code to what I've done:

#include <iostream>
#include "Vertex.h"
#include "ShapeList.h"
using namespace std;

class ShapeNode {
public:
  const Shape* s;
  ShapeNode* bak;
  ShapeNode* fram;
  ShapeNode(const Shape* _s, ShapeNode* _bak, ShapeNode* _fram) : s(_s), bak(_bak), fram(_fram) {
//    cout << "\tNode_Constructor" << endl;
  }
  ~ShapeNode() {
//    cout << "\tNode_Destructor" << endl;
//    delete s;
  }

  void radera(const Vertex& v) const {
/*cout << endl << "\tNode_Radera_Begin" << endl;
    Vertex vx = v;
    Shape* pk;
cout << "  Efter Pk-pekaren" << endl;
    pk->Tabort(vx);*/
cout << "\tNode_Radera_Finish" << endl;
  }
};

int ShapeList::Rkn = 0;                         // General counter
int ShapeList::AntLst = 0;                      // Counter for lists

ShapeList::ShapeList() {
  ny = new ShapeNode(0, 0, 0);
  ny->fram = ny->bak = ny;
  AntLst++;
}

ShapeList::ShapeList(const ShapeList& listofShapes) { // <--- Problem on this method
  P = new ShapeList[1];
  P[0] = shapes;
}

ShapeList::~ShapeList() {}

// Metoder
void ShapeList::add(const Shape &s){
//  cout << "     ShapeList_Add_Begin" << endl;
  Rkn++;
  const Shape* tmpPek = &s;
  tmpPek = tmpPek->clone();
  ShapeNode *tmp = ny;
  ny = new ShapeNode(tmpPek, 0, tmp);
  tmp->bak = ny;
  cout << "     ShapeList_Add_Finish" << endl;
}

/*void ShapeList::remove(const Vertex& v) {
cout << "     ShapeLis_Remove_Begin" << endl;
  int Cnt = 0;
  for (const ShapeNode* p=ny; Cnt<Rkn; p=p->fram) {
      Cnt++;
      cout << Cnt << ":";
  }
} */

double ShapeList::area() {
  double TotArea;
  int Cnt = 0;
  for (const ShapeNode* p=ny; Cnt<Rkn; p=p->fram) {
      TotArea += p->s->area();
      Cnt++;
  }
  return TotArea;
}

void ShapeList::print() {
int Cnt = 0;
//  for (const ShapeNode* p=ny; p!=0; p=p->fram) {
  for (const ShapeNode* p=ny; Cnt<Rkn; p=p->fram) {
      p->s->print();
      Cnt++;
  }
}

With the help I got from you I actually got an idea of how I might do it, but again. It's the syntax I am rusty at.

Let's say that main has

ShapeList list2(list);

My problem lays on the creation of an object with input for the same object that was created.

 P = new ShapeList(listofShapes);

Where P is defined as

private:
    ShapeList* P;

in ShapeList. I don't know the syntax to create a copy constructor out of this.
Again, thank you for any help given

This article has been dead for over six months. Start a new discussion instead.