0

I have a class called figGeom. The class circulo inherits from figGeom.

I need to create a class that allows me to save object pointers of type figGeom in an array. Can you help me?

I am also interested to know how to add pointers or memory addresses to the array.

Note: I have also rectangle and triangle classes, but I removed those to make the post shorter and more readable.

My current code gives me an error.

figuraGeom.h

#define TRIANGULO 0
#define RECTANGULO 1
#define CIRCULO 2

class figGeom
{
    double area;
    int tipoFig;

public:
    figGeom();
    figGeom(int);

    void setArea(double);
    double getArea();

    void setTipoFig(int);
    int getTipoFig();

    virtual double calcArea()=0;
    virtual void toString()=0;
};


class circulo:public figGeom
{
    //atributos
    double radio;

public:
    circulo();
    circulo(double);

    void setRadio(double);
    double getRadio();

    double calcArea();
    void toString();
};

figuraGeom.cpp

#include "figuraGeom.h"
#define _USE_MATH_DEFINES
#include <Math.h>

#include <string>
#include <iostream>

using namespace std;

//FIGGEOM
//Dispositivo
figGeom::figGeom(){}
figGeom::figGeom(int itipoDis){
    setTipoFig(itipoDis);
}

void figGeom::setArea(double iArea){area = iArea;}
double figGeom::getArea(){return area;}

void figGeom::setTipoFig(int iTipoDis){tipoFig = iTipoDis;}
int figGeom::getTipoFig(){return tipoFig;}

//CIRCULO
circulo::circulo(){}
circulo::circulo(double iRadio):figGeom(CIRCULO){setRadio(iRadio);}

void circulo::setRadio(double iRadio){radio = iRadio;}
double circulo::getRadio(){return radio;}

double circulo::calcArea(){return M_PI*pow(getRadio(),2);}
void circulo::toString(){cout << endl << endl << "  Tipo Figura: Circulo" << endl << endl;}


//LISTA FIGURAS
listaFiguras::listaFiguras(){
    *lista = NULL;
   setNumElementos(0);                          
} 

listaFiguras::~listaFiguras(){
    vaciarLista();
}

void listaFiguras::setNumElementos(int iNum){numElementos = iNum;}
int listaFiguras::getNumElementos(){return numElementos;}

void listaFiguras::vaciarLista()
{
    free(lista);
}

main.cpp

#include "figuraGeom.h"

#include <iostream>
using namespace std;

int tipoFig; 
char opcion;

figGeom * dI = NULL;
listaFiguras* listaFig = new listaFiguras();

void menu();
void menu_add_figura();
figGeom* pedirTriangulo();
figGeom* pedirRectangulo();
figGeom* pedirCirculo();

void menu();
void main()
{
    setlocale(LC_ALL, ""); //Configuración Regional

    menu();
}


void menu()
{

    do{
        cout << "SELECCIONA UNA OPCIÓN" << endl;
        cout << " [1]Añadir elemento" << endl;
        cout << " [2]Ver elemento" << endl;
        cout << " [3]Eliminar elemento" << endl;
        cout << " [4]Ver todos los elementos" << endl;
        cout << " [5]Eliminar todos los elementos" << endl << endl;
        cout << " [6]Salir" << endl << endl;
        cout << "Opción: ";
        cin >> opcion;

        switch (opcion){
                    case '1':
                        menu_add_figura();
                        break;
                    case '2': 

                        break;
                    case '3':

                        break;
            }
    }while(opcion != '6');

}


void menu_add_figura()
{
do{
        system("cls"); //limpiamos pantalla
        cout << "¿Qué tipo de figura desea crear?" << endl;
        cout << " [1]Triangulo" << endl;
        cout << " [2]Rectangulo" << endl;
        cout << " [3]Circulo" << endl;
        cout << " [4]Salir" << endl << endl;
        cout << "Figura: ";
        cin >> opcion;
        //PUNTERO AUX
        //listaFiguras-

        int new_numElem = (listaFig->getNumElementos()) + 1;

        listaFig->setNumElementos(new_numElem);

        figGeom** vector = new figGeom*[new_numElem];

        switch (opcion){
                case '1':
                    dI = pedirTriangulo(); //dI
                    *vector[new_numElem-1] = *dI;
                    break;
                case '2': 
                    dI = pedirRectangulo(); //dI
                    *vector[new_numElem-1] = *dI;
                    break;
                case '3':
                    dI = pedirCirculo(); //dI
                    *vector[new_numElem-1] = *dI;
                    break;
        }

        if(opcion != '4')
        {
            //cout << endl << " Area: " << dI->calcArea() << endl << endl; //Mostrar area
            cout << endl << " Area: " << vector[new_numElem-1]->calcArea << endl << endl; //Mostrar area

            system("pause"); //pausa
            system("cls"); //limpiamos pantalla
        }else delete dI;

    }while(opcion != '4');

}

figGeom* pedirCirculo()
{
    int radio;

    cout << " -Radio: ";
    cin >> radio;

    figGeom* dIaux;
    dIaux = new circulo(radio);
    return dIaux;
}
3
  • Create a array of pointers for base class and insert the derived class objects. It will point to derived class due to dynamic parallelism. In your case figGeom* ptr[N]; Commented May 12, 2014 at 9:43
  • 2
    Why aren't you just using an std::vector and storing the pointers in that? Commented May 12, 2014 at 9:50
  • 1
    You should post the error message. Commented May 12, 2014 at 10:28

2 Answers 2

1
  1. Don't use raw pointers. Using raw pointers often leads to memory leaks. Instead, use C++11's (or Boost's) various smart pointer classes such as std::unique_ptr or std::shared_ptr. They will handle the issue of deleting objects when they're no longer needed.

    #include <memory>
    
    std::shared_ptr<figGeom> createFigure()
    {
        std::shared_ptr<figGeom> thing(new figGeom(/* whatever */));
        return thing;
    }
    
  2. You don't need to use a simple array, and you certainly don't need to write your own container class. Instead, use one of the many containers from the standard library... they're fast, efficient, handle their own memory allocation and deallocation and perhaps most importantly, they've been well tested. std::vector or std::array woudl work just fine... use vectors when you only know the size of the container at runtime, and arrays when you know the size of the container at compile time. Both containers can be accessed in the same way as a standard simple array.

    #include <vector>
    
    std::vector<std::unique_ptr<figGeom>> createFigures()
    {
        std::vector<std::unique_ptr<figGeom>> figures;
        figures.push_back(createFigure());
        figures[0].setArea(1234.56);
        return figures;
    }
    
  3. With such a simple, lightweight class as this, you don't really need to use new at all... you can allocate instances on the stack, and just copy them into a container when needed.

    figGeom createShape()
    {
        figGeom shape(/* whatever */);
    
        return shape;
    }
    
    std::vector<figGeom> createShapes()
    {
        std::vector<figGeom> shapes;
        shapes.push_back(createShape());
    
        return shapes;
    }
    
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this:

figGeom* array[10];// change 10 to any number you need

then create pointer to circulo class and save it in the array

for(int i=0;i<10;i++){
  array[i] = new circulo(/*parameters to constructor*/);
}

after you done with the objects, you should free the memory

for(int i=0;i<10;i++){
  delete array[i];// Note, your destructor in figGeom class should be virtual
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.