8.15 — Nested types in classes

Consider the following short program:

There’s nothing wrong with this program. But because enum FruitType is meant to be used in conjunction with the Fruit class, it’s a little weird to have it exist independently from the class itself.

Nesting types

Unlike functions, which can’t be nested inside each other, in C++, types can . . . → Read More: 8.15 — Nested types in classes

13.x — Chapter 13 comprehensive quiz

Templates allow us to write functions or classes using placeholder types, so that we can stencil out identical versions of the function or class using different types. A function or class that has been instantiated is called a function or class instance.

All template functions or classes must start with a template parameter declaration that . . . → Read More: 13.x — Chapter 13 comprehensive quiz

13.8 — Partial template specialization for pointers

In previous lesson 13.4 — Template expression parameters, we took a look at a simple templated Storage class:

We showed that this class had problems when template parameter T was of type char* because of the shallow copy/pointer assignment that takes place in the constructor. In that lesson, we used full template specialization to . . . → Read More: 13.8 — Partial template specialization for pointers

13.5 — Function template specialization

When instantiating a function template for a given type, the compiler stencils out a copy of the templated function and replaces the template type parameters with the actual types used in the variable declaration. This means a particular function will have the same implementation details for each instanced type (just using different types). While most . . . → Read More: 13.5 — Function template specialization

12.x — Chapter 12 comprehensive quiz

And so our journey through C++’s inheritance and virtual functions comes to an end. Fret not, dear reader, for there are plenty of other areas of C++ to explore as we move forward.

Chapter summary

C++ allows you to set base class pointers and references to a derived object. This is useful when we want . . . → Read More: 12.x — Chapter 12 comprehensive quiz

12.10 — Printing inherited classes using operator<<

Consider the following program that makes use of a virtual function:

By now, you should be comfortable with the fact that b.print() will call Derived::print() (because b is pointing to a Derived class object, Base::print() is a virtual function, and Derived::print() is an override).

While calling member functions like this to do output is . . . → Read More: 12.10 — Printing inherited classes using operator<<

12.9 — Dynamic casting

Way back in lesson 4.4a — Explicit type conversion (casting), we examined the concept of casting, and the use of static_cast to convert variables from type to another.

In this lesson, we’ll continue by examining another type of cast: dynamic_cast.

The need for dynamic_cast

When dealing with polymorphism, you’ll often encounter cases where you have . . . → Read More: 12.9 — Dynamic casting

12.8 — Object slicing

Let’s go back to an example we looked at previously:

In the above example, ref references and ptr points to derived, which has a Base part, and a Derived part. Because ref and ptr are of type Base, ref and ptr can only see the Base part of derived — the Derived part of . . . → Read More: 12.8 — Object slicing

4.3c — Using statements

If you’re using the standard library a lot, typing “std::” before everything you use from the standard library can become repetitive. C++ provides some alternatives to simplify things, called “using statements”.

The using declaration

One way to simplify things is to utilize a using declaration statement.

Here’s our Hello world program again, with a using . . . → Read More: 4.3c — Using statements

1.8a — Naming conflicts and the std namespace

Let’s say you are driving to a friend’s house for the first time, and the address gaven to you is 245 Front Street in Mill City. Upon reaching Mill City, you pull up your map, only to discover that Mill City actually has two different Front Streets across town from each other! Which one would . . . → Read More: 1.8a — Naming conflicts and the std namespace