Содержание
- 2. slide Reading Assignment Mitchell, Chapter 12
- 3. slide History C++ is an object-oriented extension of C Designed by Bjarne Stroustrup at Bell Labs
- 4. slide Design Goals Provide object-oriented features in C-based language, without compromising efficiency Backwards compatibility with C
- 5. slide How Successful? Many users, tremendous popular success Given the design goals and constraints, very well-designed
- 6. slide Significant Constraints C has specific machine model Access to underlying architecture (BCPL legacy) No garbage
- 7. slide Non-Object-Oriented Additions Function templates (generic programming) Pass-by-reference User-defined overloading Boolean type
- 8. slide C++ Object System Classes Objects With dynamic lookup of virtual functions Inheritance Single and multiple
- 9. slide Good Decisions Public, private, protected levels of visibility Public: visible everywhere Protected: within class and
- 10. slide Problem Areas Casts Sometimes no-op, sometimes not (multiple inheritance) Lack of garbage collection Objects allocated
- 11. slide Sample Class: Points class Pt { public: Pt(int xv); Pt(Pt* pv); int getX(); virtual void
- 12. slide Virtual Functions Virtual member functions Accessed by indirection through pointer in object May be redefined
- 13. slide Sample Derived Class: Color Point class ColorPt: public Pt { public: ColorPt(int xv,int cv); ColorPt(Pt*
- 14. slide Run-Time Representation 3 5 blue Point object ColorPoint object x vptr x vptr c Point
- 15. slide Compare to Smalltalk 2 3 x y newX:Y: ... move Point object Point class Template
- 16. slide Why Is C++ Lookup Simpler? Smalltalk has no static type system Code p message:params could
- 17. slide Looking Up Methods (1) 3 5 blue Point object ColorPoint object x vptr x vptr
- 18. slide 3 5 blue darken() Point object ColorPoint object x vptr x vptr c Point vtable
- 19. slide Calls to Virtual Functions One member function may call another class A { public: virtual
- 20. slide “This” Pointer Code is compiled so that member function takes object itself as first argument
- 21. slide Non-Virtual Functions How is code for non-virtual function found? Same way as ordinary functions: Compiler
- 22. slide Scope Rules in C++ Scope qualifiers: binary :: operator, ->, and . class::member, ptr->member, object.member
- 23. slide Virtual vs. Overloaded Functions class parent { public: void printclass() {printf("p ");}; virtual void printvirtual()
- 24. slide Subtyping Subtyping in principle A Example: Point: int getX(); void move(int); ColorPoint: int getX(); int
- 25. slide No Subtyping Without Inheritance class Point { public: int getX(); void move(int); protected: ... private:
- 26. slide Why This Design Choice? Client code depends only on public interface In principle, if ColorPoint
- 27. slide Function Subtyping Subtyping principle A Subtyping for function results If A Covariant: A Subtyping for
- 28. slide Examples If circle C++ compilers recognize limited forms of function subtyping
- 29. slide Subtyping with Functions In principle: can have ColorPoint In practice: some compilers allow, others not
- 30. slide Abstract Classes Abstract class: a class without complete implementation Declared by =0 (what a great
- 31. slide Multiple Inheritance Inherit independent functionality from independent classes
- 32. slide Problem: Name Clashes class A { public: void virtual f() { … } }; class
- 33. slide Solving Name Clashes Three general approaches No solution is always best Implicit resolution Language resolves
- 34. slide Explicit Resolution of Name Clashes Rewrite class C to call A::f explicitly class C :
- 35. slide vtable for Multiple Inheritance class A { public: int x; virtual void f(); }; class
- 36. slide Object Layout Offset δ in vtbl is used in call to pb->f, since C::f may
- 37. slide Multiple Inheritance “Diamond” Is interface or implementation inherited twice? What if definitions conflict?
- 38. slide Diamond Inheritance in C++ Standard base classes D members appear twice in C Virtual base
- 40. Скачать презентацию