课程名称:C++ Programming: Classes and Data
课程内容:Object-Oriented Programming
课程网址:https://www.coursera.org/learn/cplusplus-crypto-ii/home/week/1
课程进度:Week1
Week1
Introduction to the second course in the specialization
Validate验证,确认,证实
Scratch(n.)划痕,划伤(v.)挠,划破,取消
Syllabus教学大纲
C++ is strongly typed
Precursor前身,前驱,先锋
2 to the power of 3 is 8
Intriguing有趣的,引人入胜的,神秘的
Intrigue(n.)阴谋,密谋(v.)密谋,激起…的兴趣
Precision精确,准确,细致
Exponent指数,幂
The order book data set
CSV file comma separated value file
Row行
Representing numerical order book data with variables
Numerical数字的
Modelling the other data
Precision精确,准确,细致
Curly brackets花括号,波形括号
Preliminary预备性的,初步的,开始的
Using an enum to represent the bid/ask
Constrain约束,限制,限定,强制
Obscure模糊的,无名的,费解的
Representing multiple order book entries: vector
Angle braces < >
Neat整洁的,整齐的,好的
Concept workshop: basic data types-numbers and strings
Peculiarity个性,特点,特色
Encrypt(v.)加密,编码 decrypt
Abuse滥用,虐待,辱骂
Shove推,猛推,乱挤
Categorical明确的,绝对的
C++创建一个string实际上也是创建了一个class,可以调用函数等
Suffice(v.)足够,足以
Implicit含蓄的,内涵的,不直接言明的
Vector的好处之一是可以方便地延伸添加数据
Introduction to classes
Instance实例,例子,事例
Defining our first class:OrderBookEntry
Specification规范,规格
Instantiation实例化
Passing initialization data into objects
Constructor构造函数
用大括号表明这里是使用构造函数而不是类似于调用函数
OrderBookEntry就是我们自己写的constructor
Prototype原型,雏形,最初形态
Constrain(v.)约束,限制,限定,强制
Constructor initialization list
更高效简洁的一种传值写法
Create a vector of objects
改进版规避了需要写太多变量和重复太多赋值的问题,直接就可以加
都用小括号也可以,不过大括号有助于区分调用
Three ways to iterate over a vector
Scenario方案,设想,预测
第一种iteration如上
做loop的时候,i++不如++i更efficient,因为loop里i++要比++i做更多运算,i++要先make a copy of the data然后increment the copy然后assigns the copy back into I,i++是increments it directly in place
Relevant相关的,有意义的,有价值的,切题的
第二种iteration如上
第三种iteration如上
Continually不断地
Syntax for working with simple classes,and putting objects into a vector
Elaborate复杂的,详尽的,精心制作的
Encapsulate压缩,概括,简述
The constructor always has the same name as the class they are constructing所以上面的两个名称完全相同
Weekly assessment
#include
#include
#include
enum class OrderBookType
{
bid,
ask
};
class OrderBookEntry
{
public:
OrderBookEntry(
std::string _timestamp,
std::string _product,
OrderBookType _type,
double _price,
double _amount
)
:
timestamp(_timestamp),
product(_product),
type(_type),
price(_price),
amount(_amount)
{
}
std::string timestamp;
std::string product;
OrderBookType type;
double price;
double amount;
};
double computeAveragePrice(std::vector& obe)
{
double avg;
int i = 1;
for (OrderBookEntry &obes : obe)
{
avg=avg+(obes.price-avg)/i;
++i;
}
return avg;
}
double computeLowPrice(std::vector& obe)
{
double low=obe[0].price;
int i = 1;
for (OrderBookEntry &obes : obe)
{
if(obes.price<=low)
low = obes.price;
++i;
}
return low;
}
double computeHighPrice(std::vector& obe)
{
double high = obe[0].price;
int i = 1;
for (OrderBookEntry &obes : obe)
{
if (obes.price >= high)
high = obes.price;
++i;
}
return high;
}
double computePriceSpread(std::vector& obe)
{
double low = obe[0].price,high=obe[0].price;
int i = 1;
for (OrderBookEntry &obes : obe)
{
if (obes.price <= low)
low = obes.price;
if (obes.price >= high)
high = obes.price;
++i;
}
return high-low;
}
int main()
{
std::vector obe;
obe.push_back(OrderBookEntry {
“2020 / 03 / 17 17 : 01 : 24 884492”,
“BTC/USDT”,
OrderBookType::ask,
2000.1032,
0.02536
});
obe.push_back(OrderBookEntry{
“2020 / 03 / 17 17 : 01 : 24 884492”,
“BTC/USDT”,
OrderBookType::bid,
4000.5984,
0.03548});
double avg = computeAveragePrice(obe);
double low = computeLowPrice(obe);
double high= computeHighPrice(obe);
double spr = computePriceSpread(obe);
std::cout
<< avg << " " << low << " " << high << " " << spr << std::endl;
return 0;
}
Refactor重构