Dictionary of Terms Project


Templates, the STL, and Other Skills
Course Overview
PREVIEW22m 58s

The course is part of this learning path

Start course
2h 29m

In this course, we will build on your existing foundational and object-oriented oriented skills and enhance them by looking at templates, the Standard Template Library, and other skills to help you in your builds.

Learning Objectives

  • Learn about function templates and class templates
  • Learn how to write efficient and excellent code with the data structures and algorithms in the standard template library 
  • Learn about smart pointers to manage dynamic memory automatically 
  • Understand friend functions, friend classes, and operator overloading

Intended Audience

  • Beginner coders, new to C++
  • Developers looking to upskill by adding C++ to their CV
  • College students and anyone studying C++


To get the most out of this course, you should have a basic understanding of the fundamentals of C++.


Throughout this section, we've discussed a variety of topics to build up and help around our knowledge. I'd like you to create a project in Visual Studio named DictionaryProject. You should create a class named Dictionary which uses an STL map as it's underlying data structure. The design should be fairly straightforward. You will implement an addDefinition method that adds a new word with its definition or updates the definition of a word that already exists. A getDefinition method that returns the definition of a given word and printAll which prints all words and their corresponding definitions. You can assume all words will be lower case. Set up a simple user interface in main through the console so that the user can choose to add or update a word with definition. Get the definition of a word or it will return "not found" if the word is not found or print all the definitions or even exit the program. For example, you might choose to use one as an entry for add update, two for getting a definition, three for printing all and zero for exiting or something like that. You will only need one instance of the dictionary and allow the user to interact with it how they see fit. As a hint, you can use the find method of the map class. If it returns map.end(), then the element wasn't found. So, that's a big hint. If it returns anything besides map.end(), then it was found. It will return a valid iteration within the collection.

Also remember that maps have pairs inside of them and those pairs have a first and secnd public data member for the key and the value. All right. Let's take a look at what the final DictionaryProject will look like when you interact with it  without seeing the code. Debug, start without debugging and this is how I set up the interface. Yours could be different. So, add a new word. We can select 1 and then we enter the word, let's say cat, and then say, an animal that goes meow. And then, maybe we want to add another word, dog and animal that goes woof and we can print out all the definitions with 3. So, that will show us both the definitions. I put quite a bit of space in here to make sure it was separated. Good. And what if I want to update one of the definitions? Well, we can say, add the word cat again and say, an animal that likes kitty treats. And then, if I get the definition with 2 for dog, it looks like this, animal that goes woof. And 2 for cat, animal that likes kitty treats. And what happens if I say 2 and then look up horse which is not in the dictionary. Not found. And zero is exit the program. Program done. Excellent. Hopefully, that helps. So, pause the video and create the DictionaryProject in Visual Studio. Come back when you're done or if you need some help.

How did that go for you? Were you able to accomplish the tasks for this project? Let's work on it together. So, let's create a new project and we will create the DictionaryProject as an empty project. DictionaryProject. 'Create'. And of course, we need to add the Dictionary.h file, the header file for our Dictionary. Dictionary and then, for the implementation file, we can add Dictionary.cpp. And we can also add our main file, main.cpp. So, with main, I'll just include the basics for right now. Probably need some string, #include  "Dictionary.h" using namespace std, return 0. All right. So, #ifndef DICTIONARY_H #define DICTIONARY_H. Inside here, I will use the string and the map types. And then, we have class Dictionary with the public section and of course, a private section as well.

The private section will have a map, with string and string type. So dictionary, it will have the key being a string which is the word you're looking up and then, the value being the definition which is also a string. So that's why, it's string, string. It could be other types, of course, addDefinition(). There's a word and a definition and then, we have getDefinition. You notice we're missing a constructor. We don't really need one because there's no special set up that needs done. Okay, printAll() const. And now, if we go over into the Dictionary.cpp, we can #include iostream and then, we can also, of course, #include  Dictionary.h We need iostream for the printing. Now, I'm going to copy over these methods right here into Dictionary.cpp and give them each bodies. So, we'll give them each a body and then, worry about filling in the Dictionary and the scope resolution operator. So, let's see here.

