Derived Classes

You can extend almost any class you want. So once you’ve defined a base class, you just need to know enough about it, and how you want to derive from it. Considering our GenericGeometric class from before, let’s look at deriving a Circle class from it. In our header file we will have: Notice two…

Base vs Derived Classes

One of the cool things about object oriented programming is that you can derive a class from an existing class. this allows you to have most of the class built from an existing class, and not need to rewrite the whole class, only the new parts. Additionally, if the original class, called the base class…

Operator Overloading

C++ allows you to define functions for operators, this is called operator overloading. Now why would you need to do that? Well, consider the Circle class we defined earlier. Let’s say I needed to compare two circles to see which one was larger, normally I’d write something like: The problem is, C++ has know way…

The Vector Class

Using arrays to store a collection of variables, even objects, if very beneficial. However, it does have some limitations given that arrays are of a fixed size, and cannot shrink or grow with use. This means you either run out of space after a set of time, or you waste a lot of space as…

Class Templates

Not only can you use templates for a primitive data type within functions, but also within classes. Consider the following example: The methods are similar to a non-template class, however, each method is a template itself, so you have to adjust a few things. For example, you have to specify template<typename T> before each method….

Copy Constructors

We’ve talked about Constructors in C++ already. One of the special methods, along with Destructors, that C++ offers. There is a third, special method which C++ offers. That is called a Copy Constructor. The Copy Constructor is a special type of constructor, which is intended for use when you duplicate an object. Some people will…