Predefined Macro Signal Processing with List inline Output Exception Preprocessor#, ##in the Date-Time Class of c++.

Navigation

1.c++ Date & Time
2. Use lists in classes
inline function in 3.c++.
Static member functions in 4.c++ classes
5. Output Exceptions
6. Preprocessor#, ##
7. Predefined macros
8. Signal processing
--------------------

1.c++ Date & Time: Local Time, UTC Time, and Year, Month, Day Detailed Time

To define a header file**#include < ctime>**
time_t: Also a long integer that receives the value of time(0) or time(NULL)
tm: is a structure in c++ that encapsulates the year, month, day, hour, minute, second

time(0) or time(NULL) returns the number of seconds from January 1, 1970 to the present and is received in time_t type

	time_t now = time(0);
	cout<<now<<endl;   //Get seconds 

Time function:
ctime(const time_t *time): returns the string local year, month and day, with the reference type in the middle

	time_t now = time(0);
	char* dt = ctime(&now);   //Here's the time_it type passed in, received with char*
	printf("%s",dt); 

To get UTC time:
tm structure, gmtime (&now), to get the pointer to the structure

	time_t now = time(0);
	tm *gmtm = gmtime(&now);    //gmtime returns a pointer to tm 
	char *dt = asctime(gmtm);   //To get the UTC date time, the parameter requires a pointer to the structure tm and returns the string 
	printf("%s",dt); 

For time-specific information, the clock, minutes and seconds of the day, month, year:
To get a tm structure pointer between locals using Localtime (& now)

	time_t now = time(0);
    tm *ltm = localtime(&now);   //Get a structure that points to local time
	cout<<"Year:"<<1900+ltm->tm_year<<endl; 
	cout << "month: "<< 1 + ltm->tm_mon<< endl;
    cout << "day: "<<  ltm->tm_mday << endl;
    cout << "time: "<< 1 + ltm->tm_hour << ":";
    cout << 1 + ltm->tm_min << ":";
    cout << 1 + ltm->tm_sec << endl;

--------------------

2. Use lists in classes

#include <iostream>
#include <ctime>
#include <cstdio>
using namespace std;
class Person
{
	public:
		Person(int age);
		int m_age;
}; 

//First Form 
Person::Person(int age):m_age(age)  
{
	cout<<"this boy age is "<<this->m_age; 
}

//Second Form
Person::Person(int age) 
{
	this->m_age = age;
	cout<<"this boy age is "<<this->m_age; 
}
int main ()
{
	Person s(15);
	return 0;
}

--------------------

inline function in 3.c++.
Prefix function name with inline
When inline is not used, the procedure for calling a function is as follows:
When a function call is executed, it first allocates storage space for the parameter and local variable in the stack, then copies the value of the argument to the parameter, and then puts the return address of the function, which indicates where the program should go back after the function is executed, on the stack, before jumping to the internal execution of the function.This process takes time.

In addition, when a function executes a return statement, it reclaims the storage space occupied by the parameters and local variables from the stack, takes the return address out of the stack, and jumps to the address to continue execution, which also takes time.

The difference between inline functions and normal functions is that when the compiler processes a statement that calls an inline function, it does not compile the statement as a directive to the function call, but simply inserts the code of the entire function body into the call statement, just as the entire function body is rewritten at the call.

--------------------

Static member functions in 4.c++ classes

#include <iostream>
#include <ctime>
#include <cstdio>
using namespace std;

class Person
{
public:
	static int PerNum;  //Adding static is equivalent to a global function, assigning values to it starting with the creation of the first class, and sharing when creating other classes 
	Person(int age)   //Constructor 
	{
		this->m_age = age;
		PerNum++;     //Increase 
	}
	int m_age;
};
int Person::PerNum = 0;  //Extraclass Definition 

int main ()
{
	Person a(5);
	Person a1(6);
	cout<<a1.PerNum<<endl;   //Output result: 2
	return 0;
}

--------------------

5. Output Exceptions
cout and cerr:
cout: an ostream object written to standard output;
cerr: an ostream object output to a standard error, often used for program error information;

