
Grammar Issues for CSE687 Project #21
In Project #2 – Hierarchy, you are required to discover the class
relationships in and among classes in a set of C++ source code files.
Specifically, you must find these relationships:
1. Inheritance:
A class may inherit from another class, a struct, or an interface,
using the syntax:
class myClass : [public | protected | private] yourClass
{ … }
which says that myClass inherits from (is derived from)
yourClass.
2. Composition:
One class or struct, say A, is composed with another, say B,
when B holds an instance or a reference to A as a data member,
and the lifetime of A is the same as the lifetime of B. That
happens only when A is initialized or constructed in the
constructor of B.
class B // case #1 – A is initialized in B’s constructor
{
private:
A myA;
public:
B(args) [: myA(a args)] { … }
// will use default initialization if option is not
present
}
class B // case #2 – A is constructed in B’s constructor call
{
private:
A[* | &] myA;
public:
B(args) { myA = [ |*]new A(a args); }
~B() { delete [ |&] myA; } // [option]
:
1 The class text (C++ Programming Language, Stroustrup) has quite complete descriptions of the language
grammar spread over many chapters. A very nice condensation with syntax diagrams can be found in:
C++ Complete : A Reference and Tutorial to the Proposed C++ Standard, Anthony Rudd, Wiley, 1994.
Note that you need much, much, much less than a complete specification for the language grammar, as
described in this note.