inheritance

T

Tony Johansson

Hello!!

I'm reading a book about C++ and there is something that I don't understand
so I ask you.

The book says
"The set of all public features of a class is called a public inteface, or
simply interface. When the inheritance hierarchy is
properly designed, the derived class interface conforms to the base class
interface. This leads to the substitutability principle:
Whenever the base class object is expected, you can use an object of a
derived class. For example, if an object of class BankAccount is expected,
you can instead use an object of the class CheckingAccount or of the class
SavingAccount.
You may violate the substitutability principle if you override a
nonpolymorphic operation"

Question 1.
What does they mean with this last line. "You may violate the
substitutability principle if you override a nonpolymorphic operation."

Question 2.What does they mean with "When the inheritance hierarchy is
properly designed, the derived class interface conforms to the base class
interface."

//Tony
 
A

Alf P. Steinbach

* Tony Johansson:
Hello!!

I'm reading a book about C++ and there is something that I don't understand
so I ask you.

The book says
"The set of all public features of a class is called a public inteface, or
simply interface. When the inheritance hierarchy is
properly designed, the derived class interface conforms to the base class
interface. This leads to the substitutability principle:
Whenever the base class object is expected, you can use an object of a
derived class. For example, if an object of class BankAccount is expected,
you can instead use an object of the class CheckingAccount or of the class
SavingAccount.
You may violate the substitutability principle if you override a
nonpolymorphic operation"

Question 1.
What does they mean with this last line. "You may violate the
substitutability principle if you override a nonpolymorphic operation."

Assuming a "nonpolymorphic operation" is a non-virtual function: you cannot
override a non-virtual function.

Question 2.What does they mean with "When the inheritance hierarchy is
properly designed, the derived class interface conforms to the base class
interface."

Read up on the Liskov Substitution Principle.
 
P

Pete Becker

Tony said:
Question 1.
What does they mean with this last line. "You may violate the
substitutability principle if you override a nonpolymorphic operation."

It again means that the writing is sloppy. My guess is that it's
supposed to be saying that if the correct behavior of the derived class
requires replacing (not overriding) a non-virtual base class function
then the derived class cannot be used where the base type is expected.
Which, if you think about it, ought to be fairly clear: calling the base
version of that function won't do what the derived type needs.
Question 2.What does they mean with "When the inheritance hierarchy is
properly designed, the derived class interface conforms to the base class
interface."

Fuzzy thinking. "conforms to" doesn't mean anything here.

I'll renew my earlier suggestion: get a different book.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top