JeanBook
JeanBook

Reputation: 31

Inheritance, Can you remove / ignore the attribute of a parent class when creating a child class in C++?

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

Upvotes: 0

Views: 453

Answers (1)

0x5453
0x5453

Reputation: 13589

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.

Upvotes: 3

Related Questions