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