i need to add virtual function to my coding. somebody please help. whre should i put it

heres the code.
figure.h

//This is the header file figure.h. 
using std::cout;

class figure
{
      public:
             virtual void center();
             void draw();
             void erase();
};

void figure::center()
{
     cout << "Centering the Figure";
     cout << "\n";
     cout << "\n";

rectangle.h

//This is the header file rectangle.h. 
using std::cout;

class rectangle : public figure
{
      public : void erase();
               l void draw();
};


void rectangle::draw()

{
     cout << "Drawing rectangle";
}

void rectangle::erase()

{
     cout << "Erasing rectangle";
}

triangle.h

//This is the header file triangle.h.
using std::cout;

class triangle : public figure
{
      public :  void erase();
                void draw();
};


void triangle::draw()

{
     cout << "Drawing triangle";
}

void triangle::erase()

{
     cout << "Erasing triangle";
}

virtual.cpp

//This is the header file triangle.h.
using std::cout;

class triangle : public figure
{
      public : virtual void erase();
               virtual void draw();
};


void triangle::draw()

{
     cout << "Drawing triangle";
}

void triangle::erase()

{
     cout << "Erasing triangle";
}

please help me. asap. thankx in advance

Recommended Answers

All 2 Replies

The posts got messed up somehow so you have some truncated code, two triangle.h files, and no virtual.cpp function. Please repost using code tags

http://www.cplusplus.com/doc/tutorial/polymorphism/

This link has some decent examples of polymorphism and virtual functions. Code from link is pasted below.

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

class CPolygon {
  protected:
    int width, height;
  public:
    void set_values (int a, int b)
      { width=a; height=b; }
    virtual int area ()
      { return (0); }
  };

class CRectangle: public CPolygon {
  public:
    int area ()
      { return (width * height); }
  };

class CTriangle: public CPolygon {
  public:
    int area ()
      { return (width * height / 2); }
  };

int main () {
  CRectangle rect;
  CTriangle trgl;
  CPolygon poly;
  CPolygon * ppoly1 = &rect;
  CPolygon * ppoly2 = &trgl;
  CPolygon * ppoly3 = &poly;
  ppoly1->set_values (4,5);
  ppoly2->set_values (4,5);
  ppoly3->set_values (4,5);
  cout << ppoly1->area() << endl;
  cout << ppoly2->area() << endl;
  cout << ppoly3->area() << endl;cin.get();
  return 0;
}

Int this class :

//This is the header file figure.h. 
using std::cout;

class figure
{
      public:
             virtual void center();
             void draw();
             void erase();
};

void figure::center()
{
     cout << "Centering the Figure";
     cout << "\n";
     cout << "\n";

You should make draw a virtual function because you each object that
derives from it, will have a different draw method. For example,
a rectangle that derives from figure will have a different draw method
than a circle or a sphere.

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.