Class One
:material-circle-edit-outline: 约 368 个字 :fontawesome-solid-code: 9 行代码 :material-clock-time-two-outline: 预计阅读时间 4 分钟
Intro¶
-
Buzzword
encapsulation 封装 inheritance 继承
polymorphism 多态
overriding 覆盖
interface 接口
cohesion 内聚
coupling 耦合
collection classes 容器类
template 模板
responsibility-driven design 责任驱动设计 -
Book reference: Thinking in C++
-
The C language
Efficient programs
direct access to machine
flexible -
C++ improvements
data abstraction
access control
initialization & cleanup
function overloading
stream for I/O -
The first program
- iostream is a header file name without a .h suffix. iostream.h happens to exist, yet it is an outdated version
<<
: insertercin >> variable
: extractor- use
;
to connnect the shell commands
Format output¶
#include <iomanip>
: manipulators are special functions that can be included in the I/O statement to alter the format parameters of a stream.endl
is one of themsetw(int width)
: set the output widthsetprecision(int n)
: set the number of digits in decimal parts
String¶
#include <string>
: c don't have a string typestring str
: a stringstr="Hello"
,str("Hello")
,s3{"see see"}
for every type, yet when it comes to the third way of initialization, one should addg++ -std=c++11
when compiling.- objects can transfer contents through
=
cin
: only read the first wordgetline(cin, str)
: read the whole linestring s[0]
: the first character of the string, the same as the array in cstring s2=string s1+ string s3
: string concatenations2.length()
: the length of the strings2(s3,2,3)
: the third to the fifth character of s3,2
is the starting position,3
is the length of the substrings2.substr(2,3)
: the same as aboveinsert(size_t pos, const string& str)
: insert a string into another stringerase(size_t pos, size_t len)
: erase a substringreplace(size_t pos, size_t len, const string& str)
: replace a substring with another stringfind(const string& str, size_t pos=0)
: find the first occurrence of a substringappend(const string& str)
: append a string to another stringstring *p=&s
: a pointer to a strings->length()
: the length of the string, the same as(*s).length()
string s1, s2; s1=s2;
: copy the string, two different objectsstring *p=&s1; string *q=&s2; p=q;
: two pointers point to the same objects2