views:

76

answers:

3

This is a fragment from "Exceptional C++" Item 24, Solution, first bullet from the bottom of the page:

Never use public inheritance to implement "IS-ALMOST-A." I've seen some programmers, even experienced ones, inherit publicly from a base and implement "most" of the overridden virtual functions in a way that preserved the semantics of the base class. In other words, in some cases using the Derived object as a Base would not behave quite the way that a reasonable Base client could expect. An example often cited by Robert Martin is the usually misguided idea of inheriting a Square class from a Rectangle class "because a square is a rectangle." That may be true in mathematics, but it's not necessarily true in classes. For example, say that the Rectangle class has a virtual SetWidth(int) function. Then Square's implementation to set the width would also naturally set the height so that the object remains square. Yet there may well exist code elsewhere in the system that works polymorphically with Rectangle objects, and would not expect that changing the width would also change the height. After all, that's not true of Rectangles in general! This is a good example of public inheritance that would violate LSP, because the derived class does not deliver the same semantics as the base class. It violates the key precept of public inheritance: "Require no more and promise no less."

I've tried to check it and I wrote:

// Square.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;
class Rectangle
{
private:
 unsigned width_;
 unsigned height_;
public:
 Rectangle(const unsigned width, const unsigned height):width_(width),height_(height)
 {/*Empty body*/ }
 unsigned GetWidth()const
 {
  return width_;
 }
 unsigned GetHeight()const
 {
  return height_;
 }
 virtual void SetWidth(const unsigned width)
 {
  width_ = width;
 }
 void SetHeight(const unsigned height)
 {
  height_ = height;
 }
 virtual ~Rectangle()
 {
  cout << "~Rectangle()" << '\n';
 };
};

class Square : public Rectangle
{
 using Rectangle::SetWidth;
public:
 Square(const unsigned width):Rectangle(width,width)
 {
 }
 void SetWidth(const unsigned width)
 {

  SetWidth(width);
  SetHeight(width);
 }
 ~Square()
 {
  cout << "~Sqare()" << '\n';
 }
};

int _tmain(int argc, _TCHAR* argv[])
{
 Rectangle** a = static_cast<Rectangle**>(operator new (sizeof(Rectangle) * 2));
 a[0] = new Rectangle(10,10);
 a[1] = new Square(5);
 Rectangle* r = a[0];
 cout << r->GetHeight() << "\t" << r->GetWidth() << '\n';
 r = a[1];
 cout << r->GetHeight() << "\t" << r->GetWidth() << '\n';
 r = a[0];
 r->SetWidth(20);//here I'm setting just width for a Rectangle
 cout << r->GetHeight() << "\t" << r->GetWidth() << '\n';
delete a[1];
delete a;
     return 0;
    }

As for me inheriting Square from Rectangle works as intended. So where am I making mistake and do not understand what is said in this bullet?
Thanks

A: 

Your code is fine. What the bullet is suggesting is that someone may write some code that depends on the height of a rectangle remaining unchanged across a SetWidth call:

int old_height = r->GetHeight();
r->SetWidth(100);
assert old_height == r->GetHeight();

This code would fail with your implementation of SetWidth in Square.

Keith Randall
The code is not fine, `Square` violates the contract that `Rectangle` makes.
Philip Potter
The code is fine. Nowhere does he violate any contract that Rectangle makes, as Rectangle makes no contract. The spec of Rectangle::SetWidth needs a guarantee like "does not change height". Of course, we often assume this, but I would contend that without that explicit spec, it is my code that is wrong (as is the other example code in other answers to this question).
Keith Randall
A spec doesn't have to be explicit to be a spec. You can't make everything explicit.
Philip Potter
I suppose not. And to be clear, I wouldn't want to use a Rectangle class that didn't have that guarantee. But the OP's code doesn't rely on any guarantee, he needs code like mine above to create the described inheritance bug.
Keith Randall
create -> trigger
Keith Randall
+6  A: 
sth
Great example. The semantic contract is key.
Drew Hall
A: 

Here is the article on LSP from Robert Martin on this issue.

Chubsdad