Elementary C++–Classes and Objects (3) (Illustration)

Article directory

  • Let’s talk about the constructor again
    • initialization list
    • implicit type conversion
    • explicit keyword
  • static member
  • Friend class
  • inner class
  • anonymous object
  • Some optimizations when copying functions

Let’s talk about the constructor again

In our previous constructor, the compiler will give a suitable initial value to each member of the object through the constructor, but this cannot be called initialization, just assignment;< strong>Because initialization can only be initialized once, and assignments can be made countless times inside the constructor;

Initialization list

The initialization list is a syntactic feature used to initialize class member variables in the constructor.
Through the initialization list, member variables can be initialized to specified values when the object is created instead of assigning values one by one in the constructor body.

Syntax: The initialization list follows the constructor parameter list with a colon (:) and before the constructor body.
In the initialization list, member variables are listed in declaration order and separated by commas.
The initialization of each member variable is completed by the member variable name followed by a parentheses and an initial value or by calling other constructors.

test:

class A
{<!-- -->
private:
int _a;
public:
    A(int a = 1)
:_a(a)
{<!-- -->
}
/*A(int* a)
:_a(a)
{
}*/
A(const A & a)
{<!-- -->
_a = a._a;
cout << "A(const A & amp; a)" << endl;
}
~A()
{<!-- -->
cout << "~A()" << endl;

}
};

class Date
{<!-- -->
private:
int _year;
int _month;
int _day;
int & _def;
const int _n;
public:
Date(int year, int month = 1, int day = 1)
:_year(year),
_month(month),
_day(day),
_def(year),
_n(10)
\t\t
{<!-- -->
}
};
int main()
{<!-- -->
Date d1=2023;
Date d2 = (2023, 11, 4);

Date d3 = {<!-- --> 2023, 11, 4 };
return 0;
}

Test const and reference members:

Date member variables


Constructor of Date class

Define an object and initialize it to (2023, 10, 23);

Reference and const variables must be initialized;

Standard writing:


When there is a custom type in the member variable:


Order of members:

If the initialization list is not complete:

If the custom type does not have a default constructor:

For class objects with custom type member variables, you can:


Implicit type conversion





When multiple parameters



explicit keyword

This is a modifier used to modify the constructor of a class. When a constructor is explicitly modified, it will be marked as an explicit constructor, which means that the constructor cannot perform implicit type conversion.


static members

Static class members: Static class members are member variables shared by all objects of the class. Once they are declared as static members, only one copy exists in memory, and that exists before any object is instantiated. Static class members must be initialized outside the class.
Static member functions: Static member functions do not operate on a specific object. They do not have this pointer and can be accessed directly through the class name.

class A
{<!-- -->
public:
A()
{<!-- -->
count + + ;
}
A(const A & a1)
{<!-- -->
count + + ;
}
~A()
{<!-- -->
cout << "~A()" << endl;
}
//Call the function of count member variable
//Static member function, features: no this pointer
static int GetCount()
{<!-- -->
return count;
}
private:
//statement
static int count;
};
//definition
int A::count = 0;

Friend class

Friend classes describe the special relationship between two classes;
When a class is a friend class of another class, the class can access the private members of the other class.
A friend class is declared by using the friend keyword in the class declaration.

class Time
{<!-- -->
public:
friend class Date;
//Declare the Date class in Time, and you can directly access the member variables of Time in Date
Time(int hour=1,int mintue=0,int sec=0)
:_hour(hour),
_mintue(mintue),
_sec(sec)
{<!-- -->}
private:
int _hour;
int _mintue;
int _sec;
};

class Date
{<!-- -->
private:
int _year;
int _month;
int _day;
Time_t;
public:
Date(int year, int month, int day)
:_year(year),
_month(month),
_day(day)
{<!-- -->

}
void SetTime(int hour, int minute, int sec)
{<!-- -->
//You can directly access private member variables
_t._hour = hour;
_t._mintue = minute;
_t._sec = sec;
}
};

Inner class

An inner class is a class defined inside another class. The inner class can access all members of the outer class, including private members, but the outer class cannot directly access the members of the inner class.

class A
{<!-- -->
private:
static int k;
int h;
public:
class B//B is born a friend of A
{<!-- -->
public:
void f(const A & a)
{<!-- -->
cout << k << endl;//k is a static member and can be used directly
cout << a.h << endl;//h is a non-static member and requires a clear class object
}
};


~A()
{<!-- -->
cout << "~A()" << endl;
}
};

int A::k = 1;

int main()
{<!-- -->
A::B b;//B needs to add the scope character in class A
\t
b.f(A());//A() is an anonymous object
return 0;
}

Anonymous object

Anonymous objects are objects without specific names that are created directly during use.
They are usually used to temporarily execute a method or operation, or as the return value of a method.

Syntax: ClassName();

Example:

class Foo()
{<!-- -->
public:
    void display()
    {<!-- -->
        cout<<"this is a Foo"<<endl;
    }
}
int main()
{<!-- -->
    Foo().display();
}

Some optimizations when copying functions

class A
{<!-- -->
private:
int _a;
public:
A(int a=1)
:_a(a)
{<!-- -->
cout << "A(int a)" << endl;
}
A(const A & a)
{<!-- -->
cout << "A(const A & amp; a)" << endl;
}
A & amp; operator=(const A & amp; a)
{<!-- -->
cout << "A & amp; operator=(const A & amp; a)" << endl;
if (this != & amp;a)
{<!-- -->
_a = a._a;
}
return *this;
}
~A()
{<!-- -->
cout << "~A()" << endl;
}
};

 void func1(A aa)
{<!-- -->
cout << "func()" << endl;
}

A func2()
{<!-- -->
A aa;
return aa;
}

A func3(A aa)
{<!-- -->
\t
return aa;
}