Any experts who can help me out with inheritance polymorphism please?

#include <iostream>
void Triangle::move(float dx, float dy) {
    x+=dx;
    y+=dy;
}
void Triangle::zoom(float scale){
base*=scale;
height*=scale;
}
float Triangle::area(){
return base*height*(1/2);
}

int main () {
float t1, t2;
Triangle tri1, tri2;
tri1.move(12,0);
t1=tri1.area(); t2=tri2.area();
cout << "area: " << t1 << endl;
cout << "area: " << t2 << endl;
tri1.zoom(2.5);
t1=tri1.area();
cout << "area: " << t1 << endl;
}
This is my main cpp

class Triangle{
public:
    void move(float dx, float dy);
    void zoom(float scale);
    float area();
private:
    float x, y;
    float base, height;
};
This is my header file

I am currently practicing inheritance and polymorphism by myself so I picked up with easier one(which I think is drawing geometric shape) but it is quite harder than I thought. Can someone tell me how to change this with inheritance and polymorphism?
The other problem is that I also found this compiler called OpenGL which can literally draw shapes and I found that codeblock has the option for it. I really wanna try it out but I don't know how because it is not drawing anything even though I selected the option for OpenGL as project type.
Jensen HithonAsked:
Who is Participating?
 
sarabandeCommented:
the main goal for to using inheritance polymorphism in programming is to be able to use a baseclass pointer or baseclass reference and then virtually call a function implemented in the derived class which is declared both in baseclass and in derived class.

// baseclass
class Polygon
{
    
public:
       virtual void draw(Canvas * pCanvas) = 0;   // 
};

Open in new window


the = 0 means, that the baseclass doesn't provide an implementation. that implies that a pure baseclass object which was not base of a derived class can not exist. the baseclass is called an 'abstract' class and the function a pure virtual function.

Polygon poly * = new Polygon;  // error, can't create object: Polygon is an abstract class
poly->draw(myCanvas);

Open in new window


if the compile would allow to create objects of an abstract class, the call to draw member function would crash as the compile can't  found an implementation.

so, an abstract baseclass must have derived classes which provide implementations of the virtual functions declared in the baseclass. the compiler would not allow you to create an object of the derived class if it hasn't an implementation of all pure virtual functions  declared in (one of ) the baseclass(es). this check happens at compile time.

class Triangle()
{
     std::vector<Point> points;
public:
     Triangle(Point p1, Point p2, Point p3)
     { Point pts[] = { p1, p2, p3 }; points.push_back(&pts[0], &pts[3]); }
     void draw(Canvas * pCanvas) {  SomeGraphicLibrary::DrawPolygon(pCanvas, &points[0], 3);  } 
};

class Rectangle()
{
     std::vector<Point> points;
public:
     Rectangle(Point p1, Point p2, Point p3, Point p4)
     { Point pts[] = { p1, p2, p3, p4 }; points.push_back(&pts[0], &pts[4]); }
     void draw(Canvas * pCanvas) {  SomeGraphicLibrary::DrawPolygon(pCanvas, &points[0], 4);  } 
};

Open in new window

the point now is that the decision which implementation to use happens at runtime what for example may be used like that:

void SomeClass::some_function()
{
      std::vector<Polygon *> polygons; 
      Canvas * pCanvas = GraphicLibrary::CreateCanvas();
      Triangle  t1( Point (1,2), Point(3,5), Point (6, -1));
      polygons.push_back(&t1);
      Rectangle r1( Point (2,2), Point(5,2), Point (5, -1), Point(2,-1));
      polygons.push_back(&r1);

      //...  more
      // draw all polygons
      for (std::vector<Polygon*>::iterator it = polygons.begin(), end = polygons.end(); it != end; ++it)
      {
           Polygon p = *it;
           p->draw();           // draws either a Triangle or a Rectangle
      }
}

Open in new window


I also found this compiler called OpenGL ... but I don't know how because it is not drawing anything

can you show the code where you used OpenGL ?

Sara
0
 
Fabrice LambertFabrice LambertCommented:
To answer your first question:
A Triangle is a Shape.
But a shape by itself, can't compute anything, because it heavilly depend on the real shape (Triangle in your case), os a shape can only provide services that will be implemented by inherited classes.

Also, since it inherit from Shape, a Triangle have an entity semantiic, wich mean it must be non copiable, and non assignable.
class Shape
{
public:
    void move(float dx, float dy) = 0;
    void zoom(float scale) = 0;
    float area() = 0;
};

class Triangle: public Shape
{
private:
    float x, y;
    float base, height;
public:
        // constructor (add a parameterized one if you want)
    Triangle();
        // delete copy constructor and assignment operator
    Triangle(Triangle const&) = delete;
    Triangle& operator=(Triangle const&) = delete;
    void move(float dx, float dy);
     void zoom(float scale);
     float area();
};

Open in new window

0
 
Fabrice LambertFabrice LambertCommented:
No further comments from OP.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.