As a header file, there are generally declarations of corresponding classes and functions in .h.
A typical .h file is as follows: (FileName: yimian.h)
````C++
#ifndef YIMIAN_H //Prevent this file from being compiled multiple times
#define YIMIAN_H //The value of'YIMIAN_H' needs to be customized
//Lib may be used in.h
#include<iostream>
#include<string>
using namespace std; //Use the std namespace, so you don’t need to write std::cout below
//Declare the yimian class
class yimian{
public: //Public domain (can be directly called by the outside world)
void hi();
static string version;
private: //Private (not public)
};
#endif //Correspond to the anti-multiple compilation at the beginning of the file
````
For safety, the initialization of functions and variables is generally not carried out in the .h file. Instead, I am used to creating a new .cpp file with the same name.
The following is an example of a .cpp file corresponding to the morning: (FileName: yimian.cpp)
````C++
#include"yimian.h" //Need to point to yimian.h
string yimian::version = "Version 0.0.1"; //Assign a value to a string variable
void yimian::hi(){ //Define the content of the hi() function in the yimian class
Then you can use g++ to compile happily, the instructions are as follows: (How to build a GNU environment in win)
````
$ g++ test.cpp yimian.cpp -o [name of target file]
````
Then start the compiled program from the command line (what is the command line ⌇●﹏●⌇)
````
$ ./Target file name.exe
````
As you might expect, the result of the above program is:
> Hellow Yimian~
> Version 0.0.1
----------
**Pit** ::aru:meditation::
- The defined class declared in the header file must be instantiated (called) in main(), otherwise an error will be reported
------------
## Summarize the related knowledge of the class
### Class access modifier
- **public:** Content accessible inside and outside the class
- **private:** not accessible outside the class, only accessible by yourself and friends, not accessible by subclasses
- **protected:** Inaccessible from outside, accessible by yourself, friends and subclasses
### Special function types
- **Constructor:** It will be executed every time an object is created, that is, initialization
````C++
class Line{
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor
private:
double length;
};
````
- **Destructor:** The function executed when the object is deleted, that is, the aftermath
````C++
class Line{
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor declaration
private:
double length;
};
````
- **Copy constructor:** Transfer parameters between objects of the same type when copying objects
````C++
class Line{
public:
int getLength( void );
Line( int len ); // simple constructor
Line( const Line &obj); // copy constructor
~Line(); // Destructor
private:
int *ptr;
};
·········
Line::Line(const Line &obj){
cout << "Call the copy constructor and allocate memory for the pointer ptr" <<endl;
ptr = new int;
*ptr = *obj.ptr; // copy value
}
·········
int main( ){
Line line1(10);
Line line2 = line1; // The copy constructor is also called here
return 0;
}
````
- The difference between construction and copy construction:
````C++
A x(2);//Direct initialization, call the constructor
A y = x;//Copy initialization, call the copy constructor
````
### Friend function/class
- **Youyuan:** Youyuan is like an old neighbor, you can take whatever you want at home
````C++
class INTEGER{
friend void Print(const INTEGER& obj);//Declare friend function
};
void Print(const INTEGER& obj) {
//Function body
}
void main(){
INTEGER obj;
Print(obj);//Call directly
}
````
### Inline functions
- In short, the number of lines is small and the function is very simple. There are no loops and switch statements. If you want to improve the efficiency of the function, use inline (╯‵□′)╯︵┴─┴
````C++
inline int Max(int x, int y){
return (x> y)? x: y;
}
// The main function of the program
int main(){
cout << "Max (20,10): "<<Max(20,10)<<endl;
cout << "Max (0,200): "<<Max(0,200)<<endl;
cout << "Max (100,1010): "<<Max(100,1010)<<endl;
return 0;
}
````
### this pointer
- Similar to this in js, pointing to the parameter object itself