Effect:
1. Court output is buffered and monitors by default.This is a buffered output, standard output, and can be redirected (refer to the example below for the meaning of redirection);

2. The cerr outputs directly without buffering and is generally used to quickly output error information. It is a standard error and is associated with the standard output stream by default, but it is not buffered, which means the error message can be sent directly to the display without waiting for the buffer or a new line break to appear.Normally, they are not redirected (redirection seems controversial, some people say it's system related, but I don't understand it yet)

Format:
try{
if()
throw expression;
} catch {

}

--------------------

6. Preprocessor

define can be used to represent a function:

#include <iostream>
using namespace std;

#define MIN(a,b) (((a)<(b)) ? a : b)

int main ()
{
   int i, j;
   i = 100;
   j = 30;
   cout <<"The minimum is " << MIN(i, j) << endl;

    return 0;
}

Conditional compilation:

#include <iostream>
using namespace std;
#define DEBUG

#define MIN(a,b) (((a)<(b)) ? a : b)

int main ()
{
   int i, j;
   i = 100;
   j = 30;
#ifdef DEBUG
   cerr <<"Trace: Inside main function" << endl;
#endif

#if 0
   /* This is the comment section */
   cout << MKSTR(HELLO C++) << endl;
#endif

   cout <<"The minimum is " << MIN(i, j) << endl;

#ifdef DEBUG
   cerr <<"Trace: Coming out of main function" << endl;
#endif
    return 0;
}

# and ## operators

1. For #: used to connect strings

#include <iostream>
using namespace std;
#define MASK(x) #x  //The #operator converts the replacement-text token to a quoted string

int main()
{
	cout<<MASK(hello C++)<<endl; //It is equivalent to cout<<"hello C++"<"endl"; 
	//Output: hello C++.
	return 0;
} 
#include <iostream>
using namespace std;
#define CONNECT(x) "hello:"#x  //Join incoming together xy 

int main()
{
	int x = 2;
	cout<<CONNECT(x)<<endl; //cout<<ab<<endl; 
	//Output: hello:2
	return 0;
} 

2. For the ##:## operator, it is used to connect two tokens, such as a value

#include <iostream>
using namespace std;
#define CONNECT(x,y) x##y  //Join incoming together xy 

int main()
{
	int ab = 100;
	cout<<CONNECT(a,b)<<endl; //cout<<ab<<endl; 
	//Output: 100
	return 0;
} 

--------------------

7. Predefined macros

Can output directly:

#include <iostream>
using namespace std;

int main ()
{
    cout << "Value of __LINE__ : " << __LINE__ << endl;  //The position of this output statement line 
    cout << "Value of __FILE__ : " << __FILE__ << endl;  //Current File Path 
    cout << "Value of __DATE__ : " << __DATE__ << endl;  //Current date 
    cout << "Value of __TIME__ : " << __TIME__ << endl;  //Current Time 

    return 0;
}

Result:

--------------------

8. Signal processing
A signal is an interrupt passed by the operating system to a process that terminates a program earlier.On UNIX, LINUX, Mac OS X, or Windows systems, interrupts can be generated by pressing Ctrl+C.

Some signals cannot be captured by the program, but the signals listed in the table below can be captured in the program and appropriate actions can be taken based on the signals.
Header file #include < csginal>

Use the signal() function to capture SIGINT signals.Whatever signal you want to capture in the program, you must use the signal function to register the signal and associate it with the signal handler.

Syntax: void (*signal (int sig, void (*func)(int)))(int);

The first parameter is an integer representing the number of the signal; the second parameter is a pointer to the signal processing function.

#include <cstdlib>
#include <cstdio>

#include <csignal> /*for signal() and raise()*/

void hello(int signum){
  printf("Hello World!\n");
}

int main(){
  //Handle SIGINT with hello
  signal(SIGINT, hello);

  //loop forever!
  while(1);

  system("pause");
}

When ctrl+c is entered, the statement in the function is executed

87 original articles published, 8 praised, 3005 visits
Private letter follow

Tags: Unix Linux Mac OS X

Posted on Fri, 28 Feb 2020 18:26:31 -0800 by djnrempel