Classes and objects in C++ 1

1 Preliminary understanding of process-oriented and object-oriented

The C language is process-oriented, focusing on the process, analyzing the steps to solve the problem, and gradually solving the problem through function calls.
C++ is based on object-oriented, focusing on objects, splitting a thing into different objects, and completing it by the interaction between objects.

Type 2

2.1 The introduction of classes

In C language, only variables can be defined in the structure. In C++, not only variables can be defined in the structure, but also functions can be defined.

struct Student
{
    
    
 void SetStudentInfo(const char* name, const char* gender, int age)
 {
    
    
 strcpy(_name, name);
 strcpy(_gender, gender);
 _age = age;
 }
 
 void PrintStudentInfo()
 {
    
    
 cout<<_name<<" "<<_gender<<" "<<_age<<endl;
 }
 
 char _name[20];
 char _gender[3];
 int _age;
};

int main()
{
    
    
 Student s;
 s.SetStudentInfo("Peter", "男", 18);
 return 0;
 }

In the definition of the above structure, I prefer to use class instead of C++.

2.2 Class definition

class className
{
    
    
 // 类体:由成员函数和成员变量组成
 
}; // 一定要注意后面的分号

Class is the keyword to define the class, ClassName is the name of the class, {} is the main body of the class, pay attention to the semicolon after the end of the class definition.
The elements in the class are called the members of the class: the data in the class are called the attributes or member variables of the class; the functions in the class are called the methods or member functions of the class.

Two definition formats of class

  1. Declarations and definitions are all placed in the class body. Note: If a member function is defined in the class, the compiler may treat it as an inline function
    .
  2. The declaration is placed in the .h file, and the class definition is placed in the .cpp file.
    Insert picture description here
    Under normal circumstances, it is more desirable to adopt the second method.

2.3 Scope of the class

The class defines a new scope, and all members of the class are in the scope of the class. To define a member outside the class body, you need to use the :: scope resolver to
specify which class domain the member belongs to.

class Person
{
    
    
public:
 void PrintPersonInfo();
private:
 char _name[20];
 char _gender[3];
 int _age;
};
// 这里需要指定PrintPersonInfo是属于Person这个类域
void Person::PrintPersonInfo()
{
    
    
 cout<<_name<<" "_gender<<" "<<_age<<endl; }

2.4 instantiation of classes

The process of creating an object with a class type is called instantiation of the class.
1. A class is just a model-like thing, which limits the members of the class, defines a class and does not allocate actual memory space to store it.
2. A class can instantiate multiple objects, and the instantiated objects occupy the actual physical Space, * only stores class member variables
3 For example. Class to instantiate an object like a real use architectural plans to build a house, like a class design, only need to design what
what what, but the building did not exist entity, the same category is only a design example of a Can actually store data and occupy
physical space
Insert picture description here

2.5 Class access qualifiers and encapsulation

2.5.1 Access qualifier

C++ realizes the way of encapsulation: Use classes to combine the properties and methods of the object to make the object more complete, and selectively
provide its interface to external users through access rights .
Access qualifier

  • public
  • protected
  • private

Description of access qualifiers

  • Members modified by public can be accessed directly outside the class
  • Protected and private modified members cannot be directly accessed outside the class (protected and private are similar here)
  • The scope of access rights starts from where the access qualifier appears until the next access qualifier appears
  • The default access permission of class is private, and struct is public (because struct is compatible with C).
    Note: The access qualifier is only useful at compile time. After the data is mapped to memory, there is no difference in access qualifier.

What is the difference between struct and class in C++?
C++ needs to be compatible with the C language, so struct in C++ can be used as a structure. In addition, struct in C++ can also be used to define classes.
It is the same as class which defines a class. The difference is that the default access method of struct members is public, and the default access method of class is struct members
is private.

2.5.2 Package

Three characteristics of object-oriented: encapsulation, inheritance, and polymorphism.
In the class and object stage, we only study the encapsulation characteristics of the class, so what is encapsulation?
Encapsulation: Combine data and methods of manipulating data organically, hide the properties and implementation details of the object, and only expose the interface to the outside to
interact with the object .
Encapsulation is essentially a kind of management: how do we manage the terracotta warriors? For example, if nothing is left, the terracotta warriors will be destroyed at will. So we
first built a house to encapsulate the terracotta warriors. But our purpose is all encapsulated and not let others see it. Therefore, we have opened up the ticket sales
channel, and you can buy tickets to break through the package and enter under a reasonable supervision mechanism. The same is true for classes. We use class data and methods to encapsulate them.
Don't want others to see, we use protected/private to encapsulate the members. Open some shared member functions to reasonably access members
. So encapsulation is essentially a kind of management.

2.6 Calculation of the object size of the class

2.6.1 Storage of class objects

Save only the member variables, member functions stored in a common piece of code
a class size is actually the class "member variables" in and, of course, but also for memory alignment, note the size of an empty class, empty analogy
more special compiler A byte is given to the empty class to uniquely identify the class.

Reference to the memory alignment rules of the structure: https://blog.csdn.net/CZHLNN/article/details/110006248

2.7 this pointer of class member function

2.7.1 Leading out of this pointer

Let's first define a date class Date

class Date
{
    
     
public :
 void Display ()
 {
    
    
 cout <<_year<< "-" <<_month << "-"<< _day <<endl;
 }
 
 void SetDate(int year , int month , int day)
 {
    
    
 _year = year;
 _month = month;
 _day = day;
 }
private :
 int _year ; // 年
 int _month ; // 月
 int _day ; // 日
};
int main()
{
    
    
 Date d1, d2;
 d1.SetDate(2018,5,1);
 d2.SetDate(2018,7,1);
 d1.Display();
 d2.Display();
 return 0; }

For the above classes, there is such a problem:
There are two member functions of SetDate and Display in the Date class. There is no distinction between different objects in the function body. When s1 calls the SetDate function
, how does the function know that the s1 object should be set? Instead of setting the s2 object?
C ++, solve this problem by introducing this pointer, namely: C ++ compiler for each of the "non-static member function" adds pointer reference a hidden
number, so that the pointer to the current object (the object of the function calls the function run-time) , All member variable operations in the function body are accessed through this
pointer. It's just that all operations are transparent to the user, that is, the user does not need to pass it, and the compiler completes it automatically.

2.7.2 The characteristics of this pointer

1 The type of this pointer: class type * const
2 It can only be used inside a "member function".
3 This pointer is actually a formal parameter of a member function. When an object calls a member function, the object address is passed as an actual parameter. this
parameter. So the this pointer is not stored in the object.
4 this pointer is the first member of an implicit function pointer parameter, automatically passes generally through the ecx register by the compiler, the user does not need to
pass

Guess you like

Origin blog.csdn.net/CZHLNN/article/details/113729030