To see the video where I walk through all of this code with you, please click here for the youtube video.
Introduction: Manageable Goals
The goal of this blog post is to gain an understanding (with example code) of how C++ Classes and Templates work, as well as being able to build our own custom data structure.
Now, I don't want to go into classes and objects, because that conversation confuses most beginners. For now, you don't need to know about those. We just want to be able to create and use a class, then learn about templates, and then use that knowledge to build our own custom data structure.
The following code is also available on github, found here.
A Brief Warm-Up: Hello World and Functions
Let's start with a brief warmup.
Here we have a basic Hello World program, where the code for our output is in the function SayHi. Functions are useful in that we can separate some code away from main into it's own special little bundle that we can call whenever we need it. Often, it saves time as well if we use the function multiple times.
Not too hard, right? Let's move on to the next step.
Classes are similar to functions in that we create a blueprint for what we'd like to have the class do, and define functions that we can use whenever we'd like.
The following code demonstrates the ability to create a class, that holds some functions and variables, and how we can use that class. Read the comments to see how everything works.
The favorite number is 7
The favorite number is 20
The sum of 5 and 5 is: 10.
Same Class Code, But Separate Files
Okay so we can make and use classes in C++, but to be honest, it's a bit unreasonable (and not that helpful) if you just keep everything in one file. That's not how projects actually work. (And it leads to a lot of clutter and often poor documentation.)
We're going to use the exact same class as above, but we're going to separate the class code into a new file.
Our class code is the exact same as before, copied into a new file named "MyMathLibrary.cpp". The only addition to this file is the "#include<iostream>" and "using namespace std;", since the class's code still needs those.
Next, we need a header file that prototypes the class and functions for when we include them in our main file.
This is pretty much the same code as our class blueprint, but notice how we need to include the "using namespace std;" and the function definition because of our poor choices. (Always define your class functions outside of the class. The class itself should be as clean as possible.)
In a file named main.cpp, we have the same "main" function that we did before. The only difference is that we make a reference to the header file with the #include "MyMathLibrary.h" line. This lets us use that file's code in this one.
Don't forget to add MyMathLibrary.cpp to be compiled if you're using a terminal. The commands for this set of code are:
The output is the same as before.
To advance a bit further, we're going to learn about templates in C++. On a basic/student level, you won't really need to know what templates are. But if you want to actually build a real class, then templates are necessary and is the difference between good coding and poor coding.
A template is basically when you want to use the same code for your functions, but have it work regardless of data type. Let me show you an example.
Integer addition of 5 and 5: 10
Double addition of 5.5 and 5.5: 11
Long addition of 5 and 5: 10
Notice how we have an "Add" function, there are three different data types that it could return...
Let's use a template this time.
The output is the same as before.
You can see that inside of needing three separate functions, we just needed one.
Okay so this seems cool and all but...how does this relate to classes? And are templates even that much better? Well...
Our Own Custom Data Structure
Okay so real talk, if you want to be a godly programmer and have everyone use your code...if you want to make a library...if you want to work on real projects...then you need to use classes and templates.
Have you ever used vectors before? Or stacks? Queues? I have another article that goes over how these data structures work, but basically, some pro people made libraries for these data structures, they implemented all the necessary tools (pushing, popping, sorting, etc.) and packaged it up neatly for anyone else to be able to use.
Are classes necessary? Yes, the classes are necessary.
Are templates necessary? Yes, look at the following code.
The class name is "vector", and since we want a vector of "int"s, the vector class initializes everything for integers. As a user of the vector class, we have a very easy time since the class is so well made.
Since our inspiration is being able to make a library that other people can use, let's make a quick coding example that puts together everything we've learned so far by creating our own data structure. (We'll keep it VERY simple.)
We're going to create our own stack library. Let's start with our stack implementation. We put our code in a file named srcstack.t.hpp (because template code can't be in a separate cpp file):
As you can see, we implement a very simple stack class. We have the necessary stack functions, and we include the "template<class T>" line above each of them since they're part of a class that's a generic type.
Our main function simply includes our template .hpp file and demonstrates the use of our new class:
Our code will compile with the standard "g++ main.cpp -o run" command.
The output is:
1 0 0
1 2 3 4 5
We learned how to create classes in C++ (and implement them in separate classes, as is necessary in a professional environment). We also saw how templates work for simple functions, and we extended that to classes to make a generic class that we used to implement our own data structure.
The techniques in this article are fundamental to developing larger projects, and it wasn't that hard, right? Keep practicing and read more blog posts to learn more about programming.
To see this code on github or to download it yourself, please click this link.
To see me walk through all of this code while explaining everything, please watch this video:
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Hi, I'm srcmake. I play video games and develop software.
Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.