C++ Constructors

Let’s look a little closer at constructors than we did in our initial Defining Classes for Objects example. Anytime you create an object, a constructor is called. If you overload your constructor, C++ will pick the right constructor to call, just like it does when you overload a function. Constructors must have the same name…

Creating an Object

Once you create your class, you need to create an object. The Class is just a blueprint for the program to follow. It doesn’t do anything until you create an object based off of that blueprint. Think of a house. If you have a house blueprint, all you have are some sheets of paper or…

Inline Functions in C++

Calling a function has a performance penalty because you have to create variables, specify scope, pause the current variables and push data onto the call stack, just to name a few reasons. C++ allows you to define an inline function, which should be short, to improve the performance. In doing so, the compiler will actually…

Default Arguments

C++ has added the ability to have default arguments be defined when you declare your functions. To define a default value, you must specify the parameter name, and then assign it a value. If someone calls this function without passing an argument to the function, the computer will use 20 as the parameter. If they…

Function Prototypes

In previous examples, we’ve had to write our functions above the place where they are called. However, many developers like to place main as their first function they see, so it’s easier to find. Or they may not know the order that the function will be called. C and C++ allow you to define a…