Hi am trying to write a program and its giving me this error "cannot instantiate abstract class", can someone help me please?
this is the code:
Header:

#ifndef ABSTRACTGEOMETRICOBJECT_H
#define ABSTRACTGEOMETRICOBJECT_H
#include <string>
using namespace std;

class GeometricObject
{
public:
  GeometricObject();
  GeometricObject(string color, bool filled);

public:
  string getColor();
  void setColor(string color);
  bool isFilled();
  void setFilled(bool filled);
  string toString();
  virtual double getArea() = 0;
  virtual double getPerimeter() = 0;

private:
  string color;
  bool filled;
}; // Must place semicolon here

#endif

Cpp:

#include "GO.h"

GeometricObject::GeometricObject()
{
  color = "white";
  filled = false;
}

GeometricObject::GeometricObject(string color, bool filled)
{
  this->color = color;
  this->filled = filled;
}

string GeometricObject::getColor()
{
  return color;
}

void GeometricObject::setColor(string color)
{
  this->color = color;
}

bool GeometricObject::isFilled()
{
  return filled;
}

void GeometricObject::setFilled(bool filled)
{
  this->filled = filled;
}

string GeometricObject::toString()
{
  return "Geometric object color " + color +
    " filled " + ((filled) ? "true" : "false");
}

Main:

#include <iostream>
#include "GO.h"
using namespace std;
int main()
{
    string colour;
    bool filed;
    cout<<"Enter color"<<endl;
    cin>>colour;
    cout<<"Enter is filled or not"<<endl;
    cin>>filed;
    GeometricObject g(colour, filed);
    g.setColor(colour);
    g.setFilled(filed);
    cout<<"The color is "<<g.getColor()<<endl;
    cout<<"The object is "<<g.isFilled()<<endl;
    cout<<g.toString()<<endl;
    system("pause");
    return 0;
}

thnx

Comments
Not URGENT to anyone else. Leave it out of your thread titles.

Hi,

I think, this is because of creation of the object on the abstract class name. I mean line no: 12 in the main function.

GeometricObject g(colour, filed);

You should not create the object for any Abstract class (here; you created 2 methods with =0 ie., pure virtual function (is nothing but Abstract class)).

virtual double getArea() = 0;
virtual double getPerimeter() = 0;

Thanks,
KMat

---------------------------------------------------------------------

Hi am trying to write a program and its giving me this error "cannot instantiate abstract class", can someone help me please?
this is the code:
Header:

#ifndef ABSTRACTGEOMETRICOBJECT_H
#define ABSTRACTGEOMETRICOBJECT_H
#include <string>
using namespace std;

class GeometricObject
{
public:
  GeometricObject();
  GeometricObject(string color, bool filled);

public:
  string getColor();
  void setColor(string color);
  bool isFilled();
  void setFilled(bool filled);
  string toString();
  virtual double getArea() = 0;
  virtual double getPerimeter() = 0;

private:
  string color;
  bool filled;
}; // Must place semicolon here

#endif

Cpp:

#include "GO.h"

GeometricObject::GeometricObject()
{
  color = "white";
  filled = false;
}

GeometricObject::GeometricObject(string color, bool filled)
{
  this->color = color;
  this->filled = filled;
}

string GeometricObject::getColor()
{
  return color;
}

void GeometricObject::setColor(string color)
{
  this->color = color;
}

bool GeometricObject::isFilled()
{
  return filled;
}

void GeometricObject::setFilled(bool filled)
{
  this->filled = filled;
}

string GeometricObject::toString()
{
  return "Geometric object color " + color +
    " filled " + ((filled) ? "true" : "false");
}

Main:

#include <iostream>
#include "GO.h"
using namespace std;
int main()
{
    string colour;
    bool filed;
    cout<<"Enter color"<<endl;
    cin>>colour;
    cout<<"Enter is filled or not"<<endl;
    cin>>filed;
    GeometricObject g(colour, filed);
    g.setColor(colour);
    g.setFilled(filed);
    cout<<"The color is "<<g.getColor()<<endl;
    cout<<"The object is "<<g.isFilled()<<endl;
    cout<<g.toString()<<endl;
    system("pause");
    return 0;
}

thnx

You need to create method bodies for GeometricObject::getArea() and GeometricObject::getPerimeter().

Ok

So the above posts are giving the right idea's kindof i think :P

For what i would consider a easier explination try the following.

An abstract class can not be created as an object, its purpose is to create an API or interface to a collection of objects who do the same thing but differently.

Take the following abstract class based on a shape.

class shape
{
public:
    virtual ~shape(){};
    virtual int GetArea( void ) = 0;
}

The above class says that all shapes have an area and you can get it, but it does not tell the class how to do that work. so you need an inherited class to do the work. Meet square and rectangle.

//shapes .h 
class square : public shape //inherits shape 
{
public:
   int length;
   virtual int GetArea( void );
}

class rectangle: public shape
{
public:
   int length;
   int width;
   virtual int GetArea( void );
}


// shapes .cpp 
int square::GetArea( void )
{
   return length*length;
}

int rectangle::GetArea( void )
{
   return length*width;
}

Now what you have is that shape defines the features / API / interface and the inherited classes square and rectangle defined the behaviour of the interface, differently as required.

This allows you to have a pointer of type shape to an object and call any function in the shape class without knowing which knid of shape you have the language does this for you take the following.

#include "shapes.h"

int main( void )
{
   square*    pMySquare = new square; /* create a square */
   rectangle* pMyRectangle = new rectangle /* create my rectangle */

   /* init my shapes */
   pMySquare->length = 4;

   pMyRectangle->length = 2;
   pMyRectangle->width = 3;
   shape* pSomeShape;


   /* i can now assign either of the square or rectangle pointers to pSomeShape
    * and call any function in the shape class and it will work
    */
   
   int result;
   
   pSomeShape = pMySquare;
   result = pSomeShape.GetArea(); //result will == 16 

   pSomeShape = pMyRectangle ;
   result = pSomeShape.GetArea(); //result will == 12 
}

And that my friend is polymorphism in a nutshell which is what you are doing.

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