All right. So, we also need to copy that and put it right here and also, right there, seems to have gotten rid of the majority of our errors. Now, what we're going to do is for addDefinition, which is one of the more complicated ones, probably the most complicated getDefinition's pretty straightforward for the most part. I guess they're all about even, printAll is easy but the other two are probably about even. So, we need auto it. We could set it up as an iterator and fill it out but this is quicker. We are using the find that I alluded to during the requirements discussion. Remember that if the iterator is not equal to the end of the map of the dictionary, that means it's already in the dictionary. If it does equal the end, that means that it went through the whole dictionary trying to find it and couldn't. So, I will say if the iterator is not our map called dictionary.end(), if it's not the end iterator, it's one case, otherwise it's another. The else is a new word and the if section is already in the dictionary. So, what do we do if it's already in the dictionary? So, we say we already know what the first is, it's already the word. So, we're just changing second. That's one way to do it right there. You could use bracket notation. For a new word, we'll use bracket notation, I suppose. So, dictionary[word] = definition. This is actually the best way to do it when you're adding a new word, in my opinion. Now, how do we get a definition?

Well, for getting a definition, we again set auto it = dictionary.find(word). And we're going to set a result here. Set it equal to empty string and eventually, it will return that result. But if it is not equal to dictionary.end, we also look in the else section. So, if it's in the dictionary, we set result equal to the definition. If it is not in the dictionary, meaning the it equals, the iterator equals the end of our map, the end iterator, then we know that the result is equal to what we were supposed to set it to.

That's right. Not found like that. So, I added the !, printAll. For each of these, auto and I'm just going to call it pair, you can call it whatever you want really. We're just printing pair.first and then maybe, :\t and " and then, endl. And then, after the loop, I'm putting another endl, just to add some more separation. The next thing we need to do is work on the interaction with the user. So, we've got the dictionary set up and it does it's job. But it doesn't handle the user interaction. So, for my implementation, which is unlikely to be exactly like yours, it might be similar depending on what you've decided to do. But I'm going to printMenu(). I'm also going to have a user interface for adding the word that takes a dictionary reference. Remember, these are not member functions. They're inside main. They're just global functions, getDefinition, this is the UI for getting a definition. And you could have made it more streamlined, you could have done more UI components, you could do whatever you want So, below main, I'm going to give each of these a body like what I do for member functions. I like doing that. We will come back to main in just a moment but print menu, let's do this. So, maybe a little extra space at the beginning. We're going to say, "Type your selection." Okay. And while I'm typing large amounts of things of course any time during the course you can do it but I expect not everyone types very fast and that's totally fine If you have to pause me or pause any instructor on any course, that's one of the reasons taking a course online can actually be advantageous.

You can slow down, speed up, you can do all kinds of different stuff. So, add a new word and definition. Now, our program makes no sense of this input yet, right? But we're just printing out the options to the user and then we will decide what to do when they enter particular input. Print all definitions. So, some of these will take further input and some like exiting or printing everything won't. Exit the program and then maybe a little extra space at the bottom. All right, pretty good for that. We'll use that. We're not doing the input in this. We're just printing the menu. Now, what about adding the word? So, when we go to add a new word, I have a word and I have a definition that we will use. We'll tell the user to enter the word and then we should use get line, right? Because that's what we do for strings. Generally, enter the definition for and then I just thought I'd be clever put a space there and actually say internet for cat or cow or whatever they decide to define. This time, it will go into definitions. We've got the two strings but they don't really exist in anything. So, what are we doing? We're having the dictionary handle it for us and since dictionaries passed in we can just say add definition. Remember, we wrote that. So, it takes care of all the nuances. If they said enter the word and the definition of the word already exists, add definition will actually take care of updating it as well. We took care of that. So, that's pretty cool. Now uiGetDefinition. So, string theWord we'll say for which word do you want the definition? All right. And then we'll do get line. Okay. And then what are we going to do? We're going to say dictionary.getDefinition and pass in the word that they asked for. Maybe a little extra space at the end. And remember get definition handles the case where the word doesn't exist. So, if it looks and it can't find it then what do we do we print out, "Not Found". Right? That's pretty clever. So, that's pretty good. Now, we should probably add some stuff to main. Now, main is going to have some of the work in it. We could have broken out even more work if we wanted to but we'll put the dictionary right here and then end user choice. So, we're going to be using an integer and taking an input. Do you remember what we have to do? Because we've also got some get lines coming out. So, do you remember what we have to do when we take in an integer. I bet you do remember we have to consume the new line character otherwise the get line is going to try to consume it as its input and that's going to cause all kinds of problems. So, printMenu that just prints the menu and then the user enters their choice which at this point is just an integer. We do cin.get to consume the new line character. It's one of the ways to consume the new line character and that's the priming read for our loop while the user's choice, I'm going to put end while here just so we can see where the curly braces line-up.

