c + + lesson 5 ~ special members

Posted by 7awaka on Wed, 02 Feb 2022 21:36:28 +0100

catalogue

const member

static member

Friends

this pointer and explicit

const member

  • const data member

const type variables cannot be modified and are in read-only mode

It must be initialized in the form of initialization parameter list

  • const member function

In terms of writing, const is written after the function

A constant member function is a read-only data member that cannot be modified

Constant member functions can exist simultaneously with ordinary functions

Ordinary objects and constant member functions exist at the same time. Ordinary objects call ordinary functions first

Ordinary objects can call constant member functions

//Simultaneous existence
class MM
{
  void print()
	{
		//num = 0;// Error, cannot modify read-only mode
		cout <<"Ordinary function"<< endl;
	}
	void print()const
	{
		//name = "modify"// Cannot modify
		//num = 9;
		cout << "const Member function" << endl;
	}
}
int main()
{
	MM mm("Hello", 11);
	mm.print();

	return 0;
}

  • const object

const decorated object

A constant object can only call constant member functions

#include <iostream>
#include <string>
using namespace std;
class MM
{
public:
	MM(string name, int num) :num(num)
	{
		//MM::num = 0; You cannot directly initialize the parameter list in it
		MM::name = name;//No, you can initialize the parameter list
	}
	void print()
	{
		//num = 0;// Error, cannot modify read-only mode
		cout <<"Ordinary function" << endl;
	}
	//Constant member function
	void print()const
	{
		//name = "modify"// Cannot modify
		//num = 9;
		cout << "const Member function" << endl;
	}
	void print2()
	{
		//num = 0;// Error, cannot modify read-only mode
		cout << "Ordinary function" << endl;
	}
protected:
	string name;
	const int num;//const data member
};
int main()
{
	MM mm("Hello", 11);
	mm.print();//Ordinary objects call ordinary functions
	const MM mm2("Constant object", 111);
	mm2.print();//Constant object calls constant member function
	//mm2.print2();// Error, a constant object can only call constant member functions

	return 0;
}

static member

static members do not belong to objects, but belong to classes, which means that they are common to all objects. They can be called without objects, and they can also be called with objects at the beginning. static members are still limited by permissions

  • static data member

It must be initialized outside the class. For initialization outside the class, static modification is no longer required, but class name qualification is required

Class is incorrectly initialized and cannot be initialized with an initialization parameter list

#include <iostream>
#include <string>
using namespace std;
class MM
{
public:
	MM(string name=" ")
	{
		num++;
	}
protected:
	string name;
public://You need to change the permission to public
	static int num;
	//static int num=0;// Class is incorrectly initialized
};
//For initialization outside the class, static modification is no longer required, but class name qualification is required
int MM::num = 1001;
int main()
{
	//Static members can be accessed without objects
	cout << MM::num << endl;//1001
	//What is mutual ownership?
	MM mm("mm");
	//Static data members can be accessed through objects
	cout << mm.num << endl;//Now num=1002
	MM arr[4];
	MM* p = new MM("newNum");
	cout << MM::num << endl;//1007
	cout << p->num << endl;//1007
	cout << mm.num << endl;//1007
	delete p;
	p = nullptr;


	return 0;
}
  • static member function

Static is written in front of the function, and the object must be specified when calling non static members

#include <iostream>
#include <string>
using namespace std;
class MM
{
public:
	MM(string name=" ")
	{
		num++;
	}
	static void printData();
	static void printData2(MM& mm)
	{
		cout << mm.name << " " << mm.num << endl;
	}
protected:
	string name="Hello";
public://You need to change the permission to public
	static int num;
	//static int num=0;// Class is incorrectly initialized
};
//For initialization outside the class, static modification is no longer required, but class name qualification is required
int MM::num = 1001;
void MM::printData()
{
	//An object must be specified when calling a non static member
	//cout << name << endl;// When this function is called without an object, name has no source
	cout << num << endl;//Static function calls static data, no requirements
	cout << "Non static member" << endl;
}

int main()
{
	//Static members can be accessed without objects
	cout << MM::num << endl;//1001
	//What is mutual ownership?
	MM mm("mm");
	//Static data members can be accessed through objects
	cout << mm.num << endl;//Now num=1002
	MM arr[4];
	MM* p = new MM("newNum");
	cout << MM::num << endl;//1007
	cout << p->num << endl;//1007
	cout << mm.num << endl;//1007
	delete p;
	p = nullptr;
	mm.printData2(mm);
	MM::printData2(mm);
	

	return 0;
}

  • static object

The release is the last

Friends

In the relationship described by friend, the friend only provides a place to give the object the permission to break the class limit (ignoring the permission)

  • friend function

Ordinary friend functions act as friend functions

#include <iostream>
#include <string>
using namespace std;
void printData();
class MM
{
public:
	MM(string name, int age) :name(name), age(age)
	{

	}
	void print()
	{
		cout << name << " " << age << endl;
	}
	friend void printData()//The above declaration is required for the implementation in the friend function class without parameters
	{
		//cout << name << age << endl;// The wrong does not belong to class and cannot access members directly
		MM m2("Ignore authority", 14);
		//Friend functions provide a place for objects to ignore permissions
		cout << m2.name << " " << m2.age << endl;
	}
protected:
	string name;
private:
	int age;
	friend void printData2(MM& mm);
};
//The implementation outside the class does not need friend modification and class name qualification
void printData2(MM& mm)
{
	cout << mm.name << mm.age << endl;
}


