Classes and object instances in C ++, and object instances
1. Dynamic Creation and release of Objects
1. What is dynamic object creation and release?
Normally, all the objects we create are created by the C ++ compiler in the stack memory, and we cannot manage their lifecycles. Therefore, we need to dynamically create this object, so we need to create and release the object in the heap memory. In the C language, we provide malloc () and free () functions to allocate variables in the heap memory, however, the new and delete keywords are introduced in C ++ to allow us to dynamically create and release variables.
2. new and delete keywords
The new keyword is used to create a variable in the heap memory. The format is Type * ptr = new Type (constant/expression). The constant/expression in the parameter list can be used to initialize the variable, it can also be omitted without writing. The returned result is a pointer of this type. If the memory allocation fails, a null pointer is returned.
The delete keyword is used to release the memory created with the new Keyword, in the format of delete ptr (the release array must be enclosed in parentheses, delete [] ptr ).
3. Differences between the new and delete keywords and malloc and free keywords
When allocating memory, the new Keyword calls the constructor of the corresponding class based on the parameters it creates. Before releasing the memory, the delete keyword first calls the class destructor to release the memory defined in the object.
The malloc and free keywords do not call class constructor and destructor.
4. Example of new and delete keywords
# Define _ CRT_SECURE_NO_WARNINGS
# Include
Using namespace std;
Class Teacher
{
Public:
Char * name;
Int age;
Public:
/* No parameter constructor */
Teacher ()
{
Name = NULL;
Age = 0;
Cout <"No parameter constructor executed..." <endl;
}
/* Constructor with parameters */
Teacher (char * name, int age)
{
/* Allocate heap memory in the constructor */
This-> name = new char [sizeof (name) + 1];
/* Initialize the member variable */
Strcpy (this-> name, name );
This-> age = age;
Cout <"the constructor with parameters is executed..." <endl;
}
/* Copy the constructor */
Teacher (const Teacher & student)
{
/* Re-allocate memory */
This-> name = new char [sizeof (name) + 1];
/* Initialize the member variable */
Strcpy (this-> name, name );
This-> age = age;
Cout <"copy constructor executed..." <endl;
}
/* Destructor */
~ Teacher ()
{
If (this-> name! = NULL)
{
Delete [] this-> name;
This-> name = NULL;
This-> age = 0;
}
Cout <"destructor executed..." <endl;
}
};
Int main ()
{
/* Create an int variable and release it */
Int * a = new int;
Int * B = new int (100 );
Delete;
Delete B;
/* Create a double variable and release it */
Double * c = new double;
Double * d = new double (10.1 );
Delete c;
Delete d;
/* Create an array and release it */
Char * e = new char [100];
Delete [] e;
/* Create an object and release it */
Teacher * stu1 = new Teacher ("Wang Gang", 22 );
Cout <"name:" <stu1-> name <", age:" <stu1-> age <endl;
Teacher * stu2 = new Teacher ();
Delete stu1;
Delete stu2;
/* Using malloc and free to create an object, you cannot call its constructor and destructor */
Teacher * stu3 = (Teacher *) malloc (sizeof (Teacher ));
Free (stu3 );
}
2. static member variables and static member functions
1. static keywords
The static keyword is used to declare a member of a class as a static attribute. After a member is modified with the static keyword, the objects created by this class share the static member. No matter how many objects are created, this member has only one instance. Static members are related to the class and act as a class, rather than related to the class objects.
2. Concept of static members
A static member is a shared member of all objects of the class, rather than a member of an object. It does not occupy storage space in the object. This member belongs to the entire class and does not belong to a specific object, therefore, static member variables cannot be initialized inside the class and must be initialized outside the class. For example, to define a student class, the total number of student objects can be declared as static. In the constructor, add 1 to the variable to calculate the number of student objects.
3. Summary of static member variables
Static member variables can be defined with the static keyword, but initialization must be performed outside the class.
Static member variables can be accessed and modified by class and class objects.
Static member variables follow the class access control principle. If they are modified in private mode, they can only be accessed inside the class and during class initialization, and will not be accessed by other methods.
4. Summary of static member functions
Static member functions are defined using the static keyword. static member functions can access static member variables and static member functions, but cannot access common member variables and member functions, because ordinary members belong to objects rather than classes. Different levels. However, normal members can access static members.
When static member functions are defined in the class but implemented outside the class, no static keywords need to be added.
The static member function does not have the this pointer.
5. Summary of static members
Static members are the owners of classes and classes. Therefore, static member variables cannot be initialized inside the class and must be initialized outside the class.
Static members still follow the access control principles of private, protected, and public.
Static member functions do not have the this pointer. They cannot access common member variables and member functions. They can access static member variables and member functions, but can access common members by passing objects.
6. static member variable demonstration
# Include
Using namespace std;
Class MyStudent
{
Private:
Static int count;/* Total number of student objects */
Char name [64];
Int age;
Public:
Static int n;
Public:
MyStudent (char * name, int age)
{
Strcpy (this-> name, name );
This-> age = age;
MyStudent: count ++;/* Number of students plus 1 */
}
Void getCount ()/* common member functions access static member variables */
{
Cout <"Total number of students:" <MyStudent: count <endl;
}
};
/* Initialize static member variables */int MyStudent: count = 0;
Int MyStudent: n = 10;
Int main ()
{
/* Test static member variables */
MyStudent student1 ("Wang Gang", 22 );
Student1.getCount ();
/* Access static member variables using objects and Classes */
Student1.n = 100;
MyStudent: n= 200;
}
7. static member function demonstration
# Include
Using namespace std;
Class Test
{
Private:
Int m;
Public:
Static int n;
Public:
Void setM (int m)
{
This-> m = m;
/* Access static member functions */
Test ();
}
Public:
Static void xoxo ();
Static void test ()
{
N = 100;
// M = 10; access to common member variables is not allowed
// Int c = getM (); normal member functions cannot be accessed.
// This-> m = 1000; this pointer does not exist
Cout <"static void test () function..." <endl;
}
};
/* Initialize static members */int Test: n = 10;
/* Declared in the class, out-of-class implementation */void Test: xoxo ()
{
Cout <"static void Test: xoxo" <endl;
}
Int main ()
{
Test t;
/* Common member functions access static member functions */
T. setM (10 );
/* Call method of member functions */
T. test ();
Test: test ();
}
? 3. youyuan functions and youyuan classes
1. youyuan Function
When we define a class, we use the private keyword to modify the member variable (member function) to achieve access control. Sometimes, we need some functions to access the private members (attributes or methods) of the object. C ++ provides us with the concept of friend functions, A friend meta function is a good friend of this class. It allows this function to access the private attributes and private methods of the objects created in this class. Friends functions are declared using the friend function. The declaration of friends functions must be inside the class, and the implementation of friends functions must be outside the class (if the implementation of friends functions is also inside, so what else do we need to use the friend functions ?), The declared position of the Enis function is independent of the Access Controller.
2. youyuan function example
# Include
Using namespace std;
/* Define Point class */class Point
{
Private:
Int x;
Int y;
/* Define a friend function: calculate the distance between two points */
Friend int distance (Point & p1, Point & p2 );
Public:
Point (int x, int y)
{
This-> x = x;
This-> y = y;
}
};
/* Implementation of the youyuan function */int distance (Point & p1, Point & p2)
{
Int dx = p1.x-p2.x;
Int dy = p1.y-p2.y;
Return sqrt (dx * dx + dy * dy );
}
Int main ()
{
Point p1 (3, 4 );
Point p2 (0, 0 );
Int dis = distance (p1, p2 );
Cout <"The distance from point (3, 4) to the origin is:" <dis <endl;
}
3. youyuan class
If Class B is A Class A's membership class, all member functions of Class B are membership functions of Class. Class B can use all the private attributes and methods of Class.
The user Meta class is usually designed as an auxiliary class for data operations or message transmission between classes.
4. youyuan Class Example
# Include
Using namespace std;
/* Define class A */class
{
Private:
Int x;
Friend class B;/* defines class B as the membership class of class A */private:
Void setX (int x)
{
This-> x = x;
}
};
/* Define class B */class B
{
Private:
A AObj;
Public:
/* All member functions of Class B are membership functions of Class A. Therefore, you can use the private attributes and methods of Class */
Void operater (int tmp)
{
AObj. setX (tmp );
}
Void display ()
{
Cout <"Private Property x =" of Class A <AObj. x <endl;
}
};
Int main ()
{
B B;
B. operater (100 );
B. display ();
Return 0;
}