2/27/2024 0 Comments Chapter 9 classes a deeper lookThe assignment operator ( =) can be used to assign an object to another object of the same type.So, returning pointers or references to private data is a dangerous practice that should be avoided. Returning a reference or a pointer to a private data member breaks the encapsulation of the class and makes the client code dependent on the representation of the class’s data.Function abort performs similarly to function exit but forces the program to terminate immediately, without allowing the destructors of any objects to be called Function exit forces a program to terminate immediately and does not execute the destructors of automatic. The corresponding destructors are called when main terminates. Constructors are called for objects defined in global scope before any other function (including main) in that file begins execution (although the order of execution of global object constructors between files is not guaranteed).A class may have only one destructordestructor overloading is not allowed. A destructor may not specify a return typenot even void. A destructor receives no parameters and returns no value.Note that a destructor is often referred to with the abbreviation “dtor” in the literature. This naming convention has intuitive appeal, because as we will see in a later chapter, the tilde operator is the bitwise complement operator, and, in a sense, the destructor is the complement of the constructor. The name of the destructor for a class is the tilde character (~) followed by the class name. A destructor is another type of special member function.The C++ user community benefits by having more ISV-produced class libraries available. No proprietary information is revealedas would be the case if source code were provided. The ISVs provide in their products only the header files and the object modules. This encourages independent software vendors (ISVs) to provide class libraries for sale or license. The clients do, however, need to be able to link to the class’s object code (i.e., the compiled version of the class). Clients of a class do not need access to the class’s source code in order to use the class.Hidden global variables can be accessed with the unary scope resolution operator. Such a hidden variable can be accessed by preceding the variable name with the class name followed by the scope resolution operator ( ::). If a member function defines a variable with the same name as a variable with class scope, the class-scope variable is hidden by the block-scope variable in the block scope. Variables declared in a member function have block scope and are known only to that function.Nonmember functions are defined at file scope. A class’s data members (variables declared in the class definition) and member functions (functions declared in the class definition) belong to that class’s scope.The function code is nonmodifiable (also called reentrant code or pure procedure) and, hence, can be shared among all objects of one class. Each object, of course, needs its own copy of the class’s data, because the data can vary among the objects. All objects of the class share this one copy. The compiler creates one copy (only) of the member functions separate from all objects of the class. ![]() Applying operator sizeof to a class name or to an object of that class will report only the size of the class’s data members. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |