-1

This is my code. I need help making this Dynamic Binding work.

#include<iostream>
#include<conio.h>
using namespace std;

class Shape {
    protected:
    double x,y;
    public:
    void get_data(double a,double b=3.14)   {
        x=a;
        y=b;
    }   
    virtual void display_area(){
    };
};

class Rectangle : public Shape  {
    public:
    void display_area() {
        cout<<"\nArea of Rectangle : "<<x*y<<" units.\n";
    }
};
class Circle : public Shape {
    public:
    void display_area() {
        cout<<"\nArea of Circle : "<<y*x*x<<" units.\n";
    }
};
class Triangle : public Shape   {
    public:
    void display_area() {
        cout<<"\nArea of Triangle : "<<0.5*x*y<<" units.\n";
    }
};

main()  {
    Shape *ptr;
    char opt,wait;
    double a,b;
    do{
        system("cls");
        cout<<"\n1.Area of Rectangle\n2.Area of Circle\n3.Area of Triangle\n4.Exit\n\nEnter your Option : ";
        opt=getche();
        switch(opt) {
            case '1':
                *ptr = new Rectangle;
                cout<<"\nEnter Length : ";
                cin>>a;
                cout<<"\nEnter Width  : ";
                cin>>b;
                ptr->get_data(a,b);
                ptr->display_area();
                break;
            case '2':
                *ptr = new Circle;
                cout<<"\nEnter Radius : ";
                cin>>a;
                ptr->get_data(a);
                ptr->display_area();
                break;
            case '3':
                *ptr = new Triangle;
                cout<<"\nEnter Base   : ";
                cin>>a;
                cout<<"\nEnter Height : ";
                cin>>b;
                ptr->get_data(a,b);
                ptr->display_area();
                break;
            case '4':
                cout<<"\n\nGoodBye ! Have a Nice Day !\n";
                exit(0);
            default:
                cout<<"\n\nEnter a Valid Option !\n";
        }
        cout<<"\n\nPress Any Key to Continue....";
        wait=getche();
    }while(opt!='4');
}

The error I get is:

5   7  [Note] Shape& Shape::operator=(const Shape&)

5   7  [Note] no known conversion for argument 1 from 'Rectangle*' to 'const Shape&'

55  10 [Error] no match for 'operator=' (operand types are 'Shape' and 'Circle*')

55  10 [Note] candidate is:

5   7  [Note] Shape& Shape::operator=(const Shape&)

5   7  [Note] no known conversion for argument 1 from 'Circle*' to 'const Shape&'

62  10 [Error] no match for 'operator=' (operand types are 'Shape' and 'Triangle*')

62  10 [Note] candidate is:

5   7  [Note] Shape& Shape::operator=(const Shape&)

5   7  [Note] no known conversion for argument 1 from 'Triangle*' to 'const Shape&'
halfer
  • 19,824
  • 17
  • 99
  • 186
  • **Please take a few weeks to read a [good C++ programming](https://stroustrup.com/programming.html) book** then [this C++ reference](https://en.cppreference.com/w/cpp) and more about [virtual method tables](https://en.wikipedia.org/wiki/Virtual_method_table) – Basile Starynkevitch Jun 09 '20 at 06:05

2 Answers2

4

You are assigning ptr incorrectly. You should not be dereferencing it when assigning an object to it.

Change this

*ptr = ...;

To this

ptr = ...;

Also, you are leaking the objects you allocate with new. You need to call delete ptr; when you are done using each object. Which also means you need to add a virtual destructor to Shape so the proper descendant destructor is called correctly:

class Shape {
... 
public:
    virtual ~Shape() {}
...
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • That was truly helpful. It resolved my problem. Please tell me more about 'dereferencing'.. im confused. – Pirthvi Hasan G Jun 09 '20 at 06:03
  • @Cyber-x-x-Stunner you should read a [good C++ book](https://stackoverflow.com/questions/388242/). There are whole chapters on what pointers are and how they work. – Remy Lebeau Jun 09 '20 at 06:14
0

I would like to supplement. Don't use raw pointer. If you used smart ponter std::unique_ptr then you could avoid mistakes. And you don't need to think about deleting object. For example:

std::unique_ptr<Shape> ptr;
ptr = std::make_unique<Rectangle>();
Anton Shwarts
  • 515
  • 2
  • 10