Abstract base class help

I have an Abstract base class Abc along with two other classes that im trying to get working, but im having trouble declaring the object. I want to declare Abc as a pointer with the coordinates (4,3) , then use it as a circle and find its area. Im getting this error:
I:\C++\AbstractBaseClass_Shapes\main.cpp||In function 'int main()':|
I:\C++\AbstractBaseClass_Shapes\main.cpp|7|error: initializer expression list treated as compound expression|
I:\C++\AbstractBaseClass_Shapes\main.cpp|7|warning: left-hand operand of comma has no effect|
I:\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(4,3);
    //Abc * Obj(4,3); doesnt work, how to do this?
    Obj = new Circle(5.0); //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
#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
#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()
{

}


I really appreciate any help, thanks!
Last edited on
closed account (o1vk4iN6)
 
Abc * Obj(4,3);


You are initializing a pointer here and it is an invalid initialization.

You want to allocate an Ellipse on the heap then assign the poitner to obj.

 
Abc *obj = new Ellipse( 4, 3 );
Topic archived. No new replies allowed.