OOP Summary
OOP
- OOP
- Preliminaries
- Class
- Functions and Variables
Preliminaries
Linux Basics
- Change Password:
passwd
- shutdown:
sudo shutdown -h 3
(broadcast to all users in 3 mins) - reboot:
sudo reboot -r now
- create an empty file:
touch file-name
- Display file contents:
cat/more/less/head/tail file-name
- find name of current directory:
pwd
Command-line Arguments
Access argv and envp in the main routine
argc
: Number of arguments in the listenvp
: Shell reads in environment variables into envp list; can be omitted if not needed
Compilation and multiple-file projects
options for g++
-o filename
: specify the output filename-E
: Stop after the preprocessing stage (只进行预编译)-S
: Stop before assembling (生成汇编代码,不进行汇编)-c
: do not run the linker (不进行链接)
Declaration
A function (函数) or variable (变量) can be defined only once, but can be declared many times.
- Variable declaration:
extern int x;
- Function declaration:
extern void sum (int argc, char* argv[]);
Include Header Files
-
Preprocessor directive (预编译命令)
#include
- Angle brackets
<>
: typically for system headers - Double quotes
" "
: First search in current directory where the compilation is launched
Suggestion: include related header files in all the source files, wherever the declared functions are either defined or referenced (引用).
- Angle brackets
Makefile
Separate Compilation
-
Different source files (.cpp) (源文件) are compiled separately into different relocatable object files(.o or obj) (可重定位的目标文件)
-
The relocatable object files are linked into the executable object file (可执行目标文件) by a tool called linker (链接器)
make and Makefile
A tool called make is introduced to manage the compilation process.
- Dependency rule (依赖规则)
- Defined by comparing the modification dates of the source/header files (比较文件的更新日期)
- When a dependency rule is satisfied, the corresponding (compilation/linking) commands will be executed.
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:
More on Makefile
- Default target is all
- If one specific target is needed:
make test1.exe
There are tools for automatically generating the makefile, such as cmake.
Library
Static library (Linux)
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.
-static: tells the linker to build a fully linked executable object file without any further linking at load time
Shared Library
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 (动态链接)
Class
variables and function are encapsulated (封装) into a structure called class.
Key features of OOP:
- Abstraction (抽象)
- Encapsulation (封装) (information hiding)
- Inheritance (继承)
- Polymorphism (多态)
Object & Abstraction
Member Variable and Member Function
Access Control to Class Members
-
Do not restrict class designers (设计者)
-
Restrict class users (使用者)
Example:
Public Members
- accessible from objects by class users
- the interface (接口) between class designers and the class users
- When no keyword is given
- Default access control for class is private
- Default access control for struct is public
Private Members
- only accessible within member functions by class designers
- This enables the information hiding (信息隐藏) for the class designers
Protected Members
- Similar access control to
private
- Different from
private
for the derived classes- Used in inheritance (继承)
Definition of Member Functions
Member function
- Declared inside the class definition (类声明内部)
- Typically defined outside the class definition
- With scope resolution operator
::
- With scope resolution operator
Inline function
Functions less than 10 lines can be declared inline to improve efficiency
Encapsulation
-
Integrate attribute and behavior into an entity
-
Improve safety
- Set different access control (访问权限控制) for different members.
- Class users can only access public members (also called interfaces, 接口)
-
Decouple (解耦) between class users and designers
this Pointer
-
Keyword
this
: points to the address(地址) of the current object. -
Usage
Return current object’s address from its member functions
- Typically used in operator overloading(e.g.,
=
):return *this;
- Typically used in operator overloading(e.g.,
Constructor (构造函数)
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:
-
Constructors have the same name as the class
-
Parameters can be provided in constructor for initialization
Default Constructor
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
Using default keyword for Default Constructor
The use of the default
keyword is preferred in modern C++ code
Member Initializer List
enjoys better efficiency
Keyword explicit
Single-parameter constructor enables the implicit type conversion (隐式类型转换)
To forbid such conversions, use keyword explicit
.
Delegating Constructor
Depends on other constructors for the initialization
Destructor (析构函数)
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:
- An object goes out of its scope
- Using
delete
:ClassA *pA = new ClassA; delete pA;
- In modern C++, we can use
std::unique_ptr<>
andstd::shared_ptr<>
instead ofnew
anddelete
.
DON’T explicitly call (显式调用) the destructor unless necessary.
- Destructor
- Name of destructor is the same as the class, preceded by
~
- No function parameters
- There should be only one destructor
- The destructor needs to be public to be automatically called
- Name of destructor is the same as the class, preceded by
When is a destructor necessary?
- To free (释放) member variables allocated on the heap (堆)
- Free other occupied resources (socket, printer, etc.)
Default Keyword
the same as previous
Stack vs. Heap Variables
-
Stack (栈) and Heap (堆) are memory (内存) fragments for storing the variables of the program.
-
Stack
- Limited size of several kilobytes (千字节) to megabytes (兆) depending on the system (系统) and compiler settings.
- Store local variables of different functions
- Example of variables on stack:
int i; double val; ClassA a;
- Stack overflow (栈溢出)may occur for large array variables (e.g.,
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?
-
Functions and Variables
Macro
Macro is an improvement over magic numbers based on preprocessor directive (预编译命令).
- ``#ifdef` checks whether a macro is defined
Useful macros in debugging
__func__
: function name in const character array__FILE__
: file name in string literal (字面值)__LINE__
: current line number in integer literal__TIME__
: time of the compilation in string literal__DATE__
: date of the compilation in string literal
Inclusion Guard for Headers
#define XX
: defines a macro XX#ifndef XX
: if XX is not defined, go forward; otherwise,
skip the following part before #endif
Header format
Without the header guard, a header file may be included multiple times in one source file, causing compiling errors.
Undefine Macros
#undef VALUE
helpful in avoiding name conflicts.
assert and NDEBUG
assert
: is a preprocessor macro for debugging
-
assert(expr);
If expr is false, the program will be terminated with error messages.
-
Header file:
#include <cassert>
When the program is to be released, assert should be disabled by macro NDEBUG
.
- Way No. 1: “``#define NDEBUG`” in source file
- Way No. 2:
g++ -D NDEBUG xxx.cpp
Prefer enum/const to Macros (宏)
Design principle: avoid macros as much as possible.
Prefer compiler (编译器) to preprocessor (预编译器)
Replace function-like macros by inline functions (内联函数).
Const
const
(常量): used to define read-only (只读) variables.
const
defaults to internal linkage (内部链接)
-
Visible (可见) only within the source file where it is defined
-
Invisible (不可见) in other translation units (翻译单元) (.cpp files)
-
Definition (定义) and initialization (初始化) must be done at the same time.
Enum Instead of Macros
enum
: all values are available during compilation
- Restriction (局限): can only represent integers
Using const in Class
-
Constructor Initializer List
Const objects can only call const member functions.
as_const and const_cast
Conversion between const and non-const (常量与非常量之间的转换)
const_cast
: cast between const and non-const
const_cast<Type*>(expression)
const_cast<Type&>(expression)
as_const
: cast non-const into const
Top-Level vs. Low-Level Const
Function Overloading
Function overloading (函数重载): allow the same function name to be used with different arguments
Reuse original compiler: transform overloaded functions into general functions.
-
For the function arguments:
f(int)
f_int()
f(float)
f_float()
-
For the scope (作用域):
X::f()
_X_f()
not return type : The compiler cannot determine which function is called
Grammars on Function Overloading
- A const parameter is only distinguished from a non-const parameter for references and pointers.
- For a fundamental type such as int, const int is identical to int.
Default Arguments
Default argument (缺省参数): function argument in function declaration (函数声明) with default values(缺省值) that the compiler automaticallyassigns if not provided in the function call (函数调用)
Grammar
-
Only tailing arguments can be defaulted
-
Default arguments are only given in function declaration(函数声明), NOT in function definition (函数定义)
- Exception: when there is no function declaration
-
Coding style: commented values can be given in function definition for better readability
Function Overloading vs. Default Arguments
Difference
- Overloaded functions: multiple function bodies (函数体)
- Default arguments: only one function body
Constant Expression
Constexpr functions
- Suggests the compiler to compute constant value from the function at compile time (编译期)
- Implicitly set as inline functions (缺省设为内联函数)
Constexpr expressions
- Requires the compiler to return constant value at compile time
Difference between const and constexpr expressions
const
: only requires that variables be constant (read-only), no matter it is constant since compilation or notconstexpr
: the variable should be constant since compilation
Inline Function
Beside general inline functions, there is a special inline function: Inline member functions (内联成员函数)
-
Grammar:
inline Type foo (Type1 a, Type2 b) { ... }
-
The
inline
keyword is only sending a request to the compiler (向编译器发送内联请求), which may be ignored.
Inline Member Functions
A member function with its body (函数体) defined within the class definition is automatically inline. On modern processors, smaller code usually runs faster due to better use of the instruction cache.
Namespace
namespace
: a name wrapper to enclose the defined names in a distinct space.
- Typically for avoiding name collisions (命名冲突)of different global functions, global variables, and classes
But namespace
is dedicated to (专用于) the creation of a new space.
- Two ways of referring to names:
using namespace spaceName
:opens the whole namespaceusing spaceName::myName
:only opens myName
NEVER open namespaces by using in header files, which increases the chance of name collision (名字冲突)
Static Variable and Function(静态变量与静态函数)
Static (静态) data/variable: lives throughout lifetime of program
- Allocated once (分配存储空间) at a fixed address in a special static data area (静态存储区), which is neither stack (栈) nor heap (堆)
Keyword static vs. extern
-
static
:Internal linkage: static variables/objects and static general functions (普通函数) are local to its translation unit (编译单元)
-
extern
:External linkage: visible to the linker (链接器) everywhere, i.e., external to the translation unit (编译单元) where it is defined
Example:
Static Class Members
Static members belong to the class: they are shared by all objects of the same class
-
Static member functions:
Cannot call non-static member functions or access non-static member variables
-
Two ways of creating static arrays:
enum
static const
Singleton
-
Singleton design pattern
- Ensures only one instance (object) for a given class
- By setting constructors as private
Reference
Reference (引用) : alias (别名) of a given object or variable
-
All operations (操作) on a reference affects the referenced object
-
Reference: A const pointer (指针常量) that is automatically dereferenced
-
NULL references are not allowed
-
But NULL pointers are allowed
-
Reference Usage
-
References are mainly used for function parameters (函数形参) and return value
-
Modify values or carry return values by function arguments (函数实参)
-
Avoid making copies of function arguments and/or return value for better efficiency
-
Design principle: Least privilege principle (最小特权原则)
Award function enough access to accomplish the task, but no more
-
-
In a range-based for loop(pass by reference)
Copy constructor (拷贝构造函数)
A special constructor for cloning a new object from an existing object. Both objects are of the same class
Grammar: X(const X& obj) { ... }
- Argument takes an existing object
Examples of constructor definition
Copy constructor is called when:
If the designer does NOT define a copy constructor,the compiler will synthesize (合
成) one automatically
- Bitcopy (位拷贝) is used in the synthesized copy constructor
- This is error-prone for classes with pointer members
Avoid Copy Constructor
- Pass and return objects by reference
complex& func(complex& c) { return c; }
- For better safety, use
const
to avoid modificationsvoid func2(const complex& c) { … }
Keyword delete
delete
: Disable certain member function without defining the function body
Move Constructor
lvalue (左值) and rvalue (右值)
-
lvalue: left-hand value of
=
- Its memory supports both read and write
int *p, i; // then *p and ++i are lvalues
-
rvalue: right-hand value of
=
-
Its memory is read only, including literals (字面量, e.g., 10, “Hello World”) and temporaries (临时对象)
-
-
Const and Non-Const References
Rvalue Reference
An rvalue reference only binds to an rvalue, which is going to be destroyed.
Keyword: &&
When is rvalue reference useful?
Actually, we do not need to make copies at all
- Grammar:
X(X&& obj)
- Object obj should be non-const
- Use std::move(x) to invoke the move constructor
- Here,
move
means that “you can treat x as an rvalue”, i.e., it may be destroyed
- Here,
Note: If a copy constructor or a destructor is defined, the compiler will NOT synthesize (合成) the move constructor
Move assignment (移动赋值) is also used in the above example, which will be introduced when we learn operator overloading.
__EOF__

本文链接:https://www.cnblogs.com/zjp-shadow/p/14853809.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
2018-06-05 LOJ #6433. 「PKUSC2018」最大前缀和(状压dp)
2018-06-05 LOJ #6432. 「PKUSC2018」真实排名(组合数)