base class= Shape
derived class from class Shape= TwoDimensionalShape, ThreeDimensionalShape
derived from TwoDimensionalShape=Square, Rectangle
from Three= Cube, Cuboid
Here is my code
shape.h
#ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double getArea()=0; virtual void print()=0; private: }; #endif
TwoDimensionalShape has no member function added
#ifndef TWODIMENSIONALSHAPE_H #define TWODIMENSIONALSHAPE_H #include "Shape.h" class TwoDimensionalShape: public Shape { public: private: }; #endif
ThreeDimensionalShape only has an extra member function, getVolume()
#ifndef THREEDIMENSIONALSHAPE_H #define THREEDIMENSIONALSHAPE_H #include "Shape.h" class ThreeDimensionalShape :public Shape { public: virtual double getVolume()=0; private: }; #endif
test program
#include "Square.h" #include "Rectangle.h" #include "Cube.h" #include "Cuboid.h" #include "Shape.h" #include <cstring> #include <vector> #include <typeinfo> #include <iostream> using namespace std; int main() { vector <Shape *> shapePtr (4); shapePtr[0]=new Square(5); shapePtr[1]=new Rectangle(5,4); shapePtr[2]=new Cube(5); shapePtr[3]=new Cuboid(1,2,3); for (size_t i=0; i<shapePtr.size(); i++) { if (strcmp(typeid(*shapePtr[i]).name(),"class TwoDimensionalObject")==0) { cout<<typeid(*shapePtr[i].name())<<endl; cout<<shapePtr[i]->getArea()<<endl; } else if (strcmp(typeid(*shapePtr[i]).name(),"class ThreeDimensionalObject")==0) { cout<<typeid(*shapePtr[i].name())<<endl; cout<<shapePtr[i]->getVolume()<<endl; } } return 0; }
How am I supposed to determine if lets say square is derived from TwoDimensionalObject??
and also, since I didn't provide the member function getVolume() for class Shape , this code won't work
cout<<shapePtr[i]->getVolume()<<endl;
Ugh darn, why can't I use the code tag?? I just copy pasted my whole code in it, but all the newlines are gone!
This post has been edited by NickDMax: 25 November 2009 - 07:54 AM

New Topic/Question
Reply



MultiQuote





|