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

Dani AI

Generated

Short answer: put the virtual keyword on the base-class declaration (the interface you call through a figure* or figure&), keep implementations in one .cpp (or mark them explicitly inline), and give the base a virtual destructor so derived objects clean up correctly.

A compact checklist for based on the posted files and the replies from and :

  • Declare virtuals in the base class header for any method you intend to call polymorphically (for example, draw and erase). If the base is meant only as an interface, consider making them pure virtual (= 0).
  • Add a virtual destructor in the base (virtual ~figure() {}) so delete base_ptr; on a derived object calls the right destructor.
  • Use override in derived-class declarations (C++11+) to catch signature mismatches at compile time.
  • Move out-of-class function definitions into one .cpp file to avoid multiple-definition link errors. If you must put definitions in headers, make them inline or define them inside the class body.
  • Fix obvious problems in the pasted files: remove duplicate headers, correct typos like l void draw();, and avoid using directives in headers (using std::cout;) — headers should not pollute namespaces.
  • Add include guards or #pragma once to every header.

Common errors to watch for and their cause: "multiple definition" = function implemented in a header included in multiple translation units; "undefined reference" = declared but not defined; "no matching function" = signature mismatch (use override to detect this).

For a concise reference on virtual functions and destructors see cppreference: virtual functions.

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.