C ++ class

Source: Internet
Author: User

C ++ class

In the real world, there are often objects of the same class. For example, your bicycle is only one of many bicycles in the world. In object-oriented software, there are also many objects that share the same features, such as rectangles, employment records, and video clips. You can use the same features of these objects to create a set for them. This set is called a class. A class is a blueprint or prototype that defines the variables and methods of all objects of the same class. For example, you can create a bicycle class that defines instance variables such as the current gear position. This class also defines and provides the implementation of the instance method (shift change, brake. The instance variable value is provided by each instance of the class. Therefore, after creating a bicycle class, you must instantiate it before use. When creating a class instance, an object of this type is created, and the system allocates memory for the Class-defined instance variables. Then, you can call the instance method of the object to implement some functions. Instances of the same class share the same instance method. In addition to instance variables and methods, classes can also define class variables and class methods. You can extract class variables and methods from the class instance or directly from the class. Class methods can only operate class variables-you do not need to access instance variables or instance methods. When the system first encounters a class in the program, it creates a copy of all its class variables for this class-all instances of this class share its class variables.

Next, let's look at the code and give a simple case:

1,

# Include
 
  
Using namespace std; // defines the HotDog class // note: the class has no space class HotDog {// private member private: // only the Member method can be accessed or the member function can access int age; // The member variable int bbb; // public member public: // member function // static void say_hello (void) {int a; int B; cout <"hello HotDog" <endl ;} // generally, as long as it is a private member, the get set operation method void Set_Age (int age) {this-> age = age;} int Get_Age (void) is required) {return this-> age;} // declare a function as the friend function of the class: friend int main (void); // protected member protected :}; int main (void) {class HotDog Dog; Dog. age = 100; cout <"age:" <Dog. age <endl; // Dog. set_Age (100); // cout <"age:" <Dog. get_Age () <endl; // you can call HotDog: say_hello (); return 0;} directly with the class name unless the operation method is declared static ;}
 
Result: a: 100

Hello HotDog

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.