Dear all, I am trying to understand object oriented programming and I can't find the solution to this problem: I have an abstract class "Container" which is an interface to the main file. Inside "Container", I have a class called "Shape" which should create either a "Circle" or a "Square" and call the member function "DrawShape" (either in "Circle" or "Square"). This is the (not working) code I've written:
///////////////////////////////////////////////////////////////////////////
// main.cpp
//
#include <memory>
#include <iostream>
#include <string>
#define EXPORT
class EXPORT Container
{
protected:
// constructors...should be protected to avoid direct instantiation
Container(){};
public:
~Container(){};
static Container* createContainer(std::string);
virtual void DrawShape(void) = 0;
virtual void SetShape(std::string) = 0;
virtual void CreateShape(void) = 0;
};
class Shape : public Container
{
public:
Shape(){};
public:
virtual ~Shape(){};
virtual void DrawShape();
void SetShape(std::string Type);
void CreateShape();
std::string ShapeType;
};
class Circle : public Shape
{
public:
Circle(){};
public:
virtual ~Circle(){};
void DrawShape();
};
class Square : public Shape
{
public:
Square(){};
public:
virtual ~Square(){};
void DrawShape();
};
Container* Container::createContainer(std::string Type)
{
return new Shape;
}
void Circle:rawShape()
{
std::cout<<"Indide "<<ShapeType;
}
void Square:rawShape()
{
std::cout<<"Indide "<<ShapeType;
}
void Shape:rawShape()
{
std::cout<<"Still inside";
}
void Shape::CreateShape()
{
if (ShapeType == "Circle")
{
new Circle;
}
else
{
new Square;
}
}
void Shape::SetShape(std::string Type)
{
std::cout<<"I'm inside";
ShapeType = Type;
}
int main(int argc, char* argv[])
{
std::auto_ptr<Container> MyContainer(Container::createContainer("aaa"));
MyContainer->SetShape("Circle");
MyContainer->CreateShape();
MyContainer->DrawShape();
return 0;
}
//////////////////////////////////////////////////////////////
Of course, this is not doing what I'd like it to do, since the DrawShape() which is executed is the one member of "Shape". Can anybody help me, please?
Thanks,
M
///////////////////////////////////////////////////////////////////////////
// main.cpp
//
#include <memory>
#include <iostream>
#include <string>
#define EXPORT
class EXPORT Container
{
protected:
// constructors...should be protected to avoid direct instantiation
Container(){};
public:
~Container(){};
static Container* createContainer(std::string);
virtual void DrawShape(void) = 0;
virtual void SetShape(std::string) = 0;
virtual void CreateShape(void) = 0;
};
class Shape : public Container
{
public:
Shape(){};
public:
virtual ~Shape(){};
virtual void DrawShape();
void SetShape(std::string Type);
void CreateShape();
std::string ShapeType;
};
class Circle : public Shape
{
public:
Circle(){};
public:
virtual ~Circle(){};
void DrawShape();
};
class Square : public Shape
{
public:
Square(){};
public:
virtual ~Square(){};
void DrawShape();
};
Container* Container::createContainer(std::string Type)
{
return new Shape;
}
void Circle:rawShape()
{
std::cout<<"Indide "<<ShapeType;
}
void Square:rawShape()
{
std::cout<<"Indide "<<ShapeType;
}
void Shape:rawShape()
{
std::cout<<"Still inside";
}
void Shape::CreateShape()
{
if (ShapeType == "Circle")
{
new Circle;
}
else
{
new Square;
}
}
void Shape::SetShape(std::string Type)
{
std::cout<<"I'm inside";
ShapeType = Type;
}
int main(int argc, char* argv[])
{
std::auto_ptr<Container> MyContainer(Container::createContainer("aaa"));
MyContainer->SetShape("Circle");
MyContainer->CreateShape();
MyContainer->DrawShape();
return 0;
}
//////////////////////////////////////////////////////////////
Of course, this is not doing what I'd like it to do, since the DrawShape() which is executed is the one member of "Shape". Can anybody help me, please?
Thanks,
M