something like
class base
{
public :
virtual void draw() const = 0;
virtual void area() const = 0;
};
class rectangle : public base
{
public :
virtual void draw() const {std::cout<<"rectangel\n";}
virtual void area() const {std::cout<<"rect area\n";}
};
class triangle : public base
{
public :
void draw() const {std::cout<<"triangle\n";}
virtual void area() const {std::cout<<"tri area\n";}
};
void drawShape(base const *basePtr)
{
basePtr->draw();
basePtr->area();
}
int main()
{
rectangle recta;
triangle tri;
base *basePtr[2] = {0};
basePtr[0] = &recta;
basePtr[1] = &tri;
for(size_t i = 0; i != sizeof_array(basePtr); ++i) //the most important part
drawShape(basePtr[i]);
std::cout <<"system pause"<<std::endl;
std::cin.get();
return 0;
}
The most important part is I want to iterate different strategies of draw and
area by for loop one by one.Do I have any way to make template simulate the
behavior of line 37 and 38?
Thanks a lot