Classes and data abstraction
Classes are the second form of structured data type in C++, and they are what makes C++ an object-oriented language. A class combines both the data, similar to the members of a struct, and the operations you can perform on that data. Unlike structs, a class presents up to three levels of access to the members (public, private, and protected). Members of a class may be public and accessible to the users of the class. Private members of a class are only accessible within the class, and all members are private by default. When you declare a variable of a class type, you are declaring an instance of the class, also called an object. You use the dot operator (.) to access members of a class, much like when accessing members of a struct. By default, the only operation defined for classes is the assignment operator. You may define functions that have classes as parameters, and you can pass them as either reference or value parameters.
Classes carry functionality with the data contained in the class. It is not uncommon for a class to have no public data members, only methods (operations on a class) and properties (in C++ these are usually void functions, either a void return or void parameter function). The overall process of restricting data from the users of your classes is called information hiding, and it is part of the overall abstraction process. One of the goals of object-oriented designs is to remove the unimportant details from the view of those using the objects, which enhances the integrity of the data in the object by preventing direct manipulation. There are two special members of a class, the constructor and the destructor. The constructor is called whenever an instance of the class is instantiated, and the destructor is called whenever an instance is destroyed, either automatically or explicitly. You may develop many constructors for different types of input parameters. The default constructor has no parameters, but you may change this to a function with parameters that all have default values.
Static members of a class create a level of persistence similar to a static variable in a function. If a static data member or function is also publicly visible, the static member can be called directly by a user of the class without using an instance of the class. Static data members of a class are shared across all instances of the class, which can conserve memory for key data structures, but it can introduce undesirable interactions between seemingly independent objects. A static function cannot access non-static data members of the class unless the access is through a local variable in the function.
The overall process in object-oriented design works toward creating Abstract Data Types, which are general purpose abstractions. A complete ADT defines all of the data and the methods you can perform on that data. The implementation details are hidden in cases where the ADT is not provided as a full-source kit. One final thing to remember is that classes by themselves do not create executable code; rather, when you compile a program that uses classes, the code defined by the class is included in your program.
|