-6

I just wonder if all virtual function has got to be a const?

I'm having some issue with them as the area always return 0 for my square when ever i wanna print them out. would appreciate if someone could enlighten me.

shapetwod.h

class ShapeTwoD
{
    protected:
        string name, warpSpace;
        bool containsWarpSpace;
    public:
        //constructor
        ShapeTwoD();
        ShapeTwoD(string, bool);

        //accessors/set function
        void setName(string);

        //mutator/get function
        string getName();



        //methods
        virtual double computeArea();
        virtual void view();
};

shapetwod.cpp

ShapeTwoD::ShapeTwoD()
{
    string name = "";
}

ShapeTwoD::ShapeTwoD(string ShapeName)
{
    name = ShapeName;
}

void ShapeTwoD::setName(string shapeName)
{
    name=shapeName;
}

string ShapeTwoD::getName()
{
    return name;
}

double ShapeTwoD::computeArea()
{
    return 0;
}

void ShapeTwoD::view()
{
    cout << "Area is: " << endl;
}

square.h

class Square:public ShapeTwoD
{
    private:

        int xVal,yVal;
        int length, breath;
        double area;

    public:

        Square();
        Square(string, int, int, double);


        //acessor method
        //int getSquareDetails();
        int getxCord();
        int getyCord();
        double getArea();

        virtual double computeArea();
        void view();

        int xvalue[4];
        int yvalue[4];

};

square.cpp

Square::Square()
{
    xVal = 0;
    yVal = 0;
    area = 0;
}

Square::Square(string ShapeName, bool warpspace, int xval, int yval, double areas):ShapeTwoD(ShapeName, warpspace)
{
    xVal = xval;
    yVal = yval;
    area = areas;
}

void Square::setSquareCord()
{
    for (int i=0; i<4; i++)
    {
        cout << "Please enter x-ordinate of pt " << i+1 << ": "; 
        cin >> xVal;
        xvalue[i] = xVal;

        cout << endl;

        cout << "Please enter y-ordinate of pt " << i+1 << ": ";
        cin >> yVal;
        yvalue[i] = yVal;
        cout << endl;
    }
}

double Square::computeArea()
{
    int xmax = xvalue[1];
    int xmin = xvalue[1];

    int ymax = yvalue[1];
    int ymin = yvalue[1];

    for(int i=0; i<4; i++)
    {
        if(xvalue[i]>xmax)
        {
            xmax = xvalue[i];
        }
        else if(xvalue[i]<xmin)
        {
            xmin = xvalue[i];
        }
    }

    for(int i=0; i<4; i++)
    {
        if(yvalue[i]>ymax)
        {
            ymax = yvalue[i];
        }
        else if(yvalue[i]<ymin)
        {
            ymin = yvalue[i];
        }
    }
    length = xmax - xmin;
    breath = ymax - ymin;

    area = length * breath;
    return (area);
}

int Square::getxCord()
{
    return xVal;
}

int Square::getyCord()
{
    return yVal;
}

double Square::getArea()
{
    return area;
}


void Square::view()
{
    cout << "Name: " << getName() << endl;
    cout << "Area is: " << area << endl;
}

Sorry but I don't really know how to phase my question. i'm actually using Polymorphism & Virtual Functions here and my computeArea and view function are virtual.

so in my square.cpp under the "view" function the programme would always return me 0 and i'm not sure why is this so..

this is how i call the view function. not sure if it's helping here..

void Shape2DLink::InputSensor()
{
    string shape,type;  

    cout<<endl<<"\n"<<"[ Input sensor data ]"<<endl;


    cout << "Please enter name of shape: " << endl;
        cin >> shape;
        shape2D.setName(shape); 

    cout << "Please enter special type : " << endl;
        cin >> type;
        shape2D.setWarpSpace(type);

    if(shape == "Square")
    {
            square.setSquareCord();
            square.computeArea();
            square.isPointOnShape();
            square.isPointInShape();
            Square *mySquare = new Square;
            shapeobject.push_back(mySquare);
            //shapeobject.push_back( new Square );
    }
}
void Shape2DLink::Display()
{
    vector<ShapeTwoD*>::iterator vectorIt = shapeobject.begin();
    while(vectorIt != shapeobject.end())
    {
        (*vectorIt)->view();
        vectorIt++;
    }
}
user2900611
  • 67
  • 2
  • 10

2 Answers2

6

I just wonder if all virtual function has got to be a const?

No.


I'm having some issue with them as the area always return 0 for my square when ever i wanna print them out.

Hmm, ok, let's take a look.

double ShapeTwoD::computeArea()
{
    return 0;
}

Yup, uh-huh.


I've got my Magic Monkey Hat on, and I predict that the problem you're having is caused by Object Slicing. Consider:

void foo (Shape2D shape)
{
  cout << shape.compute_area() << "\n"
}

int main()
{
  Square sq;
  foo (sq);
}

Since foo above takes a Shape2D by-value, all the Squareness of whatever is passed to it is sliced away, leaving only a Shape2D. Of course since Shape2D::compute_area just returns 0, that's what is computed.

To fix this particular problem, don't copy the object or take it by-value, but take it by-reference instead:

void foo (Shape2D& shape)
{
  cout << shape.compute_area() << "\n"
}

Now the object isn't sliced.

Look for similar problems in the code you haven't shown us.

Community
  • 1
  • 1
John Dibling
  • 99,718
  • 31
  • 186
  • 324
1

No a virtual method may or may not be const. Your problem is somewhere else, but not in the code you have included.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176