Definition of Object C Parsing
[11 of C parsing] Class-A Collection of thousands of pets in one"Special Data Type "-class, the object is a class instance. A class is a "template" for generating objects. A class is essentially an abstract concept, and the system does not allocate storage space for the class. An object is a physical entity of a class-"special data type" and has a storage space.The relationship between classes and objects is the relationship between data types and variables.1. object definition:
- Define both objects of a class: The method is to directly write the Object Name List in the right curly brackets defined by the class.
- Use class name to define objects: The definition format is the same as that of common variables.
2. Object Storage space:
The object has member data and member functions. For example, the book has member data: int price;
Int pages;
Member functions:
Void ShowPrice (); void ShowPages ();
How can I allocate the object storage space? First, different object data is different, so each object should have its own independent data space. Second, the member functions of the same object are the same. For high efficiency, the function only needs to store one copy, all objects can share function space.
3. Object Access:
- Access through objects: Object Name. Data member object name. Function Member
For example, book. price;
Book. ShowPrice ();
- Access via pointer: pointer-> data member pointer-> function Member
For example, p-> price;
P-> ShowPrice ();
4. small object test:
This small example demonstrates the entire process from defining classes to defining objects. According to the running results, it is suggested that the sizeof value of mybook is 8, which is exactly the sum of the size of price and pages. This confirms that the object has its own data space, the storage policy of the shared function space.