Problem with Classes

Joined
Oct 23, 2008
Messages
3
Reaction score
0
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::DrawShape()
{
std::cout<<"Indide "<<ShapeType;
}

void Square::DrawShape()
{
std::cout<<"Indide "<<ShapeType;
}


void Shape::DrawShape()
{
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top