Hi,

I am in process of creating a Money class, as per requirement I am suppose to represent each denomination by a class(eg: $5Class, $10Class etc.). I want to use inheritance in this scenario. I cannot think of the class design. I was able to create a class design for a denomination though as below. Can someone please help me creating a inherited structure of the same. below is my work so far:

Class FiveDoll{
  private:
     int val_five;
  public:
     FiveDoll(int v_fivedoll){ val_five = v_fivedoll; }
     int GetFive() { return val_five; }
     friend FiveDoll operator+(const FiveDoll &f1, const FiveDoll &f1);
};

FiveDoll operator+(const FiveDoll &f1, const FiveDoll &f1){
   return FiveDoll(f1.val_five + f2.f1.val_five);
}

How can I create a hierarchy of currencies?

>>represent each denomination by a class(eg: $5Class, $10Class etc.).
Here is your answer right here... If you want to create an inheritance hierarchy, a good place to start is your description of what an object is.

A five-dollar bill is a monetary denomination representing a value of $5.

An example commonly used is dog vs cat. Dogs and cats are both mammals, mammals are animals.

class Animal {
 public:
   virtual void Speak() = 0;
};

class Mammal : public Animal {
};

class Dog : public Mammal {
 public:
   void Speak() { std::cout << "Woof! Woof!"; }
};

class Cat : public Mammal {
 public:
   void Speak() { std::cout << "Meow! Meow!"; }
};

class Human : public Mammal {
 public:
   void Speak() { std::cout << "Hello World!"; }
};

But using tools when appropriate is an important skill to learn. Sometimes it's not appropriate to create classes or use inheritance if the problem can be better solved using other mechanisms, and to me, trying to force inheritance on a system that isn't suited to it doesn't help you learn how to use inheritance. Since the only difference between different bills is the description (5 vs 10 dollars, red vs white, picture of president A or president B on the paper, coin vs bill) and not the function or underlying representation, then I would just stick with a single money class and not try to force inheritance in the organization. If you must come up with a program that uses inheritance as part of a class, then I would develop a different system.

class Money
{
protected:
int value (5, 10, 20)
virtual void Spend() = 0;
[other methods...]
}

now you have a simple interface Money class that you can create the other money classes off of, although in this case, since they're about all the same and the only thing changing is the value, I'd recommend making one class to encompass all this and have the constructor simply assign the value attribute

But using tools when appropriate is an important skill to learn. Sometimes it's not appropriate to create classes or use inheritance if the problem can be better solved using other mechanisms, and to me, trying to force inheritance on a system that isn't suited to it doesn't help you learn how to use inheritance. Since the only difference between different bills is the description (5 vs 10 dollars, red vs white, picture of president A or president B on the paper, coin vs bill) and not the function or underlying representation, then I would just stick with a single money class and not try to force inheritance in the organization. If you must come up with a program that uses inheritance as part of a class, then I would develop a different system.

I would tend to agree, Currency/Money is not really a good candidate for an inheritance hierarchy. However, value is a unique-enough quality though that it's possible to do it in a logical fashion, but it's definitely not an ideal usage. If that's the requirement though...

class Money
{
protected:
 int value (5, 10, 20)
 virtual void Spend() = 0;
 [other methods...]
}

now you have a simple interface Money class that you can create the other money classes off of, although in this case, since they're about all the same and the only thing changing is the value, I'd recommend making one class to encompass all this and have the constructor simply assign the value attribute

I almost think a single class with a nested enumeration would be a better option:

class Money {
 protected:
   enum Denominations {Penny = 1, Nickel = 5, ... ,  Five_Dollar = 500, ... } value;

 //other members

};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.