[C parsing 13th] object definition

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.