B
Banaticus Bart
I wrote an abstract base class from which I've derived a few other
classes. I'd like to create a base class array where each element is
an instance of a derived object. I can create a base class pointer
which points to an instance of a derived class, but when I pass that
base class pointer into a function, it can't access the derived
object's public functions. Although, the base class pointer does call
the appropriate virtual function in the derived class.
Here's a section of code, I've posted comments in my code to more
clearly communicate the problem I'm having.
int main()
{
void printTestObject(BAHshape &);
BAHshape testArray[] = {BAHcircle(2.5), BAHrectangle(3.1, 2),
BAHcube(4, 2, 3)};
for (int i = 0; i < 3; i++)
{
printTestObject(testArray);
}
system("pause");
system("cls");
} //end main function
void printTestObject(BAHshape &testObject)
{
/*
The following three lines display correctly. When I pass in
testArray[0], the program displays "The area of this circle is ..."
the correct calcArea() function is called in each instance -- the
circle object calculates the area from the circle class calcArea()
function, the rectangle calculates the area from the rectangle class
calcArea() function, etc.
*/
std::string objectName = testObject.getName();
std::cout <<"The area of this " <<testObject.getName() <<" is: ";
std::cout <<testObject.getArea() <<std::endl;
/*
The following sction of code (which is commented out) is what I'm
having a problem with. Seeing as how I'm passing in a BAHshape object
to this function, the compiler won't allow the function getRadius().
How can I allow a BAHshape pointer (which points to an instance of a
derived class) to access the public functions in BAHcircle?
*/
/*
if (objectName == "circle")
{
std::cout <<"This circle's radius is: ";
std::cout <<testObject.getRadius();
}
//this second if block could have been an else if, but I felt that
//a seperate if block made for better readability.
if (objectName == "rectangle" || objectName == "cube")
{
std::cout <<"The ";
std::cout <<testObject.getName();
std::cout <<"'s base is ";
std::cout <<testObject.getBase();
std::cout <<" and its height is ";
std::cout <<testObject.getHeight();
if (objectName == "cube")
{
std::cout <<" and its depth is ";
std::cout <<testObject.getDepth();
}
std::cout <<".\n";
}
*/
std::cout <<"The " <<testObject.getName() <<" is located at (";
std::cout <<testObject.getXCoordinate() <<", ";
std::cout <<testObject.getYCoordinate() <<", ";
std::cout <<testObject.getZCoordinate() <<").\n";
std::cout <<std::endl;
} //end printTestObject function
This is all rather frustrating. Because I can't instantiate a
BAHshape object (since I have a pure virtual function in that class),
I can't do the following:
BAHshape testArray[2];
testArray[0] = BAHcircle(2.5);
testArray[1] = BAHrectangle(3.1, 2);
The compiler won't allow me to put any constant integer in the
brackets after the name testArray. It's somewhat weird that I can't
create a base class object, but I can create a base class pointer.
So, any hints on how I can allow the base class pointer (which points
to an instance of a derived class) to access the public functions in
that derived class?
classes. I'd like to create a base class array where each element is
an instance of a derived object. I can create a base class pointer
which points to an instance of a derived class, but when I pass that
base class pointer into a function, it can't access the derived
object's public functions. Although, the base class pointer does call
the appropriate virtual function in the derived class.
Here's a section of code, I've posted comments in my code to more
clearly communicate the problem I'm having.
int main()
{
void printTestObject(BAHshape &);
BAHshape testArray[] = {BAHcircle(2.5), BAHrectangle(3.1, 2),
BAHcube(4, 2, 3)};
for (int i = 0; i < 3; i++)
{
printTestObject(testArray);
}
system("pause");
system("cls");
} //end main function
void printTestObject(BAHshape &testObject)
{
/*
The following three lines display correctly. When I pass in
testArray[0], the program displays "The area of this circle is ..."
the correct calcArea() function is called in each instance -- the
circle object calculates the area from the circle class calcArea()
function, the rectangle calculates the area from the rectangle class
calcArea() function, etc.
*/
std::string objectName = testObject.getName();
std::cout <<"The area of this " <<testObject.getName() <<" is: ";
std::cout <<testObject.getArea() <<std::endl;
/*
The following sction of code (which is commented out) is what I'm
having a problem with. Seeing as how I'm passing in a BAHshape object
to this function, the compiler won't allow the function getRadius().
How can I allow a BAHshape pointer (which points to an instance of a
derived class) to access the public functions in BAHcircle?
*/
/*
if (objectName == "circle")
{
std::cout <<"This circle's radius is: ";
std::cout <<testObject.getRadius();
}
//this second if block could have been an else if, but I felt that
//a seperate if block made for better readability.
if (objectName == "rectangle" || objectName == "cube")
{
std::cout <<"The ";
std::cout <<testObject.getName();
std::cout <<"'s base is ";
std::cout <<testObject.getBase();
std::cout <<" and its height is ";
std::cout <<testObject.getHeight();
if (objectName == "cube")
{
std::cout <<" and its depth is ";
std::cout <<testObject.getDepth();
}
std::cout <<".\n";
}
*/
std::cout <<"The " <<testObject.getName() <<" is located at (";
std::cout <<testObject.getXCoordinate() <<", ";
std::cout <<testObject.getYCoordinate() <<", ";
std::cout <<testObject.getZCoordinate() <<").\n";
std::cout <<std::endl;
} //end printTestObject function
This is all rather frustrating. Because I can't instantiate a
BAHshape object (since I have a pure virtual function in that class),
I can't do the following:
BAHshape testArray[2];
testArray[0] = BAHcircle(2.5);
testArray[1] = BAHrectangle(3.1, 2);
The compiler won't allow me to put any constant integer in the
brackets after the name testArray. It's somewhat weird that I can't
create a base class object, but I can create a base class pointer.
So, any hints on how I can allow the base class pointer (which points
to an instance of a derived class) to access the public functions in
that derived class?