Join Bill Weinman for an in-depth discussion in this video The C++ classes used in this course, part of Code Clinic: C++.
When I'm working on a project in C++, I'll often use a set of personal classes for a number of purposes. One of the most important benefits of an object-oriented language like C++ is the enhanced reusability of classes. I have a number of classes that I use regularly and I've included some of them here. As this course title expands in the future, I may add more classes to this library. Here I have the library open in Xcode. This is standard C++ 11, so you can use any compiler or IDE that supports standard C++ 11.
You do not need to use Xcode. There are a number of classes here. Let's just take a look at them one by one. BWUTest is a simple unit test class. It's really very simple and I like to use this rather than the unit test for any particular environment because that makes my unit tests portable. Also, it's just a very simple problem and easy to implement. I don't think this took me more than an hour to write and test. In the header file, I have a couple of constant strings for pass and fail, and I have a couple of object variables that keep track of the pass count and the fail count.
And that's really all there is to it. And here in the implementation, there's a test method, which takes a character string for description of the task and a flag for whether it passed or failed, something that it'll evaluate to true or false. And this pf pointer is for my pass or fail string. If the flag, the result of the test, is true, then I get the pass string and I increase the pass count, and if it's false, I increase the fail count and I have the fail string. It's really as simple as that and I have a little report method that reports the pass and fail totals, and if we come over here to my main.cpp file, this is unit test for this entire library.
And if I just do clean, and build, and run, and here we have the results. You see each of these tests, they say pass, pass, pass, pass, pass. And then, at the end of each section, I have a pass count. And under the strings, I get a pass and fail count for that. And the numbers, I get a pass and fail count for that. And then in my source code here, you notice all I do is I call, I create the unit test object some place up here, BWUTest u, and I'll give it a string name for that set of unit tests.
And then I call unit test with something that's got a true or false result to it. And so you'll noticed, you know, I'm doing a memory compare of this value and that value. If it equals 0, then I pass, and if it doesn't equal 0, then I get fail. And then, you know, these just come out like this in the results. It’s very, very simple and very, very useful. The main benefit, and I’m harping on the unit test here a little bit because I know that a lot of people don’t use them. The main benefit of using unit tests is that as I go and develop more things in these classes, I just add unit tests to my test script.
And my existing unit tests are already there, so if I add some code that breaks something that was already implemented and tested, it shows up really, really easily. So, I highly recommend that you use units tests. Again, it's easy to implement. Most IDEs have a unit test module. I personally prefer to use my own. It's very simple and it also makes it portable, so no matter what environment. Again, I use these same unit tests on Unix, I use the same unit tests on Windows, I use the same unit tests on the Mac, and it's just so easy to implement.
Feel free to use this one or use it as a model to write your own. The BWString class is a simple string class that does most of things that the STLString class does, but without templates or iterators, so that makes it small and lightweight. I have nothing against templates and iterators. I use them often when I'm using STL classes. But for the purpose of a string class, this is smaller and lighter and it works great for many purposes. The BWNumber class provides a unified numeric object with both integer and floating point values.
I don't use it a, lot but for some things, it's really, really convenient and really, really valuable. I use this with the database routines. The BWDB class is a database wrapper for the SQLite database engine. I talk about this in detail in the Lake Pend Oreille chapter. That's the first problem in this course. The BWKV is a simple key value storage class. It's based on BWDB and I use it for a lot of different things. Not sure that I've used it yet in this course, but I probably will later. BWWebGet is a simple class for retrieving HTTP objects and BWCGI is a simple class for CGI.
I cover both of these in the Lake Pend Oreille chapter. And then we've already seen my main file. This is really just a place for me to do my unit tests. And so I can keep all of this working and keep it in good shape. As I go and add things and change things, I update the unit tests and make sure that everything works great. So, these are the classes that I find convenient to keep around. Feel free to use them for your own projects or use them as a starting point for your own class library.
Bill introduces challenges and provides an overview of his solutions in C++. Challenges include topics such as statistical analysis, searching directories for images, and accessing peripheral devices.
Visit other courses in the series to see how to solve the exact same challenges in languages like C#, Java, PHP, Python, and Ruby.