AlexLecture_VisitorPattern - Generalization Visitor Pattern...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Visitor Pattern Generalization Employee first_name : string last_name : string hire_date : Date department : short * A.k.a. the “is-a” relation Relates class to one that is “more general” Open arrow points to base class (i.e., the generalization) Derived class inherits all tt ib t / ti f b l 2 Manager level : short group 0..1 attributes/operations of base class Relation is anti-symmetric and transitive Implementation of Generalization Must use PUBLIC inheritance. Definition of “is-a” relationship: B “is a” A if and only if instances of B can be used in contexts that expect instances of A. Example: A manager is an employee. A student is a human. Formally defined in in the classic paper by Liskov and Wing: Barbara Liskov Jeannette Wing A behavioral notion of subtyping ACM 3 Liskov, Jeannette Wing, A behavioral notion of subtyping, ACM Transactions on Programming Languages and Systems (TOPLAS), Volume 16, Issue 6 (November 1994), pp. 1811 - 1841. Why cannot be private/protected inheritance? Alex X. Liu “is-a” cannot via private/protected Will this compile? #include <iostream> using namespace std; class A { private: int foo; public: A(int f) : foo(f) {}; void print() { cout << foo << endl; } }; Avoid private/protected inheritance Defining class B by privately inheriting from class A is almost always equivalent to giving B a private data member of A. Same to protected inheritance. #include <iostream> using namespace std; class B { public: 4 class B : private A { public: B(int f) : A(f) {}; void p2() { A::print(); } }; int main(void){ A a(3); B b(4); a.print(); b.print(); b.p2(); } Alex X. Liu public: B(int f) : a(f) {}; void p2() { a.print(); } private: A a; }; Exercise Question 1: Does an abstract class need to have a constructor? Answer: Yes, you still need to initialize data members for derived classes.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/14/2009 for the course CSE 335 taught by Professor Kraemer during the Fall '08 term at Michigan State University.

Page1 / 3

AlexLecture_VisitorPattern - Generalization Visitor Pattern...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online