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++;
}
}