Learning Notes in C++ Departure from Port

Learning Notes in C++ Departure from Port

1. C++ Language References

1.1 What is a reference

Lift a chestnut, a person's name is Ro XX, and the nickname is Radish Head

Reference is the alias of a variable
Symbol: &

You cannot have only aliases, references cannot exist alone

1.2 Application of References
// *********************
//	References to basic data types
// *********************
#include <iostream>
using namespace std;
int main(void)
{
	int a = 3;
	int &b = a;   // Reference must be initialized, alias of a is b

	b = 10;   // An operation on an alias is really an operation on itself
	cout << a << endl;
	return 0;
}

// *********************
//	Reference to structure type
// *********************
#include <iostream>
using namespace std;

typedef struct
{
	int x;
	int y;
}Coor;   // Structure Coor Defines Coordinates
int main(void)
{
	Coor c1;
	Coor &c = c1;   // The alias of the c1 structure is c

	c.x = 10;
	c.y = 20;
	cout << c1.x << " " << c1.y << endl;
	return 0;
}
// *********************
//	Reference to pointer type
//  Type*&Pointer Reference Alias=Pointer;
// *********************
#include <iostream>
using namespace std;
int main(void)
{
	int a = 10;
	int *b = &a;   // &Take the address, pointer b points to a
	int *&c = b;  // Reference to pointer, alias of b is c
	*c = 20;

	cout << a << endl;
	return 0;
}
// *********************
//	References as function parameters
// *********************
// C Language
void fun(int *a, int *b)
{
	int c = 0;
	c = *a;
	*a = *b;
	*b = c;
}
int x = 10, y = 20;
fun(&x, &y)

// C++ Language
void fun(int &a, int &b)  // Incoming Parameter Alias
{
	int c = 0;
	c = a;
	a = b;
	b = c;
}
int x = 10, y = 20;
fun(x, y)

3. Keyword Const

Role: Controlling change

// Common data types
	const int x = 5;   // x is constant
// Pointer
	int x = 5;
	const int *p = &x;
	*p =3;  // Error, *p is constant
	x= 3;   // Correct, compilation can pass through

	int x = 3;
	int y = 5;
	int *const p = &x;
	p = &y;  // Error, p can only take an address for x, not point to someone else
	*p =10;  // Correct, assignable, modifiable x value
// Quote
	int x = 3;
	int y = 5;
	int const &z = x;
	z=10;  // Error, cannot assign a constant
	x= 10;  // Correct
// function
#include <iostream>
using namespace std;
void fun(const int &a, const int &b);

int main(void)
{
	int x = 3;
	int y = 5;
	fun(x,y);   // Error, a, b are constants
	
	system("pause");
	return 0;
}

void fun(const int &a, const int &b)
{
	a = 10;
	b = 20;
}

4. New Features of Functions

4.1 Function parameter defaults

Rule: Parameters with default values must be at the rightmost end of the parameter table
Default values are defined when a function is declared and not when a function is defined
No arguments use default values, otherwise override default values with arguments

#include <stdlib.h>
#include <iostream>
using namespace std;

void fun(int i = 30, int j = 20, int k = 10);  // Assignment once from back to front

int main (void)
{
	fun();
	fun(100);
	fun(100,200);
	fun(100,200,300);
	system("pause");
	return 0;
}

void fun(int i, int j, int k);
{
	cout << i << " " <<  j  << " " << k << endl;
}
4.2 Function overload

Same name, distinguishable parameters
Prerequisite: In the same scope, multiple functions defined by the same function name have different parameter types and numbers
Benefits: For example, maximizing serves the same purpose but requires different types and ranges of objects

#include <iostream>
#include <stdlib.h>
using namespace std;

void fun(int i = 30, int j = 20, int k = 10);  
void fun(double i, double j);

int main (void)
{
	fun(1.1,2.2);  // Call the second function
	fun(1,2);  // Call the first function, k takes the default value 

	system("pause");
	return 0;
}

void fun(int i, int j, int k);
{
	cout << i << " " <<  j  << " " << k << endl;
}
void fun(double i, double j);
{
	cout << i << " " <<  j  << endl;
}
4.3 Inline Function

Purpose: Save call time
Keyword: inline
Think: Why not set all functions as inline?
Reasons: (1) Inline compilation is recommended and is determined by the compiler;
(2) Simple logic, frequent function calls using inline, for loop and other complex cannot be used;
(3) Inline functions cannot be used for recursive functions.

#include <iostream>
#include <stdlib.h>
using namespace std;

inline void fun(int i = 30, int j = 20, int k = 10);  // Keyword: inline
inline void fun(double i, double j);   // Add keywords only at function declarations, everything else is the same

int main (void)
{
	fun(1.1,2.2);  // Call the second function
	fun(1,2);  // Call the first function, k takes the default value 

	system("pause");
	return 0;
}

void fun(int i, int j, int k);
{
	cout << i << " " <<  j  << " " << k << endl;
}
void fun(double i, double j);
{
	cout << i << " " <<  j  << endl;
}

Keywords: C

Added by LordShryku on Fri, 23 Aug 2019 05:32:45 +0300