Synopsis:
This lecture provides an introduction to structuring designs with C++ classes and their relationships.
We will cover basic ideas in a presentation and then discuss code examples.
Readings and Activities for the Week:
- CPPPL: Chapters 16 - 21, focusing on Chapter 21.
- Review requirements for your second Project assignment: Projects > Project #2
- Look carefully at Compound Objects code.
Glossary of Terms
-
Class:
A language construct that binds together member functions and data:- Class syntax: class X { ... }
- Method syntax: R X::f(T t) { ... }
- Data syntax: Y y;
-
Class Relationships:
Object Oriented Designs use C++ classes and their relationships to structure an implementation. The relationships are:-
Inheritance:
Derived classes specialize base classes, using the syntax:class D : public B { ... };
-
Composition:
Composing classes hold instances of other types as data members, using the syntax:class C { ... private: A a; B b; };
-
Aggregation:
Aggregating classes hold pointers to instances of data on the native heap, using the syntax:class A { ... private: B* pB = nullptr; C* pC = nullptr; };
The pointers are initialized to point to heap allocations in some member function of the class, perhaps in it's constructors and the allocations are returned in its destructor. -
Using:
Using classes are passed class instances they will use through member function calls. They do not own these instances, simply use them temporarily, with the syntax:class U { void usingMF(R* pR) { pr->someUsefulFunction(); } };
-
Inheritance:
Lecture Content:
-
Projects:
-
Sample Project #1,
Code folder
Lexical scanner -
Sample Project #2.
Code folder
Rule-based parser -
Sample Project #3,
Code folder
Parser with Abstract Syntax Tree -
Sample Project #4,
Code folder
Code Analyzer
-
Sample Project #1,
Code folder
-
Class Hierarchy
-
class relationships,
docx,
class relationships - layout view,
vsdx,
ClassRelationships code folder
Illustrates static (class) and physical (memory) structure. -
Hierarchy Presentation,
pptx
Hierarchy is concerned with the relationships between classes - aggregation, composition, inheritance, and using. -
Principles - esp. Liskov Substitution
Discussion of principles that guide good Object Oriented Design
-
Virtual Function Pointer Tables,
vsdx
Virtual Function Pointer Tables -
Demonstrates Compound Object creation
You need to understand how constructor initialization is used to control creation of the objects you use. -
Three Tier Hierarchy
Interface defines a contract, abstract base class provides sharing, derived classes implement the contract in specialized ways. -
Demonstrate Composition
This project contains several demonstrations of basic composition relationships. -
Demonstrate Inheritance
Contains demonstration of things to be careful with when using inheritance. -
C++'s Dark Corners,
docx
A summary of the short list, with brief explainations, of C++'s dark corners, e.g., things that can cause subtle errors.
-
class relationships,
docx,
class relationships - layout view,
vsdx,
ClassRelationships code folder
-
Preparation for Synchronous Discussion:
- Create a class diagram for your Project #2 implementation. Please indicate all of the class relationships.
- Be prepared to discuss your packages and dependencies.
-
Lecture Take-aways:
The most important things we discussed today: