OOP学习笔记coursera(1/2;2/5)

课程名称: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重构

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值