Hello,
I am writing a program whose source code can be viewed and compiled here. http://goo.gl/InB8B6 . If you try to compile it by reference, you will get an error.
main.cpp: In function 'int main ()':
main.cpp: 42: 8: error: cannot declare variable 'sh' to be of abstract type 'Shape'
Shape sh;
Some details:
Shapeabstract shape classCanvasclass canvas. Theadd()method returns a pointer to the shape. And then you can perform methods on this figure. For example:shape->draw(),shape->hide()
Please help me figure out how to fix the code so that:
- You could create an object of class
Shape - It was possible to correctly execute
Canvas::add()and remember the pointer to the figure
For convenience, here are the code snippets:
Shape.h
#pragma once using namespace std; class Shape { public: virtual void draw() = 0; virtual void hide() = 0; virtual ~Shape() {} }; Line.h (one of the heirs of Shape)
#pragma once #include <iostream> #include "Point.h" #include <math.h> #include "Shape.h" using namespace std; class Line : public Shape { public: Line(); Line(Point a, Point b); virtual void draw(); virtual void hide(); void move(Point da, Point db); void setA(Point a); Point getA(); void setB(Point B); Point getB(); float len(); protected: Point a; Point b; }; Line::Line() { Point a, b; Line(a, b); } Line::Line(Point a, Point b) { this->a = a; this->b = b; } void Line::draw() { cout << " "<< a.print() <<"-"<< b.print() << " " << endl; } void Line::hide() { cout << " " << a.print() << "-" << b.print() << " " << endl; } void Line::move(Point da, Point db) { this->hide(); a += da; b += db; this->draw(); } // ... main.cpp
#include <iostream> #include "Point.h" #include "Line.h" #include "Triangle.h" #include "Quadrangle.h" #include "Pentagon.h" #include "Ellipse.h" #include "Circle.h" #include "Canvas.h" int main() { setlocale(LC_ALL, ""); Point c2a(0, 0); Point c2b(1, 0); Point c2c(1, 2); Point c2d(0, 2); Point c2e(1, 0); Canvas cnv; Shape sh; //sh = cnv.add(c2a); //sh->draw(); return 0; }