# C ++ inheritance problem

1

was creating a program through inheritance. In the program I have a base class called FiguraGeometrica and from it Polygon is derived, from which Rectangle and Triangle are derived. Then there is the CollectionFiguras class, which has an array of FigureGeometrica pointers. CollectionFiguras

``````#ifndef ColeccionFiguras_h
#define ColeccionFiguras_h
#include "FiguraGeometrica.h"
#include <iostream>
using namespace std;
//clase control
class ColeccionFiguras{
protected:
FiguraGeometrica *pColeccion[10];
public:
ColeccionFiguras(){
}
}
}
void setColeccionAt(int idx, FiguraGeometrica &figura){
pColeccion[idx] = &figura;
cout<< pColeccion[idx] <<endl;
}
}
FiguraGeometrica* getColeccionAt(int idx){
return pColeccion[idx];
}
};

#endif /*ColeccionFiguras_h*/
``````

FiguraGeometrica

``````#ifndef FiguraGeometrica_h
#define FiguraGeometrica_h
#include <iostream>
using namespace std;
//clase base abstracta (pura)
class FiguraGeometrica{
public:
virtual int area()=0;
};

#endif /*FiguraGeometrica_h*/
``````

Polygon

``````#ifndef Polygon4_h
#define Polygon4_h
#include "FiguraGeometrica.h"
#include <iostream>
using namespace std;

class Polygon: public FiguraGeometrica{
protected:
int width, height;
public:
Polygon(int width, int height){
this->width = width;
this->height = height;
}
void setValues (int a, int b){
width=a;
height=b;
}
virtual int area()=0;

void printArea(){
cout<< this->area()<<'\n';
}
};
#endif /* Polygon4_h */
``````

Rectrangle

``````#ifndef Rectangle4_h
#define Rectangle4_h
#include <iostream>
#include "Polygon4.h"
using namespace std;

class Rectangle: public Polygon{
protected:
int diagonal;
public:
//constructor herencia
Rectangle(int diagonal, int width, int height): Polygon (width, height){
this->diagonal = diagonal;
}

int getDiagonal(){
return diagonal;
}

int area(){
return (width*height);
}
};
#endif /* Rectangle4_h */
``````

Triangle

``````#ifndef Triangle4_h
#define Triangle4_h
#include <iostream>
#include "Polygon4.h"
using namespace std;

class Triangle: public Polygon{
public:
//constructor herencia
Triangle(int width, int height): Polygon(width, height){
}

int area(){
return (width*height/2);
}
};
#endif /* Triangle4_h */
``````

The main is the following:

``````#include <iostream>
#include "Triangle4.h"
#include "Rectangle4.h"
#include "ColeccionFiguras.h"
using namespace std;

int main(){
ColeccionFiguras coleccion(2);
Rectangle rect(2,5,5);
Triangle trgl(4,5);

FiguraGeometrica *pFiguraGeometrica = &rect;
FiguraGeometrica *pFiguraGeometrica1 = &trgl;

coleccion.setColeccionAt(0, *pFiguraGeometrica);
coleccion.setColeccionAt(1, *pFiguraGeometrica1);

cout<< coleccion.getColeccionAt(i)->area()<<endl;
}
return 0;
}
``````

The program works fine, but now I have to include another class that is derived from FiguraGeometrica called Elipse and a Circle class that is derived from Elipse, I have written them in this way, but I do not know if they are correct:

Ellipse

``````#ifndef Elipse_h
#define Elipse_h
#include "FiguraGeometrica.h"
#include <iostream>
using namespace std;

class Elipse: public FiguraGeometrica{
protected:
int semieje_a, semieje_b;
public:
Elipse(int semieje_a, int semieje_b){
this->semieje_a = semieje_a;
this->semieje_b = semieje_b;
}
void setValues(int smayor, int smenor){
semieje_a = smayor;
semieje_b = smenor;
}
virtual int semi()=0;
void printEjes(){
cout<<this->semieje_a<<endl;
cout<<this->semieje_b<<endl;
}

};
#endif /* Elipse_h */
``````

Circle

``````#ifndef Circulo_h
#define Circulo_h
#include "Elipse.h"
#include <iostream>
using namespace std;

class Circulo: public Elipse{
protected:
public:
Circulo(int radio, int semieje_a, int semieje_b): Elipse(semieje_a, semieje_b){
}
int semi(){
}
};
#endif /* Cirulo_h */
``````

What I have to do is include instances of these classes in the main as it was done with Rectangle and Triangle, and I also have to print only the Polygon area and only the semiaxes of the Elipse. What I tried to do is make a dynamic_cast like this:

``````    int main(){
ColeccionFiguras coleccion(2);
Rectangle rect(2,5,5);
Triangle trgl(4,5);
Elipse elps(6,4);
Circulo crcl(3,3,3);

FiguraGeometrica *pFiguraGeometrica = &rect;
FiguraGeometrica *pFiguraGeometrica1 = &trgl;
FiguraGeometrica *pFiguraGeometrica2 = &elps;
FiguraGeometrica *pFiguraGeometrica3 = &crcl;

coleccion.setColeccionAt(0, *pFiguraGeometrica);
coleccion.setColeccionAt(1, *pFiguraGeometrica1);
coleccion.setColeccionAt(2, *pFiguraGeometrica2);
coleccion.setColeccionAt(3, *pFiguraGeometrica3);

if(dynamic_cast<Polygon*>(coleccion.getColeccionAt(i))!=0){
cout<< coleccion.getColeccionAt(i)->area()<<endl;
}
else{
cout<< coleccion.getColeccionAt(i)->semi()<<endl;
}
}
return 0;
}
``````

The problem is that it gives me an error, which says

[Error] can not declare variable 'elps' to be of abstract type 'Ellipse'   variable

The same for crcl. I tried to include the virtual int semi () in the class FigureGeometrica, but the error in addition to leaving the previous error, also leaves for rect and trgl. How can I solve this?

asked by davidllerenav 09.10.2018 в 02:52
source

2

Your problem is reproducible with this code:

``````class Elipse
{
virtual int semi() = 0;
};

int main()
{
Elipse elps;

return 0;
}
``````

That throws the following error (compiled with gcc):

``````error: cannot declare variable 'elps' to be of abstract type 'Elipse'
Elipse elps;
^
note:   because the following virtual functions are pure within 'Elipse':
class Elipse
^~~~~~
note:     'virtual int Elipse::semi()'
virtual int semi() = 0;
^~~~
``````

It is telling you that the class `Elipse` is abstract and for the same reason you can not create instances. It is abstract because it has pure virtual functions, in this case the `semi` function.

If you delete the function `semi` or make it not pure virtual your code will not give you this problem.

### To take into account:

Check your code a bit more because you have some things that do not make sense:

``````radio==semieje_a==semieje_b;
``````

In the previous line you probably wanted to assign, not compare:

``````radio=semieje_a=semieje_b;
``````

A circle is defined by a point and a radius, an ellipse by two points and the radius, it makes no sense that the circle receives more parameters than an ellipse:

``````Elipse elps(6,4);
Circulo crcl(3,3,3);
``````