<aside> 💡 Recall: aggregation eg. parts in a catalogue, ducks in a pond

IMG_2967.heic

Typical Implementation: Pointer fields

class Pond {
	Duck *ducks[MaxDucks];
	...
}

Case Study: Does a pointer field always mean non-ownership?

<aside> 💡 Inheritance (Specialization)

Suppose you want to track a collection of Books

class Book {
	string title, author;
	int length; // # of pages in a book
public:
	Book(_________);
	...

};

// For textbooks - also a topic:
class Text {
	string title, author;
	int length;
	string topic;
public:
	Text(__________);
	...
};

// For comic books, want the name of the hero:
class Comic {
	string title, author,
	int length;
	string hero;
public:
	Comic(__________);
	...
};

This is OK - but doesn’t capture the relationship among Book, Text, Comic

And how do we create an array (or List) that contains a mixture of these?

(the body for the class Comic)

IMG_2972.heic

</aside>

<aside> 💡 Could

  1. Use a union
union BookTypes{Book *b; Text *t; Comic *c;};
BookTypes myBooks[20];
  1. Array of void * - ptr to anything

Rather, observe: Texts & Comics are kinds of Books - Books with extra features.

To model in C++ - Inheritance

</aside>

// Bases clss (or **Superclass**)
class Book { 
	string title, author;
	int length;
public:
	Book(________);
	...
};

IMG_2973.heic

// Derived classes (or **Subclasses**)
class Text {
	string topic;
public:
	Text(________);
	...
};

class Comic: public Book {
	string hero;
public:
	Comic(________);
	...
};

<aside> 💡 Derived classes inherit fields & methods from the base class

Who can see these members?

How do we initialize Text? Need title, author, length, topic (title, author, length initialize Book part)

class Text:public Book {
	...
public:
	Text(string title, string author, int length, string topic):
			title{title}, author{author}, length{length}, topic{topic}{}
	...
};

Wrong!! for 2 reasons

  1. title, etc., not accessible in Text(and even if they were, the MIL only lets your mention your own fields)
  2. Once ahainm when an object it created:
    1. Space is allocated
    2. Superclass part is constructed New!
    3. Fields constructed
    4. Ctor body runs

So a ctor for Book must run before the fields of Text can be initialized

<aside> 💡 If Book has no default ctor, a ctor for Book must be invoked explicitly:

class Text:public Book {
	...
public:
	Text(string title, string author, int length, string topic):
		Book{title, author, length}, topic{topic}{}
// |_________ Step 2 _________|  |_ Step 3 _||_| <- Step 4
	...
};

</aside>

<aside> 💡 Good reasons to keep superclass fields inaccessible to subclasses:

</aside>

Now consider the method is Heavy - when is a Book heavy?

class Book {
...
protected:
	int length;
public:
	bool isHeavy() const {return length > 200;}
};

class Comic:public Book {
	...
public:
	...
	bool isHeavy() const {return length > 30;}
	...
};
etc.