Docsity

C++ Class Relationships: Inheritance, Composition, Aggregation, and Uses - Prof. James Faw, Study Guides, Projects, Research of Engineering

Toggle sidebar
bg1
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.
pf3
pf4
pf5

Partial preview of the text

Download C++ Class Relationships: Inheritance, Composition, Aggregation, and Uses - Prof. James Faw and more Study Guides, Projects, Research Engineering in PDF only on Docsity!

Grammar Issues for CSE687 Project

1 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.
  1. Note about declarations: In items #2 and #3 you have to be able to detect declarations of an instance of some type. That is, you want to know that a declaration occurred and what type was declared. That can be messy since declarations can use modifiers like const, static, volatile, … , the prefix operator * to indicate a pointer, and the postfix operators [] and = used for defining an initialization sequence. You can simplify that by throwing away all modifiers (look up the C++ keywords and decide which may be declaration modifiers), the * prefix if it occurs, and the postfix [] and the postfix = and everything that follows it. You will then be left with two tokens, a Type and its instance identifier.