C + + classes and objects

Keywords: C Linux Operation & Maintenance

1, Class
Constructor: the constructor is a special member function with the same name as the class name. It is automatically called by the compiler when creating a class type object to ensure that each data member has an appropriate initial value and is called only once in the life cycle of the object.
Characteristics of constructors: constructors are special member functions. It should be noted that although the name of constructors is called constructors, it should be noted that the main task of constructors is not to create objects in open space, but to initialize objects. If the constructor is not explicitly defined in the class, the C + + compiler will automatically generate a parameterless default constructor. Once the user explicitly defines the constructor, the compiler will no longer generate it. Both parameterless constructors and fully default constructors are called default constructors, and there can only be one default constructor.

  1.The function name is the same as the class name.
  2.No return value.
  3.When an object is instantiated, the compiler automatically calls the corresponding constructor.
  4.Constructors can be overloaded.

Destructor: Contrary to the constructor function, the destructor does not complete the destruction of objects, and the destruction of local objects is completed by the compiler. When the object is destroyed, it will automatically call the destructor to clean up the resources of the class.
Characteristics of destructors: destructors are special member functions.

 1.The destructor name is preceded by a character in the class name ~. 
 2.No parameter, no return value.
 3.A class has only one destructor. If not explicitly defined, the system automatically generates a default destructor.
 4.At the end of the object lifecycle, C++The compiler system automatically calls the destructor

Copy constructor: there is only a single formal parameter, which is a reference to an object of this class type (commonly used const decoration). It is automatically called by the compiler when creating a new object with an existing class type object.
**Copy constructor feature: * * copy constructors are also special member functions

1. The copy constructor is an overloaded form of the constructor.
2. There is only one parameter of the copy constructor, and the parameter must be passed by reference. Using the value passing method will cause infinite recursive calls.
3. If the definition is not displayed, the system generates a default copy constructor. The default copy constructor object completes the copy in byte order according to memory storage. This copy is called shallow copy, or value copy.

Deep copy:

class String
{
public:
	String(const char* string = "")
	{
		int len = strlen(string) + 1;
		m_data = (char*)malloc(strlen(string) + 1);
		strcpy(m_data, string);
	}
	String(const String& s)  //Deep copy
	{
		m_data = (char*)malloc(strlen(s.m_data) + 1);
		strcpy(m_data, s.m_data);
	}
	//Release the current space before applying for space
	//This also belongs to abnormally unsafe code
	String& operator=(const String& s)
	{
		if (this != &s)
		{
			free(m_data);  //Free up currently managed space
			m_data = (char*)malloc(strlen(s.m_data) + 1);
			strcpy(m_data, s.m_data);
		}
		return *this;
	}
	~String()
	{
		free(m_data);
		m_data = nullptr;
	}
public :
	void Show()const
	{
		cout << m_data << endl;
	}
private:
	char* m_data;  //If the class is a pointer member, it must be used. Both deep copy and deep assignment functions must be overridden
};

void main()
{
	char str[] = "abcxyz";
	cout << strlen(str) << endl;
	cout << sizeof(str) << endl;

	String s("abcef");
	s.Show();
	String s1 = s;
	String s2;
	s2 = s1;   //Equivalent to s2 operator=(s1)
}

Assignment operator overloading: in order to enhance the readability of the code, C + + introduces operator overloading. Operator overloading is a function with a special function name, as well as its return value type, function name and parameter list. Its return value type and parameter list are similar to ordinary functions.
The function name is: the keyword operator followed by the overloaded operator symbol.
Function prototype: return value type operator operator (parameter list)
be careful:

1.You cannot create a new operator by concatenating other symbols: for example operator@ 
2.Overloaded operators must have an operand of class type or enumeration type 
3..* ,:: ,sizeof ,?: ,. Note that the above five operators cannot be overloaded. This often appears in written multiple-choice questions.
class Complex
{
	
public:
	Complex(int real=0,int imag=0):m_real(real),m_imag(imag)
	{}
	~Complex()
	{}
public:
	friend Complex operator+(int value, const Complex& x);  //Friends must give true parameters
	friend ostream& operator<<(ostream& out, const Complex& x);
public:
	Complex operator+(const Complex& c)
	{
		return Complex(m_real + c.m_real, m_imag + c.m_imag);
	}
	Complex operator-(const Complex& c)
	{
		return Complex(m_real - c.m_real, m_imag + c.m_imag);
	}
public:
	Complex operator+(int value)
	{
		return Complex(m_real + value, m_imag );
	}
private:
	int m_real;
	int m_imag;
};
Complex operator+(int value, const Complex& x)
{
	return Complex(value + x.m_real, x.m_imag);
}
ostream& operator<<(ostream& out, const Complex& x)
{
	out << "(" << x.m_real << "," << x.m_imag << ")";
	return out;
}
void main()
{
	Complex c1(1, 2);
	Complex c2(3, 4);
	Complex x = c1 + c2;
	Complex y = c1 + 10;

	cout << c1 << endl;
}

Const member: the class member function modified by const is called const member function. Const modifies the class member function and modifies the implicit this pointer of the member function, indicating that no member of the class can be modified in the member function.

class test
{
public:
	//Constructor
	test(int data = 0)
	{
		m_data = data;
	}
	//copying functions 
	test(const test& t)
	{
		m_data = t.m_data;
	}
	//assignment
	test& operator=(const test& t)
	{
		if (this != &t)
		{
			m_data = t.m_data;
		}
		return *this;
	}
	//Destructor
	~test()
	{}
```public:
	void SetData(int data)
	{
		m_data = data;
	}
	//int GetData(const Test * const this) with const becomes a constant and cannot be modified
	int GetData() const // Constant method
	{
		return m_data;
	}
	//int GetData(Test *const this) can exist simultaneously. Overloading occurs
	int GetData()
	{
		return m_data;
	}
private:
	int m_data;
};

void main()
{
	Test t;  //Constant methods ordinary objects can be called. Constant objects can only call constant methods
	cout << "t=" << t.GetData() << endl;
	t.SetData(100);
	cout << "t=" << t.GetData() << endl;
}

Posted by siobhan on Tue, 23 Nov 2021 00:53:15 -0800