0

In the university i've the task to design and implement three classes in c++: Integer (as an abstract), Binary and Decimal, with an array as a digit storage AND Integer should contain virtual methods of arithmetic operations.

So i've got some misunderstandings about the last point...what's signature should the methods have and how to define them in child classes?

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
crew4ok
  • 195
  • 1
  • 3
  • 10

2 Answers2

0

This is not an easy task, being arithmentic operation binary operatons and being virtual methods a single dispatching mechanism.

In fact the elegant solution to the problem should require Multimethods, not native in C++.

Just as an hint, let me give you an idea about addition: Give a look to C++ dual dispatch and to the visitor pattern.

Community
  • 1
  • 1
Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63
  • So i guess the task in such a wording is rather incorrect...right? – crew4ok Feb 03 '12 at 20:51
  • @crew4ok: not necessarily. The idiom I told use virtual methods... but the solution is not just a `virtual Integer operator+(const Integer&) const`since adding Dec to Dec is different then add Bool to Bool, than Dec to Bool than Bool to Dec... – Emilio Garavaglia Feb 03 '12 at 20:58
  • so then it's my question =). How to implement operation, that will be able to differentiate what Integer reference references to? I have an idea about down-casting with dynamic_cast but i'm not sure it's correct – crew4ok Feb 03 '12 at 21:08
  • @crew4ok: yes, that's another way to to that. It's essentially equivalent: every class must be aware of all the others. – Emilio Garavaglia Feb 05 '12 at 07:53
-1
class Integer {
  public:
    virtual void Add(Integer &B) { } = 0; // Adds B to the current value, etc..

}
class Decimal : public Integer {
  public:
    void Add(Decimal &B) { /* do sth */ }
}

int main(void) {
  Integer *a = new Decimal();
  Integer *b = new Decimal();
  a->Add(b);
}
qdot
  • 6,195
  • 5
  • 44
  • 95
  • I suppose that void Integer::Add(Integer &B) and void Decimal::Add(Decimal &B) have different signature, so it's different methods at all And what's about operators that should return reference to a result? – crew4ok Feb 03 '12 at 19:19
  • the homework is about the implementation, the question is what the assignment meant by 'function signatures' in this particular case – qdot Feb 03 '12 at 19:20
  • well, you can return reference to the derivative class in the derivative class, and have the compiler deal with casting it into base class. – qdot Feb 03 '12 at 19:21
  • 1
    I mean if u declare the virtual method with void Add(Integer& B) signature, u should reimplement it with the same one. – crew4ok Feb 03 '12 at 19:24
  • @crew4ok Yes, prototype of overloaded function will be the same. And it is quite reasoned, because you should be able to add decimal integer to binary. This is what inheritance is designed for. If you want different behavior (e.g. decimal can deal with only decimal and binary only with binary) you should not use common parent for these classes. – Mikhail Feb 03 '12 at 20:34
  • @Mikhail, yes, that's what i'm talking about. But i can't get how to implement this method in such a way...i mean, Binary be able to add decimal to itself. Should it be some RTTI technics? – crew4ok Feb 03 '12 at 20:48
  • You can simply use `dynamic_cast` to find out actual type of pointer or reference. However, since you are using inheritance, it is better to define some protected methods like GetDigit(int) and GetRadix() in `Integer` and implement common solutions for arbitrary input radix in `Decimal` and `Binary`. I think, that's what you are expected to do in this homework. At least, I would give this task for my students :) – Mikhail Feb 03 '12 at 21:59
  • @Mikhail, i think the best choice - is to implement theese methods in every child class separately...isn't it? – crew4ok Feb 03 '12 at 22:11
  • 1
    What methods? `GetDigit()` and `GetRadix()` - yes, and they must be declared pure virtual in `Integer`. `Add` can be implemented in `Integer` since it will use child implementations of virtual `GetDigit()` and `GetRadix()`. – Mikhail Feb 03 '12 at 22:59
  • @Mikhail, hmm, yes...i guess i'll this way =). – crew4ok Feb 03 '12 at 23:08