While the user's choice is not zero, meaning don't exit the program. Remember, we have a priming read here so we're taking care of the one they just entered. We will put another prompt at the end of the loop for the next round. Okay, that's what the priming read's for. We could use a clever stuff with a do while loop and some other techniques but that's what I chose to do. So, if the user choice is one, then what do we do we call uiAddWord and then pass the dictionary. Right? That's cool. So, the dictionary was declared in main and we are passing it to the function to handle it. If the choices two, what do they want to do? They want to print the definition or get the definition and print the definition for a particular word and that will be decided inside of uiGetDefinition because it causes the UI to occur. If the choice is three, then that means that they want to print all the definitions. So, printing all definitions and we can call theDictionary.printAll see that's pretty clever to be able to do that, right? Now, after the while loop, what can we, after the if statements rather what do we do? We have to ask the user for their next choice. So, maybe put a little extra space here and then print the menu so we have a couple levels of space. The user enters the choice again. And we also have to consume the newline character, right? So, that's what we're doing with this, so consume the newline. Okay. Oops. All right. Now, what do we do now? So, after the while loop we can just verify that the users exited and just say, "Program done" or " Program complete" or whatever you want to do. Alrighty. Now, I think we're probably ready to run this. So, let's give it a shot. 'Debug', 'Start without debugging'. Let's see what we get. So, we get this nice cool menu here let's say add a new word one and then enter the word cat and then say meowing creatures. So, it's a creature who goes meow, whatever you want to do an animal that lives in my house and requires petting whatever you want to say the definition is, then it will say type your selection, this is the next selection. Now, what do we do for the next selection? Maybe add a new word, Dog a woofing. I put creating. That's not good. That's okay. A woofing creature. So, I guess maybe this gives us an opportunity to update something. So, three print all definitions. It'll print cat a meowing creating. So, that wasn't right. I didn't want that. So, let me see. Get the definition for a word or say for which word do you want the definition, cat? And it says a meowing creating. That's interesting. So, I'll say add a new word and then I'll add cat again, but I'll say a meowing creature. That's really what I want. And then say print the definition for a word cat. And now it's updated, a meowing creature and we can print all definitions again and it prints all too. And what else can we do? There's all kinds of possibilities. Maybe add another word. Let's say, computer, a really cool device that helps me check Facebook. Right? And then get all, print all definitions and we got all the definitions down here. Awesome. Looks good to me. Now, what about exiting the program? Can we do that? And the answer is yes. Hit zero. Program done. Pretty cool. Awesome. Seems to work pretty great, doesn't it? And again, it's highly likely that if you worked on this on your own, whether you completed part of it or the whole thing. You likely use some very different techniques and approaches than I did, and that's totally okay. I hope you were able to get most or all of this done. But even if you didn't don't stress out, keep practicing. In the next lecture, we have another project where we will create some aliens. Aliens? What's that all about? Join me in the next lecture to find out. I'll see you there.

About the Author
Learning Paths

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics