1. C + + class declaration:
Class Class_name
{
Private
/*
* Private data and member functions
* Can only be referenced by member functions in this class, cannot be called outside the class
* Friend class exception
*/
Public
/*
* Public data and member functions
* Can be referenced by member functions in this class, or by other functions within the scope of the class
*/
Protected
/*
* Protected data and member functions
* cannot be accessed outside the class, but can be accessed by a member function of a derived class
*/
};
2. Define member functions outside the class
void Class_name::fanction_name ()
{
function body
}
3. Define built-in functions
inline void Class_name::fanction_name ()
{
/*
* Built-in functions embed function code into the program's call point when it is executing
* To reduce the time overhead of calling member functions
* Built-in function requirements: function size is small, call frequency is high
*/
}
4. Define the object of the class
Class_name name_1,name_2;
5. References to Object members
(1) Accessing members in an object by object name and member operator
Class_name name_1;
Name_1.fanction_name ();
(2) Accessing a member in an object by pointing to the object's pointer
Class_name name_1,*p; Pointer variable p for defining objects Name_1 and Class_name classes
p = &name_1; is P pointing to the object name_1
P->fanction_name ();
(3) Accessing members of an object by reference to the object
Class_name name_1;
Class_name & name_2 = name_1; Defines a reference variable name_2 a class and initializes it to Name_1
Name_2.fanction_name ();
/*
*name_1 and name_2 occupy a memory together
* So Name_2.fanction_name () is Name_1.fanction_name ()
*/
6. Constructor function
(1) Introduction to constructors:
Constructors are used to handle initialization of objects;
The name of the constructor must be the same as the class name and cannot be arbitrarily named;
The constructor cannot be called by the user, but is executed automatically when the object is created;
The constructor does not have any type and does not return any values;
(2) Definition of constructors outside the class
The constructor is declared first in public when the class is defined
Class_name ();
Defined outside the class
Class_name::class_name ()
{
/* If the user does not define the constructor themselves, the C + + system will automatically generate a constructor,
* Just the function body of this constructor is empty and does not perform the initialization operation
*/
}
(3) Constructors with parameters
Class Class_name
{
Public
Class_name (Int,int,int); Declaring a constructor with parameters
Private
int height;
int width;
int lengh;
};
Defining a constructor with parameters outside of a class
Class_name::class_name (int a,int b,int c)
{
Height = A;
width = b;
Lengh = C;
}
Defining an object and assigning an initial value to three member variables
Class_name Name_1 (a);
(4) Overloading of constructors
Define multiple constructors in a class to provide different initialization methods for the objects of the class.
These constructors have the same name, but the number of arguments or the type of the parameter is different
The compiler determines the corresponding constructor based on the parameters that are assigned to the object when the object is defined
Declaring constructors in the definition of a class
Class_name (); Declaring a parameterless constructor
Declares an argument constructor that initializes a data member with the initialization table of the parameter.
class_name (int a,int b,int c): Height (a), width (b), Lengh (c) {}
Defining objects
Class_name::name_1; Defines an object name_1 does not specify an argument, the first constructor is called
Class_name::name_2 (+/-);//Define an object name_2 Specify an argument, call the second constructor
7. destructor
Declaration of a destructor when defining a class
~class_name ();
Definition outside of class
Class_name::~class_name ()
{
The function of the destructor is to do some cleanup before undoing the memory occupied by the object
}
8. This pointer
Each member function contains a special pointer to this, which is a pointer to this class of objects
Its value is the starting address of the object where the currently called member function resides
The this pointer is used to differentiate the member functions of different objects by referencing their corresponding data members
9. Derivation and inheritance
(1) Declaration of a derived class
Declares that the base class is a derived class that class_name in common inheritance Class_name_1
Class Class_name:public Class_name_1
{
Private
New members in derived classes
Public
};
(2) Access properties for derived class members
A base class member function cannot access members of a derived class
The member functions of a derived class differ depending on how they are inherited:
Public inheritance
———————————————————————————————————
Base Class Members | Access properties in derived classes
———————————————————————————————————
Private Members | Not accessible
———————————————————————————————————
Public Members | Public
———————————————————————————————————
Protect Members | Protection
———————————————————————————————————
Private inheritance
———————————————————————————————————
Base Class Members | Access properties in derived classes
———————————————————————————————————
Private Members | Not accessible
———————————————————————————————————
Public Members | Private
———————————————————————————————————
Protect Members | Private
———————————————————————————————————
Protect inheritance
———————————————————————————————————
Base Class Members | Access properties in derived classes
———————————————————————————————————
Private Members | Not accessible
———————————————————————————————————
Public Members | Protection
———————————————————————————————————
Protect Members | Protection
———————————————————————————————————
10, friend Yuan
(1) Meaning of friend:
Declaring an external function or a member function of another class in a class
is a friend, this function can access the private data of this class
(2) Statement of Friend
Declaring an external function fanction_name as a friend when the class is defined
friend void Fanction_name (Class_name &);
Declare class_name_1 member function Fanction_name as friend when Class_name class is defined
friend void Class_name_1::fanction_name (Class_name &);
Review of C + + classes