I guess you can look at classes from a couple perspectives, the compiler and the programmer's.
To a compiler a class is a data type that contains data and all the functions that are necessary to access and manipulate that data. The data and the functions make up the class members. The class declaration and all member declarations are written in a header file (.h). All members of a class belong to an access scope. An access scope is like a security container. Access scopes are compiler specific, but ones to remember are private, protected, and public. Private members can only be accessed by members of the same class. Protected members can only be accessed by members of the same class and its derived (child) classes. Public members have no access restrictions. Class functions are written in a source file (.cpp). The source file must contain a #include statement that links it to the header file.
In c++, classes come with a few built-in features. There are two functions that are implied in a class definition. They are the constructor and destructor. Whenever a class is instantiated the constructor is called and creates an instance of the class, or object of the class. Whenever an instantiated class (object) is removed from memory its destructor is called. The constructor can be overloaded (declared with a different parameters). To use the overloaded constructor when instantiating the class, you'll have to call it explicitly.
To a programmer, a class provides a added layer of abstraction that provides encapsulation and modularity. You may be familiar with the term "black-box" technology. It means that you don't need to know how the device works. All you know is whenever you put something in you'll get something out. Kind of like in the cartoons where there will be a conveyor belt and an enclosure. Where some raw material, or character, goes into the enclosure, it comes out as some finished product. What happens in the enclosure we don't need to know. The enclosure is the "black-box", or object. The blue-print for the object is the class.
But say you want the raw material to not just come out as some finished product, but to also be packaged and ready to ship. Do we have to redesign our enclosure? Nope, just write a blue-print for a machine that takes our finished product and packages it, build the machine (instantiate the class), and connect the two. Not only did you not have to rewrite your original blue-print, but if something goes wrong, you can quickly track it down by examining the product as it passes between machines (objects) and re-write only the blue-print (class) that contains the error.
|