0

I want to override operator+ for my classes Fridge and TV (to make it add power consumption), so I made them extend Rtv class which should override that operator, however I don't know how to get proper POWER_CONSUMPTION field value if both Fridge and TV have different overriden value. To be more precise i want to achieve this to be possible:

  Fridge fridge = new Fridge();
  Tv tv = new Tv();
  int totalPowerConsumptionOfFridgeAndTv = fridge + tv;

My idea for Rtv class was:

  class Rtv
{
    private const int pOWER_CONSUMPTION= 0;

    public static int POWER_CONSUMPTION=> pOWER_CONSUMPTION;

    public static int operator+ (Rtv a, Rtv b)
    { 
        return a.GetType().POWER_CONSUMPTION + b.POWER_CONSUMPTION;
    }
}

But simply doesn't work.

Tom Sevet
  • 25
  • 6
  • Side note: while duplicate shows how to achieve what you are asking the example in the post is very strange - in no way I would expect TV+Fridge to work, and even if it would I'd expect somehting like "FurnitureList" as result and not a numeric value. You may want to reconsider your design if that sample actually reflects what you are doing. One also unlikely to expect PowerConsumption to be static property of a class based on real world data... probably virtual in base class - "ElectronicEquipment" – Alexei Levenkov Jan 08 '21 at 21:02
  • @AlexeiLevenkov this is my homework at university and have to work like I asked :( so PowerConsumption have to be static and it have to work like this... I know it's pointless and wrong – Tom Sevet Jan 08 '21 at 21:07
  • You should ask you teacher / TA what this assignment is about - it does not look like an answer to any regular assignment I can think of. – Alexei Levenkov Jan 08 '21 at 21:11
  • @AlexeiLevenkov He's a little bit crazy... not even one person from my year (about 60 ppl) understand what he mean and he's not replying to our e-mails... my last homework was to create 10 question questionnaire that says what mood are you in and show picture of that mood in WindowsForms, however my problem stays unsolved becouse `a.GetType().GetProperty(POWER_CONSUMPTION);` doesnt work for me, or again i made it wrong – Tom Sevet Jan 08 '21 at 21:28
  • 2
    Add a non static property to read the static one. Then the problem becomes quite simple. – mjwills Jan 08 '21 at 22:33

0 Answers0