Day5: application of const static, a special member, and this explicit, a friend

const:

① Data member: must be initialized with an initialization member list.

	wbm(string name,const int age):name(name),age(age){}

② Constant member function: 1.const is at the end

                            2. You cannot use this as an interface to modify data members, that is, read-only mode

                            3. It can exist at the same time and with the same name as the ordinary function. Follow the principle of "name for household pair". General - > general. If there is no ordinary function, call the constant function.

class wbm
{
public:
	wbm(string name,const int age):name(name),age(age){}
	void print()const
	{
		cout << "i'm an ordinary hanshu" << endl;
	}
	void print()
	{
		cout << "i'm a normal hanshu" << endl;
	}

protected:
	const int age;
	string  name;
private:
};
int main()
{
	wbm gzj("Wang Ergou", 12);
	gzj.print();
	const wbm jiejie("wbm", 21);
	jiejie.print();
	return 0;
}

  ③ Constant object: only constant functions can be called

  static members:

Pre declaration:

              1. Belonging to a class rather than an object, that is   share.   Call can be called without an object.

(for this common property, we will initialize the member list below   Validate with a data member + +)

              2. However, it will still be limited by the authority. (it will be obvious when:: access is added directly through class name qualification)

① static data member:

        It must be initialized outside the class. When it is outside the class, it only needs type + class name qualification (i.e. no static modification). Then initialization in a class or in an initialization member list is not allowed.

class wbm
{
public:

protected:
	static int age;
	string name;
};
int wbm::age = 100;

  Show me how to call through class:

  Specific embodiment of sharing:

class wbm
{
public:
	wbm(const string name=" ") :name(name)
	{
		age++;//This means that each function constructed will increase the static age by one
	}
	static int age;
protected:
	const string name;
};
int wbm::age = 1;

int main()
{
	cout << wbm::age << endl;//1
	wbm k1("k1");//2
	cout << wbm::age << endl;
	wbm k2("k2");//3
	cout << wbm::age << endl;
	wbm k[3];//6
	cout << wbm::age << endl;
	wbm* k3 = new wbm;//7
	cout << wbm::age << endl;
	return 0;
}

Note that if the default function is added, the function age constructed by default will not be + + (the second way)

Therefore, we usually add initialization to the initialization list for convenience.

Secondly, if you want to implement age + +, you can no longer add int age to the initialization member list; Otherwise, the final age always stays at the initialized age value and will not change (pro test).

As long as the value of the static data member is updated once, it can ensure that all objects will access the updated value!!

② Member function:

        1.static is placed in front of the function name, and the implementation outside the class does not need static modification. Of course, it is also possible to implement it in classes.         

        2. If a non static data member is called, an object must be specified (solution: pass in an object parameter)

        Solution code implementation:

//Class	
static void printdata(const wbm& mm)
	{
		cout <<mm.name<< endl;
	}
//This is used in main
wbm k1("k1");
wbm::printdata(k1);

        3. If static data is called: no requirement.

static void printstatic_data()
	{
		cout << age << endl;
	}

③ static object:

Last release!!!  

Friend  :

① Definition: provides a place to give an object the permission limit to break the class (ignoring the permission)

