Object-oriented programming emphasizes how a program represents data. The first step in solving programming problems with the OOP method is to specify how the data is to be used, based on the interface between it and the program to describe the data. Then, design a class to implement the interface. In general, private data members store information, and public member functions (also known as methods) provide the only way to access data. Class combines data and methods into a single unit whose private nature implements data hiding.
Typically, a class declaration is divided into two parts, which are usually stored in separate files. A class declaration, including a method represented by a function prototype, should be placed in the header file. The source code that defines the member function is placed in the method file. This separates the interface description from the implementation details. Theoretically, classes can be used only by knowing the public interface. Of course, you can view the implementation method (unless only the compilation form is provided), but the program should not rely on its implementation details, such as knowing that a value is stored as an int. As long as programs and classes communicate only by means of defining interfaces, programmers are free to make independent improvements to any part without worrying about unintended undesirable effects.
A class is a user-defined type, and an object is an instance of a class. This means that the object is the most variable, such as the memory allocated by the new class description. C + + tries to make a user-defined type as similar as possible to a standard type, so you can declare an object, a pointer to an object, and an array of objects. You can pass an object by value, return a value as a function, and discovered computers an object to another object of the same type. If a constructor is provided, the object can be initialized when the object is created. If you provide a destructor method, the program executes the function after the object dies.
Each object stores its own data, and the class method is shared. If Mr_object is the object name, Try_me () is a member function, you can call the member function by using the member operator period: Mr_object.try_me (). In OOP, this function call is referred to as sending the Try_me message to the Mr_object object. When a class data member is referenced in the Try_me () method, the corresponding data member of the Mr_object object is used. Similarly, the function call I_object.try_me () accesses the data member of the I_object object.
If you want the member function to operate on more than one object, you can pass the extra object as a parameter to it. You can use the this pointer if the method requires an explicit reference to the object that called it. Because the this pointer is set to the address of the calling object, *this is the alias of the object.
Class is well suited for describing ADT. The public member function interface provides the services that ADT describes, the private part of the class, and the code for the class method that provides the implementation that is hidden from the client of the class.
C + + Primer Plus 10th object and Class learning notes