[C++] class and object and this pointer

table of Contents

One, the introduction of classes

1. The introduction of classes

Second, the definition of the class

Three, the access qualifier of the class

Fourth, the scope of the class

Five, the instantiation of the class

Six, the size of the class object (the number of bytes occupied) and storage method

1. How to calculate the size of the class object

2. The storage method of class objects

Seven, this pointer

1. Reference to this pointer

2. The characteristics of this pointer


One, the introduction of classes

1. The introduction of classes

  •  C language is a process-oriented language, focusing on the process , analyzing the steps to solve the problem, and gradually solving the problem through function calls.
  • C++ is an object-oriented programming language. It focuses on objects . It splits one thing into different objects and completes it by the interaction between objects.

Comparison of project organization between C language and C++:

Object-oriented programming has no advantage in code execution efficiency. Its main purpose is to facilitate programmers to organize and manage code, quickly sort out programming ideas, and bring innovations in programming ideas.


Second, the definition of the class

class Student{
public:
    //成员变量
    char *name;
    int age;
    float score;
public:
    //成员函数
    void say(){
        cout<<name<<"的年龄是"<<age<<",成绩是"<<score<<endl;
    }
};

【Notes】

  • Class is the keyword to define the class , Student is the name of the class , and the content in the curly brackets 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 commonly used class definition methods:

① All declarations and definitions are placed in the class body. [Note] If a member function is defined in a class, the compiler may treat it as an inline function

②The declaration is placed in the header file .h, and the class definition is placed in the .cpp file (recommended method ②)


Three, the access qualifier of the class

C++ realizes the encapsulation method: 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 permissions.

Access qualifiers: public (public), protected (protected), private (private)

[Description of access qualifiers]

1) Public modified members can be directly accessed outside the class

2) protected and private modified members cannot be directly accessed outside the class

3) The scope of access rights starts from the position where the access qualifier appears to the next access qualifier.

4) The default access permission of class is private and struct is public (because struct is compatible with C language)

[Note] Access qualifiers are only useful at compile time. After data is mapped to memory, there is no difference in access qualifiers.


Fourth, the scope of the class

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

class Student{
private:
    //成员变量
    char *name;
    int age;
    float score;
public:
    //成员函数
    void say();
   
};

//指定say()属于Student这个类域
void Student::say() {
    cout << name << "的年龄是" << age << ",成绩是" << score << endl;
}

Five, the instantiation of the class

The process of creating an object with a class type is called instantiation of the class.

【Notes】

1. A class is just a model , which defines which members are in the class. Defining a class does not allocate actual memory space to store it.

2. A class can instantiate multiple objects. The instantiated objects occupy actual physical space and store the member variables of the class .

3. The instantiation process of a class can be compared to building a house through architectural design drawings. The class is the design drawing and the object is the house.


Six, the size of the class object (the number of bytes occupied) and storage method

1. How to calculate the size of the class object

#include <iostream>
using namespace std;

//类中什么都没有——空类
class A1 {

};

//类中只有成员函数
class A2 {
public:
	void f2();
};

//类中有成员变量和成员函数
class A3 {
private:
	int a;
public:
	void f2();
};

int main() {
	cout << "类A1大小:" << sizeof(A1) << endl;
	cout << "类A2大小:" << sizeof(A2) << endl;
	cout << "类A3大小:" << sizeof(A3) << endl;
}

operation result:

in conclusion:

The size of a class is actually the sum of the "member variables" in the class (memory alignment is required); the size of the empty class is 1 byte, and the compiler gives the empty class one byte to uniquely identify this class.

2. The storage method of class objects

Each object only saves member variables, and member functions are stored in a common code segment.


Seven, this pointer

1. Reference to this pointer

#pragma once
#include <iostream>
using namespace std;

class Student {
public:
    //成员变量
    string name;
    int age;
    float score;
public:
    //成员函数
    void setData(string name, int age, float score) {
        this->name = name;
        this->age = age;
        this->score = score;
    }
    void say();
};

void Student::say() {
    cout << name << "的年龄是" << age << ",成绩是" << score << endl;
}


int main() {
    Student stu1,stu2;
    stu1.setData("Li hua", 18, 90);
    stu2.setData("Li Lin", 19, 50);
    stu1.say();
    stu2.say();
}

operation result:

There is a problem with the above categories:

There are two member functions, setData and say, in the Student class. There is no distinction between different objects in the function body. So when stu1 calls the setData function, how does the function know that the stu1 object should be set instead of the stu2 object?

Explanation:

This problem is solved in C++ by introducing this pointer. That is: the C++ compiler adds a hidden pointer parameter to each "member function", so that the pointer points to the current object (the object that calls the function when the function is running), and the operations of all member variables in the function body are Go through the pointer to visit. 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. The characteristics of this pointer

1) The type of this pointer: class type * const

2) Can only be used inside member functions

3) The essence of this pointer is a formal parameter of a member function. When an object calls a member function, the object address is passed to the this parameter as an actual parameter, so the this pointer is not stored in the object.

4) This pointer is the first implicit pointer parameter of the member function. Generally, the compiler will automatically pass it through the ecx register, and the user does not need to pass it.

 

 

Guess you like

Origin blog.csdn.net/Jacky_Feng/article/details/109403346