CPSC 427a: Object-Oriented Programming
Michael J. Fischer
Name Visibility Revisited
Names,
Members,
and
Contexts
Data and function names can be declared in many different contexts in
C++: in a class, globally, in function parameter lists, and in code blocks
(viz. local variables).
Often the same identifier will be declared multiple times in different
contexts.
Two steps to determining the meaning of an occurrence of an identifier:
Declaration and reference contexts Every reference x to a class data or function member has two contexts associated with it:
Accessibility rules apply to class data and function members depend on both the declaration context and the reference context of a reference x.
Declaration context example
Example:
Reference context example
All three commented occurrences of x have declaration context A because all three refer to A::x, the data member declared in class A.
Inside and outside class references
A reference x to a data/function member of class A is
For simple classes:
Examples
References to A::x
Inherited names
In a derived class, names from the base class are inherited by the derived
class, but their privacy settings are altered as described in the last
lecture.
The result is that the same member exists in both classes but with
possibly different privacy settings.
Question: Which privacy setting is used to determine visibility?
Answer: The one of the declaration context of the referent.
Inheritance example
Let bb be an instance of class B. Then bb contains a field x, inherited from class A. This field has two names A::x and B::x.
The names are distinct and may have different privacy attributes. In this example, A::x is protected and B::x is private.
First reference is okay since the declaration context of x is B.
Second reference is not since the declaration context of x is A.
Both occurrences have reference context B.
Inaccessible base class
A base class pointer can only reference an object of a derived class if
doing so would not violate the derived class’s privacy. Recall surprising
example 2 (bottom):
1 class A { };
2 class B : private A {}; // <-- private derivation
3 int main() { A* ap; B* bp;
4 ap = bp; }
The idea is that with private derivation, the fact that B is derived from A
should be completely invisible from the outside.
With protected derivation, it should be completely invisible except to its descendants.
Interacting Classes and UML
What is a Class: Syntax
Can include global operators in such a diagram, adding a 4th row.
Class Relationships
Class Relationship Between Two Classes
Scenarios in which class B appears in definition of class A?
Class B appears in Definition of Class A
Class B is related with class A
UML diagram for derivation:
UML diagram for friend:
B as Data Members in A
Class B objects as data members in class A, e.g.,
These reflect different class relationships:
The association relationship is weaker. A special type of association is called aggregation: when a B object is a “part” of an A object.
B as Data Members in A
UML diagram for composition:
UML diagram for aggregation:
UML diagrams for simple association (left) and one-many association (right):
Creation and Deletion
Identifying composition, association and aggregation helps with object creation and deletion:
Example: BarGraph Class Interaction
What is the class diagram of the BarGraph program?
What types of relationships do we identify? Why?