-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathoperatorOverloadingAssignment.cpp
63 lines (48 loc) · 1.6 KB
/
operatorOverloadingAssignment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <algorithm>
#include <chrono>
#include <iomanip>
#include <iostream>
class Account{
public:
Account()= default;
Account(int numb): numberOf(numb), deposits(new double[numb]){}
Account(const Account& other): numberOf(other.numberOf), deposits(new double[other.numberOf]){
std::copy(other.deposits, other.deposits + other.numberOf, deposits);
}
Account& operator = (const Account& other){
numberOf = other.numberOf;
delete deposits;
deposits = new double[other.numberOf];
std::copy(other.deposits, other.deposits + other.numberOf, deposits);
return *this;
}
Account(Account&& other):numberOf(other.numberOf), deposits(other.deposits){
other.deposits = nullptr;
other.numberOf = 0;
}
Account& operator =(Account&& other){
numberOf = other.numberOf;
deposits = other.deposits;
other.deposits = nullptr;
other.numberOf = 0;
return *this;
}
private:
int numberOf;
double * deposits;
};
int main(){
std::cout << '\n';
std::cout << std::fixed << std::setprecision(10);
Account account(200000000);
Account account2(100000000);
auto start = std::chrono::system_clock::now();
account = account2;
std::chrono::duration<double> dur = std::chrono::system_clock::now() - start;
std::cout << "Account& operator = (const Account& other): " << dur.count() << " seconds" << '\n';
start = std::chrono::system_clock::now();
account = std::move(account2);
dur = std::chrono::system_clock::now() - start;
std::cout << "Account& operator=(Account&& other):" << dur.count() << " seconds" << '\n';
std::cout << '\n';
}