Hi im trying to learn polymorphism and i stumbled upon a question. Polymorphism to me means 1 with many forms.
here is my code:

// virtual members
#include <iostream>
using namespace std;

class CPolygon {
  protected:
    int width, height;
  public:
    void display();
    virtual string area ()
      { return ("FIRST"); }
  };

class CRectangle: public CPolygon {
  public:
    string area ()
      { return ("SECOND"); }
  };

void CPolygon::display(){
     cout<<area()<<endl;
     }

int main () {
  CRectangle rect;
  CPolygon poly;
  CPolygon * p1 = &rect;
  CPolygon * p3 = &poly;
poly.display();

  system("pause");
  return 0;
}

basically it displays FIRST. I would like to add code to change it to display SECOND. How would i do so? any help is appreciated. i already did p1->area and such in the main code. This is me trying to learn it in a class code.

Of course it does. Try changing line 29 to p1->display(); :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.