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 #endifCpp:
#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