[C++ Series P3] 'Classes and Objects' - Trilogy - [Basic Knowledge] (1/3)

foreword

  • Hello everyone, welcome to the YY drop C++ series, a warm welcome!
  • The main content of the outline of [ 'Class and Object'-Trilogy ] is as follows :

  • As the title indicates, this chapter is the first chapter in the three chapters of [ 'Classes and Objects'-Trilogy ] - the basic knowledge chapter . The main contents are as follows:

Table of contents

1. This pointer

1. The essence of the compiler's handling of the This pointer - it is not allowed to modify this, but it is allowed to modify the value pointed to by this 

2. This cannot be passed in formal parameters and actual parameters, but it can be displayed and used inside the function 

3. Example comparison: when a null pointer is passed in, the operation of this

Two. explicit keyword 

1. Basic properties

2. Relevant knowledge supplement: implicit type conversion

1. Why can't implicit type conversion occur when adding "reference" 

3. auto keyword

Four. Friends

1. Friend function

2. Friend class 


1. This pointer

  • The This pointer is essentially a formal parameter , so the this pointer exists in the stack frame of the function call just like ordinary parameters

1. The essence of the compiler's handling of the This pointer - it is not allowed to modify this, but it is allowed to modify the value pointed to by this 

void Print(Date* const this)
{
	cout << this->_year << "-" << this->_month << "-" << this->_day << endl;
}

2. This cannot be passed in formal parameters and actual parameters, but it can be displayed and used inside the function 

void Print()
{
   // this不能在形参和实参显示传递,但是可以在函数内部显示使用
   //this = nullptr;
   cout << this << endl;
   cout << this->_year << "-" << _month << "-" << _day << endl;
}

3. Example comparison: when a null pointer is passed in, the operation of this

Important note: p->Print ( ) is not a dereference operation ! ! ! 

Note: the address of Print is not in the object

Two. explicit keyword 


1. Basic properties

Modifying the constructor with explicit will prohibit the implicit conversion of the constructor 

Code demo:

class Date
{
public:
 // 1. 单参构造函数,没有使用explicit修饰,具有类型转换作用
 // explicit修饰构造函数,禁止类型转换---explicit去掉之后,代码可以通过编译
 explicit Date(int year)
 :_year(year)
 {}
 /*
 // 2. 虽然有多个参数,但是创建对象时后两个参数可以不传递,没有使用explicit修饰,具有类型转
换作用
 // explicit修饰构造函数,禁止类型转换
 explicit Date(int year, int month = 1, int day = 1)
 : _year(year)
 , _month(month)
 , _day(day)
 {}
 */
   Date& operator=(const Date& d)     拷贝构造
   {
   if (this != &d)
   {
   _year = d._year;
   _month = d._month;
   _day = d._day;
   }
   return *this;
   }
private:
 int _year;
 int _month;
 int _day;
};

void Test()
{
 Date d1(2022);

 // 用一个整形变量给日期类型对象赋值
 // 实际编译器背后会用2023构造一个无名对象,最后用无名对象给d1对象进行赋值
 d1 = 2023;
}

2. Relevant knowledge supplement: implicit type conversion

Type conversions result in temporary variables/objects

PS: Constructors can not only construct and initialize objects, but also have the function of   type conversion for a single parameter or a constructor with default values ​​except for the first parameter.

Graphic: 


1. Why can't implicit type conversion occur when adding "reference" 

PS: Permission knowledge points are involved———const knowledge points can be viewed through the portal【】❀❀❀❀

Graphic: 


3. auto keyword

【to be added】


Four. Friends


1. Friend function


A friend function can directly access the private members of a class. It is an ordinary function defined outside the class and does not belong to any class , but it needs to be declared inside the class . The keyword friend needs to be added when declaring

illustrate:

  • Friend functions can access private and protected members of a class, but not member functions of the class
  • Friend functions cannot be modified with const
  • Friend functions can be declared anywhere in the class definition, not restricted by class access qualifiers
  • A function can be a friend function of multiple classes
  • The principle of calling a friend function is the same as that of a normal function
  • The declaration of a friend function is different from a function declaration, it only expresses the authority

Code demo:

class Date
{                       //友元函数声明——表达一种权限(函数可以访问类内对象)
 friend ostream& operator<<(ostream& _cout, const Date& d);
 friend istream& operator>>(istream& _cin, Date& d);

public:
 Date(int year = 1900, int month = 1, int day = 1)
 : _year(year)
 , _month(month)
 , _day(day)
 {}
private:
 int _year;
 int _month;
int _day;
};

 ostream& operator<<(ostream& _cout, const Date& d)
{
  _cout << d._year << "-" << d._month << "-" << d._day;
  return _cout;
}
 istream& operator>>(istream& _cin, Date& d)
{
  _cin >> d._year;
  _cin >> d._month;
  _cin >> d._day;
  return _cin;
}

int main()
{
 Date d;
 cin >> d;
 cout << d << endl;
 return 0;
}

2. Friend class 

illustrate:

  • All member functions of a friend class can be friend functions of another class , and can access non-public members of another class.
  • The friendship relationship is one-way and not exchangeable.

Example: For example, the following Time class and Date class, if the Date class is declared as a friend class in the Time class, then you can directly access the private member variables of the Time class in the Date class, but you want to access the private member variables in the Date class in the Time class Member variables are not.

  • The friendship relationship cannot be transmitted  (if B is a friend of A and C is a friend of B, it cannot be explained that C is a friend of A)
  • Friend relationships cannot be inherited ( details are introduced in the inheritance section)

Code demo: 

class Time
{
    friend class Date; 
// 声明日期类为时间类的友元类
//则在日期类中就直接访问Time类中的私有成员变量
public:
    Time(int hour = 0, int minute = 0, int second = 0)
        : _hour(hour)
        , _minute(minute)
        , _second(second)
    {}
private:
    int _hour;
    int _minute;
    int _second;
};
class Date
{
public:
    Date(int year = 1900, int month = 1, int day = 1)
        : _year(year)
        , _month(month)
        , _day(day)
    {}
    void SetTimeOfDate(int hour, int minute, int second)
    {
        // 直接访问时间类私有的成员变量
        _t._hour = hour;
        _t._minute = minute;
        _t._second = second;
    }
private:
    int _year;
    int _month;
    int _day;
    Time _t;
};

Guess you like

Origin blog.csdn.net/YYDsis/article/details/130894783