int main()
{
	MM mm("beauty", 18);
	mm.print();
	printData2(mm);//Direct call
	printData();

	return 0;
}

Member function of another class

Step: Class B -- > class a -- > friend function of class A (member function of class B)

//The implementation outside the class does not need friend modification and class name qualification
void printData2(MM& mm)
{
	cout << mm.name << mm.age << endl;
}

class B
{
public:
	void printA();

protected:
};
class A
{
public:
	friend void B::printA();
protected:
	string name = "a";
};

//Member function implementation must be the following implementation of another class
void B::printA()
{
	A a;
	cout << a.name << endl;
}
int main()
{
	
	B b;
	b.printA();//Output a
	return 0;
}
  • Friend class
#include <iostream>
#include <string>
using namespace std;

class MM
{
	friend class GG;
public:
	MM(string name, int age) :name(name), age(age)
	{
		
	}

protected:
	string name;
	int age;

};
class GG
{
public:
	void print()
	{
		MM mm("Hello", 14);
		cout << mm.name << " " << mm.age;
	}
	void print2(MM& mm)
	{
		cout << mm.name << " " << mm.age;
	}
	MM& returnMM(MM& mm)
	{
		return mm;
	}
protected:

};
int main()
{
	MM mm("Alalei", 444);
	GG gg;
	gg.print();
	gg.print2(mm);
	//cout<< gg. returnMM(mm) << endl;// Error, there is a friend class and you don't have permission
	return 0;
}

Mutual friendship

#include <iostream>
#include <string>
using namespace std;
//Mutual friendship
class A
{
	friend class B;
public:
	void print();
protected:
	string data = "A";
};

class B
{
public:
	friend class A;
	void print()
	{
		A a;
		cout << a.data << endl;
	}
protected:
	string data = "B";
};
void A::print()
{
	B b;
	cout << b.data << endl;
}
int main()
{
	B b;
	b.print();
	A a;
	a.print();
	return 0;
}

this pointer and explicit

  • explicit modifiers are used by constructors and do not allow implicit conversion constructs
#include <iostream>
using namespace std;

class MM
{
public:
	explicit MM(int age) :age(age)
	{
		cout << age << endl;
	}
protected:
	int age;
};
int main()
{
	//Implicit constructor
	//explicit restriction implicit construction
	MM mm = 111;
	MM mm = 1.23;//Automatically implicitly converts decimals to integers
	return 0;
}
  • this pointer

Avoid the formal parameter name and the data member with the same name, and generally refer to the address of the object. There is no this pointer outside the class

Act as the return value of the function, return the object itself, and use * this to represent the object itself

this pointer cannot be used in static member functions

#include <iostream>
using namespace std;

class MM
{
public:
	 MM(int age) :age(age){}
	 //Normal parameter list does not exist
	// Void initdata (int age): age (age) {} / / wrong writing
	 void initData(int age)
	 {
		 MM::age=age;//The class name is qualified to help the computer identify
		 this->age=age;//this pointer mode
	 }
	 void print()
	 {
		 cout << this->age << endl;
	 }
	 MM returnMM()
	 {
		 return *this;//Returns the object itself
	 }
	 //This is wrong. static has no this pointer
	/*static MM rerurnMM2()
	 {
		cout << this->age << endl;
	 }*/

protected:
	int age;
};
int main()
{
	MM mm(111);
	mm.print();
	mm.initData(222);
	mm.print();
	mm.returnMM().print();
	
	return 0;
}

Implement string

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class mystring
{
public:
	mystring(const char* str=" ")
	{
		strSize = strlen(str) + 1;
		mystring::str = new char[strSize];
		strcpy_s(this->str,strSize, str);
	}
	mystring(const mystring& object)
	{
		strSize = object.strSize;
		str = new char[strSize];
		strcpy_s(str, strSize, object.str);
	}
	char* c_str()
	{
		return str;
	}
	char* data()
	{
		return str;
	}
	mystring append(const mystring& object)
	{
		mystring temp;
		temp.strSize = mystring::strSize + object.strSize-1;
		temp.str = new char[temp.strSize];
		memset(temp.str, 0, temp.strSize);
		strcat_s(temp.str, temp.strSize, str);
		strcat_s(temp.str, temp.strSize, object.str);
		return temp;
	}
	int compare(const mystring& object)
	{
		return strcmp(str, object.str);
	}
	~mystring()
	{
		delete[] str;
		str = nullptr;
	}

protected:
	char* str;
	int strSize;
};


int main()
{
	//1. Implement the creation method in string
	mystring str1;
	mystring str2("ILOVEYOU");
	mystring str3(str1);
	mystring str4 = str2;
	//2. By implementing data and c_str function print string
	cout << str2.c_str() << endl;//Print ILOVEYOU
	cout << str2.data() << endl;//Print ILOVEYOU
	//3. Realize the connection of append string
	mystring strOne = "one";
	mystring strTwo = "two";
	mystring strThree = strOne.append(strTwo);
	cout << strThree.data() << endl;//onetwo
	//Implement string comparison
	cout << strOne.compare(strOne) << endl;//0
	//5. Freeing memory by handwriting constructor



	return 0;
}

Topics: C++ Javascript