C++ classes are simple and flexible. This is the fundamental unit of object-oriented programming and it is the major distinction between C and C++.
- [Instructor] Classes in C++ are very powerful and flexible. This is the fundamental unit of object oriented programming. So here we have a working copy of class.cpp from chapter one of the exercise files. This is an example of a very simple class, see it even says so in the comment, very simple class. This class is named c1. It's defined here using the class keyword. You can also define a class with the struct keyword, the only difference being that members default to public with struct so I would also need to say private here for it to be equivalent.
With the class keyword members default to private and so it's traditional to simply list your private members first then the public keyword and then your public members which are typically the interface. The data members are typically private because you don't want them accessed, you want them properly encapsulated so you don't want them accessed directly from the object. Your interface, your function members, sometimes called methods, are public so that they may be accessed from the object interface.
Here we have one data member, it's an integer i with initial value of zero and we have two function members or methods called setvalue and getvalue. Setvalue returns void or nothing, it does not return a value. It takes one argument, a constant int reference called value and all it does is it sets the private data member to equal the value. Getvalue simply returns the value, so it returns the integer and you'll notice this const qualifier here which indicates that this method, this function member is const safe, it may be used in a const context from the object interface.
So down here in main we have a variable i 47. We declare an object o1 from our class. We use the setvalue method to set the value and we use getvalue to get the value and when I build and run you notice it says value is 47, this is exactly what we expect. Normally it's considered best practice to separate the interface and the implementation oftentimes with one liners like this. I'll include them in the interface because it has very, very little impact on the readability or even on the compile time.
But it's often considered best practice to define these functions outside of the class and certainly with more than one line I'll do that myself all the time. So I'm going to take these, I'm going to make copies of them. We're going to reduce these definitions to forward declarations which is how this is done. So I simply take the body off and put in a semicolon. Technically I don't need the name of the variable up here. I'll often include it, sometimes I won't, it's probably best to be consistent but in practice I'm not terribly consistent about that.
In order for these functions to be associated with the class I need to use the scope resolution operator, or the double colon, with the class name. That says that these are members of this class, like that. Now this will all work as expected and even though these are one liners you might want to put them on separate lines like this. Now when I build and run you'll notice that this works exactly the same.
So these are member functions and you can tell because they have the class name with the double colon scope resolution operator. Now in practice all of this would also probably be in three different files. The class definitions, starting on line seven through line 12, would be in a .h file, a header file. The implementation, the member functions, would be in a .cpp file. Then everything else would be in the main cpp file or perhaps in several different files.
But for teaching purposes I'll usually include all of this in one file so that we have a good idea of what's going on without having to jump from file to file and we can look at the code and evaluate the code and experiment with the code more conveniently. So a class is a fundamental unit of object oriented programming and this is how a class is defined.
- Classes and objects
- Constructors and conversion operators
- Class inheritance
- Smart pointers
- Move semantics
- Lambda syntax
- The C preprocessor
- Unit tests
- Building a custom string library