Trouble with abstract base classes

EDIT:
Thanks for the help guys! i've just got one more question, how would i use the constructor for the Obj? i want to declare its (x,y) position but i dont know how to do so, i included what i tried in my new updated code.



ERROR:

F:\C++\AbstractBaseClass_Shapes\main.cpp||In function 'int main()':|
F:\C++\AbstractBaseClass_Shapes\main.cpp|7|error: initializer expression list treated as compound expression|
F:\C++\AbstractBaseClass_Shapes\main.cpp|7|warning: left-hand operand of comma has no effect|
F:\C++\AbstractBaseClass_Shapes\main.cpp|7|error: invalid conversion from 'int' to 'Abc*'|
||=== Build finished: 2 errors, 1 warnings ===|


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "Abc.h"
using namespace std;

int main()
{
    Abc * Obj;
    //Abc * Obj(4,3); doesnt work, how to do this?
    Obj = new Circle(5); //allocate new circle object
    std::cout << "The area of the circle: " << Obj->Area() << std::endl;
    delete Obj;

}


Abc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef ABC_H
#define ABC_H


class Abc
{
    public:
        virtual ~Abc();                             //destructor
        virtual double Area() const = 0;            //pure virtual
        void move(int nx, int ny) {x = nx; y = ny;} //all classes use same function
        Abc(double x0 = 0, double y0 = 0): x(x0),y(y0){}
    protected:
    private:
        double x;   //x and y of origin
        double y;
};

class Ellipse : public Abc
{
    public:
        Ellipse(double a0, double b0) {a = a0;b = b0;}
        virtual double Area() const;
        ~Ellipse(){}
    protected:
    private:
        double a; //horizontal and vertical radius
        double b;
        double angle; //anlge it is at(not used yet)
};
class Circle: public Abc
{
    public:
        Circle(double ra) {r = ra;}
        virtual double Area() const;
        ~Circle(){}
    protected:
    private:
        double r; //radius
};
#endif // ABC_H


Abc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Abc.h"
#include <iostream>

double Ellipse::Area() const
{
        return (3.14 * a * b);
}

double Circle::Area() const
{
    return (3.14 * r * r);
}

Abc::~Abc()
{

}


Appreciate the help!
Last edited on
Obj is a pointer so you have to write Obj->Area() to call the Area function.

In Ellipse and Circle you have declared Area functions with extra parameters. I guess this is just a mistake because when you define them you have left out the parameters.
Thanks for that, corrected! but i'm getting the same errors as before, any ideas of why?
Peter87 nailed both errors. You need to change Obj.Area() to Obj->Area() (in main), and you need to declare and define Area() without parameters in both Ellipse and Circle. In other words, you haven't overridden (abstract) ABC::Area() yet, you've declared and defined two new methods, Ellipse::Area(double, double) and Circle::Area(double). This makes Ellipse and Circle abstract classes still.

Note: shouldn't Circle extend Ellipse?
Last edited on
I think that maybe you forgot to specify qualificator const in functios Area in class Circle

virtual double Area(); // here shall be const.

In any case if you get an error you should show your updated code.
Thanks for the help guys! i've just got one more question, how would i use the constructor for the Obj? i want to declare its (x,y) position but i dont know how to do so, i included what i tried in my new updated code.

Last edited on
maybe:
 
Abc * Obj = new Abc (4,3);


the Abc constructor have to be modified too (perhaps)

CMIIW
im trying to declare the object as a circle, so i wouldn't allocate it as an Abc object. Still stuck
If you want to set the position of the circle, then you ought to provide methods to set it.
1
2
Circle::Circle( double x, double y, double r):
  Abc(x,y), r(r){}


shouldn't Circle extend Ellipse?
Nope. Look for Liskov's sustitution principle.
You can change any axis in the ellipse, but in the circle they must be equal (restriction)
Topic archived. No new replies allowed.