(of course, data is still accessed through each other's objects)

② Friend function: (it does not belong to a class and cannot directly access members. If it is implemented outside the class, it does not need friend modification and class name qualification) (it is also not allowed to use objects to access friend functions)

1. Put in front of the function name

2. Implementation method 1: if parameters are passed in, they can be implemented in the class and then used directly in main.

//In class:
	friend void print_no_static(const pig&mm)
	{
		cout << mm.age << "\t" << mm.name << endl;
	}
main Medium:
	pig jie ("jie", 18);
	print_no_static(jie);

In fact, it is also possible to pass in parameters outside the class

(in the class, it is equivalent to declaring.)

Implementation method 2: if no parameters are passed in and implemented in a class, it cannot be used directly in main, and the function needs to be declared in advance.

Pre declaration:

void print_no_static();
class pig
{
public:
	pig(string name, int age) :name(name), age(age){}
	friend void print_no_static()
	{
		cout << height << endl;
	}
	friend void print_static();
protected:
	int age;
	string name;
private:
	static int height;
};
int pig::height = 100;
//void print_no_static(const pig &mm)
//{
//	cout << mm.age << "\t" << mm.name << endl;
//}
void print_static()
{
	cout << pig::height << endl;
}
int main()
{

	pig jie ("jie", 18);
	print_no_static();
	return 0;
}

③ Use a member function of another class as a friend function:

Order:

        Class B

        Class A: friend

        Implementation of class A friend (i.e. B member function)  

Purpose: this function needs to access the private or protected members of another class and perform some operations on them. An example in real life is that teachers need to modify students' grades.

#include<iostream>
using namespace std;

class student;
class teacher
{
public:
	void change_student_grade(student &wbm);
};
class student
{
public:
	student(int grade):grade(grade){}
	friend void teacher::change_student_grade(student &wbm);
	void printGrade()
	{
		cout << "achievement"<<grade << endl;
	}
protected:
	int grade;
private:
};
void teacher::change_student_grade(student &wbm)
{
	wbm.grade = 100;
}
int main()
{
	student wbm(99);
	wbm.printGrade();
	teacher gzj;
	gzj.change_student_grade(wbm);
	wbm.printGrade();
	return 0;
}

④ Friend class:

1. Class A and b: tell me that there is friend class b in class A, then create class a object a in a function in class b (you can also create an object in the function now or pass a reference), then all attributes of object a can be accessed

Purpose: use a single declaration to make all functions of Class Y become friends of class X. it provides a way of cooperation between classes, so that objects of class y can have the functions of class X and class y.

#include<iostream>
using namespace std;

class student;
class teacher
{
public:
	void print(student& mm);
};
class student
{
public:
	friend teacher;
	student(string name, int age) :name(name), age(age)
	{

	}
protected:
	string name;
	int age;
};
void teacher::print(student &mm)
{
	cout << "name=" << mm.name << endl;
	cout << "age=" << mm.age << endl;
}
int main()
{
	teacher jie;
	student wbm("wbm", 18);
	jie.print(wbm);
	return 0;
}

Note: except for friend classes, they no longer have permissions. (it is proved by cout, a return reference object function in main below)

 

class student
{
public:
	friend class teacher;
	student(string name, int age) :name(name), age(age) {}
	
protected:
	string name;
	int age;
};
class teacher
{
public:
	void print(student& mm)
	{
		cout << mm.name << "\t" << mm.age << endl;
	}
	student& returnstudent(student& mm)
	{
		return mm;
	}
};
int main()
{
	student wbm("wbm", 18);
	teacher jie;
	jie.print(wbm);
	//jie.returnstudent(wbm).name << endl;
	//Error: once a friend class is created, it no longer has permissions
	return 0;
}

Supplement: the writing method of mutual friend category

(add methods implemented outside the class on the basis of one-way)

class student
{
public:
	friend class teacher;
	student(string name) :name(name){}
	void printT();
protected:
	string name;
	int age;
};
class teacher
{
public:
	friend class student;
	teacher(string name) :name(name) { }
	void printS()
	{
		student wbm("wbm");
		cout << wbm.name << endl;
	}
protected:
	string name;
};
void student::printT()
{
	teacher jie("jie");
	cout << jie.name << endl;
}
int main()
{
	student mm("mm");
	mm.printT();
	
	teacher gg("jie");
	gg.printS();
	return 0;
}

 explicit:

For the implicit type conversion of the constructor (not to him), that is, it is not initialized with B = {}.

Only explicit: B   b();

this pointer:

The "this pointer" points to the object on which the member function acts

① Avoid the formal parameter name and data member having the same name, and generally refer to the address of the object

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

③ This pointer cannot be used in static member functions (there is no this outside the class)

Next, show the usage of this by writing an interface initdata (it's not easy to distinguish when you don't need to initialize the member list, this can.)

#include<iostream>
using namespace std;

class student
{
public:
	void initdata(string name, int age)
	{
		//Distinction 1: class name qualification
		student::name = name;
		//Distinguishing method 2: this
		this->age = age;
	}
	void print()
	{
		cout << this->age << endl;
	}
	student& returnstudent()
	{
		return *this;
	}
protected:
	int age;
	string name;
};

int main()
{
	student wbm;
	wbm.initdata("wbb", 18);
	wbm.returnstudent().returnstudent().returnstudent().print();
	//Set up a big baby.
	return 0;
}

Keywords: C C++

Added by corillo181 on Sun, 05 Dec 2021 22:33:57 +0200