Re: inheritance, list of objects, polymorphism
"James Kanze" <james.kanze@gmail.com> ha scritto nel messaggio
news:faa92180-c056-4080-87da-df0695a6af39@s20g2000yqd.googlegroups.com...
On Dec 17, 8:35 pm, "io_x" <a...@b.c.invalid> wrote:
"barbaros" <barba...@ptmat.fc.ul.pt> ha scritto nel
messaggionews:f2890a48-ed1d-4451-86e6-12c5fb13ccc5@a21g2000yqc.googlegroups.com...
i not understand much but this seems ok
No it's not. Not from a design point, anyway, and it doesn't begin to
fulfill the requirements.
What he basically needs is something along the lines of:
// Interface...
class Expression
{
Expression( Expression const& );
Expression& operator=( Expression const& );
public:
virtual ~Expression() {}
virtual double value() const = 0;
// other functions...?
};
so what there is in the class "Expression"
there are only functions or there is something, a pointer, a string type, a
double etc at last some data
Error E2251 str0.cpp 24: Cannot find default constructor to initialize base
class 'Express
ion' in function OneOperandExpression::OneOperandExpression(const Expression *)
Error E2034 str0.cpp 36: Cannot convert 'const Expression *' to 'Expression *'
in function
TwoOperandExpression::TwoOperandExpression(const Expression *,const Expression
*)
Error E2034 str0.cpp 37: Cannot convert 'const Expression *' to 'Expression *'
in function
TwoOperandExpression::TwoOperandExpression(const Expression *,const Expression
*)
Error E2251 str0.cpp 38: Cannot find default constructor to initialize base
class 'Express
ion' in function TwoOperandExpression::TwoOperandExpression(const Expression
*,const Expre
ssion *)
Error E2251 str0.cpp 54: Cannot find default constructor to initialize base
class 'Express
ion' in function ConstantExpression::ConstantExpression(double)
Error E2451 str0.cpp 86: Undefined symbol 'lhs' in function
AddExpression::value() const
Error E2451 str0.cpp 86: Undefined symbol 'rhs' in function
AddExpression::value() const
*** 7 errors in Compile ***
this is what seems at last compile
but i'm sure it not like or i don't like it
#include <iostream.h>
#include <list.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
// Interface...
class Expression{
public:
Expression( ){st="";}
Expression( Expression& m){st=m.st;}
Expression& operator=( Expression& m )
{ st=m.st; return m;}
double f(Expression& m){return atof(m.st.c_str());}
double value(){return f(*this);}
~Expression() {}
string st;
};
class OneOperandExpression
{public:
OneOperandExpression(){m_op=0;}
OneOperandExpression(Expression* op){m_op=op;}
Expression* m_op;
};
class TwoOperandExpression{
public:
TwoOperandExpression(Expression* lhs, Expression* rhs )
{ m_lhs = lhs; m_rhs=rhs;}
Expression* m_lhs;
Expression* m_rhs;
};
class ConstantExpression{
public:
ConstantExpression(double value){ m_value=value;}
virtual double value() {return m_value;}
double m_value;
};
class NegExpression : public OneOperandExpression{
public:
NegExpression(){OneOperandExpression();}
NegExpression(Expression* op){m_op=op;}
double value(){return -(m_op->value());}
};
class AddExpression : public TwoOperandExpression{
public:
AddExpression(Expression* lhs, Expression* rhs )
:TwoOperandExpression( lhs, rhs ){;}
double value()
{return m_lhs->value()+m_rhs->value();}
};
int main(void)
{
return 0;
}