Ninth class, default parameter, function overload

Posted by Xasho on Wed, 15 Dec 2021 07:49:54 +0100

Original link (click the original link to get more learning dry goods):

http://blog.bools.cn/archives/1369

1. Class

1. Permission of class

Class is named by class. It can contain some properties (can be understood as variables) and some behaviors (can be understood as functions). Then the class and structure are very similar, and their big difference lies in the access permission.

In the class, the original permission is private, which means private. It means that the defined attributes and i behaviors can only be used in the class.

class circle
{
	int m_r;
	double countZC()
	{
		return 2 * pi * m_r;
	}
};
void printf_ZC()
{
	circle c1;
	c1.m_r = 6;//Error, because permissions can only be used inside classes
}

Permission also has public, which means public. Of course, in a class, you can define some public and some private. Another kind of permission is protected, which means protected. The inside and subset of the class can be accessed, but the outside cannot be accessed.

The basic process of a class is

After setting the permission of the class, we can directly use the attribute to the functions in the class without calling. If it is set to public, we can also use functions to set properties.

class circle
{
public:
	int m_r;
	double countZC()
	{
		return 2 * pi * m_r;
	}
};
void printf_ZC()
{
	circle c1;
	c1.m_r = 6;
	double girth_r = c1.countZC();
	cout << "Perimeter:" << girth_r << endl;
}

 

 

2. Inline function

In C + +, the concept of predefined macro is implemented by inline function, and the inline function itself is also a function. Inline functions have the properties and all behaviors of ordinary functions. Unlike ordinary functions, inline functions will expand like predefined macros in appropriate places, so there is no cost of function calls. Therefore, inline functions should be used instead of macros.

Add the inline keyword in front of the ordinary function to make it an inline function, but note that the function body and declaration must be combined, otherwise it is still an ordinary function

inline void test(int x);
inline void test(int x)
{
	cout << x << endl;
}

Inline functions do take up space, but the advantage of inline functions over ordinary functions is that they save the overhead of stack pressing, jump and return when calling functions. We can understand it as space for time when inlining functions.

The function defined inside the class will automatically become an inline function (no wonder it runs so slowly)

Because the compiler has some limitations when using space for time when inline functions, it is suggested that the compiler may not treat functions as inline functions in the following cases.

1. There cannot be any form of circular statement

2. There cannot be too many conditional judgment statements

3. The function body should not be too large

4. The function cannot be addressed

In a word: inline function is to solve the problem of macro defects.

2. Default parameters

The default parameter is to give a value directly to the parameter of the calling function

void func(int a = 10, int b = 20)
{
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
}

When calling a function, of course, you can also assign a value, but the default value will be overwritten.

Note here that if a location has a default parameter, the default parameter must be right from left to right behind the location

void func2(int a, int b = 10, int c = 20)
{
	cout << "a+b+c=" << a + b + c << endl;
}

When a function is called, it sometimes needs to be declared, and only one of the declaration and definition can be defined by the default parameter, such as

	void func3(int a = 10, int b = 30);
    void func3(int a, int b)
    {
    	cout << "a+b=" << a + b << endl;
    }

3. Function overloading

In C + +, function names can be repeated, but they must be in the same scope and have the same function name

The condition of overloading is that the number of parameters of a function is different, or the type or order is different, and the return value is different, which cannot be used as the condition of function overloading.

void func4(int a)
{
	cout << "have int Overloaded function of parameter" << endl;
}
void func4(int a, int b)
{
	cout << "There are two int Overloaded function with arguments of type" << endl;
}
void func4(float a, float b)
{
	cout << "There are two float Overloaded function of type" << endl;
}
void func4(int b, int a)
{
	cout << "Overloaded functions in different order" << endl;
}

However, when calling functions, you should pay attention to ambiguity

	
void func4(int a, int b)
{
	cout << "There are two int Overloaded function with arguments of type" << endl;
}
void func4(int b, int a)
{
	cout << "Overloaded functions in different order" << endl;
}
    func4();
	func4(a);
	func4(a,b);//There is ambiguity
	func4((float)a, (float)b);

Overloaded functions can also be referenced as conditions

void func4(const int& a)
{
	cout << "const Can be used as a condition for function overloading" << endl;
}

 

 

 

Topics: C#