Default member functions for classes and objects

Six default functions are automatically generated when all classes create objects.
But sometimes the default function can not meet the needs, you can create the corresponding function to replace the default function.

Initialization and Cleaning: Constructors
             Destructor
 Copy copy: assignment overload function
             copy constructor
 Address overloading: common object address overloading
             const object address overloading

I. Constructor

  • The function name is the same as the type, and there is no return value type.
  • Ensure that each member variable has an initial value. However, the default constructor assigns a random initial value to the variable.
  • The constructor is automatically called when the object is instantiated.
  • Constructors can be overloaded.
  • If there is a display constructor in the class, the compiler default constructor is not called.
class date {
public:
	date(int year, int mouth, int day)
	{
		_year = year;
		_mouth = mouth;
		_day = day;
	}
	date()
	{}
	private:
	int _year;
	int _mouth;
	int _day;
};
int main(){
	date d1(1999,06,11);//Calling a parametric constructor
	date d2;//Call the parametric constructor. Membership variables are random values.
}

2. Destructive function

Complete the cleanup of some resources of the class. It's not destruction. The destruction task is done by the compiler.

  • Destructor name is ~ + class name
  • No parameter, no return value
  • A class has and only has one destructor. If no display definition is present, the default destructor is called.
  • At the end of the object life cycle, the compiler calls automatically.
typedef int DataType;
class SeqList {
public:
	SeqList(int capacity = 10) {
		_pData = (DataType*)malloc(capacity * sizeof(DataType));
		assert(_pData);
		_size = 0;
		_capacity = capacity;
	}
	~SeqList() {
		free(_pData);
		_pData = nullptr;
		_size = 0;
		_capacity = 0;
	}
private:
	int * _pData;
	size_t _size;
	size_t _capacity;
};

3. Copy constructor

  • Is the overloaded form of the constructor.
  • There is only one parameter and reference parameters must be used, modified with const. Infinite recursion can be caused by the use of value passing.

Consideration: Reasons for infinite recursion: Passage of data generates temporary variables in the form of copy construction, which leads to infinite recursion.

class date {
public:
	date(const date&d) {
		_year = d._year;
		_mouth = d._mouth;
		_day = d._day;
	}
	private:
	int _year;
	int _mouth;
	int _day;
};
	
  • If no definition is displayed, the copy structure is a shallow copy. In some cases, resource crashes occur.

Shallow copy: Copy objects in bytes by memory storage
Solution:
Update later.

IV. operator overloading

In order to enhance code readability, operator overloading is introduced in c++.

data d1+d2; //If + is not overloaded, it cannot be compiled

Operator overloading: Functions with special function names, parameters, and return value types.
Function prototype: Optor operator of return value type (parameter type);
When overloading, attention should be paid to:

  • The meaning of the original operator cannot be changed.
  • Can't overload meaningless symbols
  • *::: sizeof?:. cannot be overloaded
    All operators are overloaded in the previous blog, and date class operators are overloaded.
	date& operator=(const date& d) {  //= Heavy Haul
		_year = d._year;
		_mouth = d._mouth;
		_day = d._day;
		return *this;
	}
	date operator+(int days) {// + heavy haul
		date temp(*this);
		int sum = days + _day;
				if (sum <= getmouthday(temp._year, temp._mouth)) {
					temp._day = sum;
					return temp;
				}
				else {
					while (sum > getmouthday(temp._year, temp._mouth))
					{
						sum -= getmouthday(temp._year, temp._mouth);
						if (temp._mouth == 12) {
							temp._mouth = 1;
							temp._year++;
						}
						else {
							temp._mouth++;
						}
					}
					temp._day = sum;
					return temp;
				}
	}

5. const members and address fetching operator overloading

1.const Modified Membership Functions
Indicates that the object indicated by its implicit parameter this pointer cannot be modified.

  • Within the const member function, other non-const member objects cannot be invoked.
void Display( ) const{
    cout << _year << endl ;
    // _ year ++; Errors will be reported and member variables cannot be modified
 }

2. Objects modified by const

  • Unable to modify object member variables
  • Unable to call non-const type member functions
const date d1;

3. Reload Address and const Address Operators
These two overloads generally do not need to be explicitly given, and the compiler's default generation is sufficient.

class date {
public:
	date* operator&() {
		return this;
	}
	const date* operator&()const {
		return this;
	}
	private:
	int _year;
	int _mouth;
	int _day;
};

Posted by ternto333 on Tue, 24 Sep 2019 01:36:38 -0700