标签:ref further 类型转换 抽象 test int private module init
passwd
sudo shutdown -h 3
(broadcast to all users in 3 mins)sudo reboot -r now
touch file-name
cat/more/less/head/tail file-name
pwd
int main(int argc, char **argv, char **envp)
argc
: Number of arguments in the listenvp
: Shell reads in environment variables into envp list; can be omitted if not needed-o filename
: specify the output filename-E
: Stop after the preprocessing stage (只进行预编译)-S
: Stop before assembling (生成汇编代码,不进行汇编)-c
: do not run the linker (不进行链接)A function (函数) or variable (变量) can be defined only once, but can be declared many times.
extern int x;
extern void sum (int argc, char* argv[]);
Preprocessor directive (预编译命令) #include
<>
: typically for system headers" "
: First search in current directory where the compilation is launchedSuggestion: include related header files in all the source files, wherever the declared functions are either defined or referenced (引用).
Different source files (.cpp) (源文件) are compiled separately into different relocatable object files(.o or obj) (可重定位的目标文件)
g++ -c main.cpp -o main.o
The relocatable object files are linked into the executable object file (可执行目标文件) by a tool called linker (链接器)
g++ main.o sum.o product.o -o test.exe
A tool called make is introduced to manage the compilation process.
test.exe: product.cpp sum.cpp main.cpp functions.h
g++ product.cpp sum.cpp main.cpp -o test.exe
Dependency rule: if test.exe does not exist or is older than any of the files after the colon(??, the following command (g++ …) will be executed.
an example:
test.exe: product.o sum.o main.o
g++ product.o sum.o main.o -o test.exe
product.o: product.cpp functions.h
g++ -c product.cpp -o product.o
sum.o: sum.cpp functions.h
g++ -c sum.cpp -o sum.o
main.o: main.cpp functions.h
g++ -c main.cpp -o main.o
make test1.exe
There are tools for automatically generating the makefile, such as cmake.
A particular file of archive format (with .a suffix), which is a collection of concatenated relocatable object files, with a header describing the size and location of each object file.
g++ -static main.o test.a -o test.out
g++ -static main.o -L. -ltest -o test.out
-static: tells the linker to build a fully linked executable object file without any further linking at load time
An object module (with .so suffix) loaded at an arbitrary memory address (内存地址) and linked with a program in memory, at either run time (运行期) or load time (启动期). This is called dynamic linking (动态链接)
g++ -c -fPIC sum.cxx -o sum.o
g++ -shared -o libtest.so product.o sum.o
variables and function are encapsulated (封装) into a structure called class.
Key features of OOP:
class Bird{
public:
void fly();
void sing();
void eat(); //Member Functions
private:
std::string name_;
std::string color_;
float weight_;
float height_; //Member Variables
};
Do not restrict class designers (设计者)
Restrict class users (使用者)
Example:
ClassName objectName;
objectName.memberVariable = XX; //should be restricted
objectName.memberFunction(YY); //without restrict
private
private
for the derived classes
::
Functions less than 10 lines can be declared inline to improve efficiency
#ifndef BIRD_H_
#define BIRD_H_ //Preprocessor directive
#include <iostream>
#include <string>
class Bird {
public:
void setSpeed(float speed) {
speed_ = speed;
}//inline functions
private:
float speed_;
};
#endif
#include "bird.h"
using namespace std;
void Bird::fly(float time) {
float distance = speed_ * time;
cout << name_ << " flies distance: " << distance << endl;
}
Integrate attribute and behavior into an entity
Improve safety
Decouple (解耦) between class users and designers
Keyword this
: points to the address(地址) of the current object.
Usage
Return current object’s address from its member functions
=
): return *this;
class Bird {
public:
void fly(float time);
Bird* setName(char *name) {
name_ = name;
return this;
}
private:
std::string name_;
};
//User program:
Bird b;
b.setName(“Eagle")‐>fly(10.0);
Lifetime (生命期) or scope (作用域) of objects
Constructor (ctor, 构造函数) and destructor (dtor, 析构函数) are introduced for better SAFETY (安全性)
Special member functions for initialization (初始化) and cleanup (清除).
Automatically called by the compiler (编译器) when an object is being created (创建) or deleted (销毁)
Constructors and destructor are typically public
call constructor/destructor by new/delete
Why constructor is necessary?
Constructor provides a place to ensure certain task be executed, such as member initialization, along with object creation (对象创建时进行初始化)
Avoids the careless/wrong usage of a class
Constructor is automatically called by the compiler when the object is being created for definition of an object:
ClassA a; //Stack object
ClassA *pA = new ClassA; //Heap object
Constructors have the same name as the class
Parameters can be provided in constructor for initialization
class Tree {
int height_;
public:
Tree() { height_ = 0; } // Constructor 1
Tree(int h) { height_ = h; } // Constructor 2
};
a special constructor without function arguments (above. Constructor 1)
When no constructor is defined in a class, the compiler will synthesize (合成) a default constructor, called a default default constructor
The use of the default
keyword is preferred in modern C++ code
class Tree {
int height_{0};
public:
Tree() = default; //equals to Tree(){}
};
class Tree {
int height_ {0}, year_ {0};
public:
Tree(int h, int y) : year_{y}, height_{h} { }
};
enjoys better efficiency
Single-parameter constructor enables the implicit type conversion (隐式类型转换)
To forbid such conversions, use keyword explicit
.
class Tree {
int height_{0};
public:
Tree(int h):height_{h} {}
//explicit Tree(int h):height_{h} {}
}; //…
void foo(Tree t){
//…
}
foo(5); //works if no explicit keyword, error if explicit is used
Depends on other constructors for the initialization
class Tree {
int height_, size_;
public:
Tree(int h) : height_{h} { }
Tree() : Tree {h} { size_ = 0; }//Delegating constructor
};
Clean up the resource (memory) occupied by the object. A special function called destructor(prepare-to-die) is introduced for cleanup.
Destructor is automatically called by compiler when:
delete
: ClassA *pA = new ClassA; delete pA;
std::unique_ptr<>
and std::shared_ptr<>
instead of new
and delete
.DON’T explicitly call (显式调用) the destructor unless necessary.
pA->~ClassA(); // works but NOT recommended
~
When is a destructor necessary?
- To free (释放) member variables allocated on the heap (堆)
- Free other occupied resources (socket, printer, etc.)
the same as previous
Stack (栈) and Heap (堆) are memory (内存) fragments for storing the variables of the program.
Stack
int i; double val; ClassA a;
int a[100000000];
) and deep recursive functions (递归函数)Heap
The size of heap is much larger than stack: so large array variables(数组变量)should be defined on heap
How to define variables on the heap?
int num = 1000000; // on stack
int *arr = new int[num]; delete []arr; // on heap
int *arr = (int*)malloc(sizeof(int)*num); free(arr); // on heap
标签:ref further 类型转换 抽象 test int private module init
原文地址:https://www.cnblogs.com/zjp-shadow/p/14853809.html