|
-
October 12th, 2009, 04:28 AM
#1
Passing objects to data structures
I have a problem with design that I need some help. I have classes as shown below, my problem is on COperator: oOperation,
explained in the comment. please help me through this. thank you.
Code:
CElement : public CObject
{
}
CNumber : public CElement
{
friend CNumber operator+(const CNumber& x, const CNumber& y);
{
CNumber result;
result.m_nNumber = x.m_nNumber + y.m_nNumber;
return result;
}
public:
CNumber& operator=(const CNumber& x);
{
if(this == &x)
return;
this->m_nNumber = x.m_nNumber;
return *this;
}
private:
int m_nNumber;
}
COperator : public CElement
{
public:
enum OP_TYPE {ET_ADD=0,ET_SUB,ET_MULTIPLY,ET_DIVIDE};
CElement* doOperation(CElement* op1,CElement* op2)
{
//here is the problem, when the result type is of CElement*,
//then returning this object would be not correct,
//becoz im expection a pointer to CNumber,
//or soon i'm adding another class called CFraction, so how do i do this?
//should I get the type of op1 and op2 then create a new object of that Type?
//if i do that, then how about if i'm doing the operation on CFraction class??
CElement* result;
switch(m_operator)
{
case ET_ADD:
result = op1 + op2;
case ET_SUB:
result = op1 - op2;
case ET_MULTIPLY:
result = op1 * op2;
case ET_DIVIDE:
result = op1 / op2;
};
}
private:
OP_TYPE m_operator;
}
CMathExpression : public CArray<CElement*,CElement*>
{
//this class would construct the math expression
//example,
this->AddTail(new CNumber(10));
this->AddTail(new COperator(ET_ADD));
this->AddTail(new CNumber(10));
}
CAlgorithm
{
public:
int evaluatePostfix()
{
CList<CElement*,CElement*> objTemp;
//convert "m_objExpr" to postfix and store in "objTemp"
//now evaluate the postfix expr and get the result
Stack s;// stack is a derived CList implimentation
POSITION pos = objTemp.GetHeadPosition();
while(pos)
{
switch(objTemp.GetType())//getType function is virtual common to all derived class from CElement
{
case ET_ELEMET:
case ET_NUMBER:
case ET_FRACTION:
s.push(objTemp.GetAt(i))
break;
case ET_OPERATOR:
CElement* operand2 = s.getTop();
s.pop();
CElement* operand1 = s.getTop();
s.pop();
CElement* result = ((COperaotr*)objTemp.GetAt(i))->doOperation(operand1,operand2);
s.push(result);
break;
};
}
//get and display the result which is at the top of the Stack
s.getTop()->toString(); //toString function is virtual common to all derived class from CElement
}
private:
CMathExpression m_objExpr; //this is an array of elements in "infix"
}
somewhereinafarfarawayclass::calculate()
{
CAlgorithm algo;
//... here all work done creating the expression
int nResult = algo. evaluatePostfix();
}
Last edited by Max Payne; October 12th, 2009 at 04:37 AM.
Come Join This Poll Where are we from? (Ultimate)
Knowing is not enough; we must apply. Willing is not enough; we must do. - Johann Wolfgang
An idle brain is the devil's workshop. - unknown
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|