0

I'm new in programming and I'm studying inheritance principle, I'm working on an exercise that requires me to create a parent class called "normalClock", the attributes include the time and AM or PM, I need to create a child class that inherits from "normalClock" but this one only works with AM time, only displaying the morning time, is it possible for me to modify the attribute from the parent class.. lets say remove "PM" without breaking the rules on inheritance? I don't want to change anything from the parent class I just want to know if its something that can be done in the child class.

thanks in advance

Carlos
  • 5,991
  • 6
  • 43
  • 82
JeanBook
  • 31
  • 7
  • 1
    Not sure if I understand correctly, butI don't think it's possible. Showing some code help to clarify your question. – Lukas-T Jul 31 '20 at 20:15
  • i dont understand how you can remove PM from a clock. What does the clock do after lunch? – 463035818_is_not_an_ai Jul 31 '20 at 20:18
  • "only works ?" if you described what your child class does in the AM, someone might have an idea of what the action would be for PM times. Example: perhaps in AM, the object outputs the time-of-day at a known place, i.e. it works by updating the screen. On the other hand, In PM maybe the object a) clears the output (something logically different than updating the screen) or b) outputs a msg "PM does not work" or well, use your imagination. It is always possible to implement a method that does nothing. – 2785528 Jul 31 '20 at 20:57

1 Answers1

3

This is not possible (aside from some really messy template/macro hacks) in C++.

Furthermore, the fact that you want to do this in the first place indicates that your design is likely wrong. Your "AM only" clock violates the Liskov Substitution Principle - that is, code that works on normalClocks should also work on morningClocks, but this design would cause the two types to be incompatible.

0x5453
  • 12,753
  • 1
  • 32
  • 61