Created
March 6, 2021 01:54
-
-
Save cassc/1c7e9e03afdbb280096d50e53a8b39a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "pt.h" | |
| #include <iostream> | |
| #include <vector> | |
| #include <string> | |
| using namespace std; | |
| static int bid = 0; | |
| double cost(int items, double price = 0.8); | |
| double cost(int items, double price) { return items * price; } | |
| Button::Button(int a, int b): id{bid++}, a{a}, b{b}{ | |
| cout << "π Creating object " << this->id << endl; | |
| } | |
| Button::Button() : Button(6, 6) {} | |
| Button::Button(int x) : Button(x, x) {} | |
| Button::~Button() { cout << "π Deleting object " << this->id << endl; } | |
| Button::Button(const Button &old): id{bid++}, a{old.a}, b{old.b}{ | |
| cout << "β Copying object " << old.id << " to " << this->id << endl; | |
| } | |
| Button::Button(Button &&old) : id{old.id}, a{old.a}, b{old.b} { | |
| cout << "β Moving object " << old.id << endl; | |
| } | |
| int Button::getArea() const{ return a * b; } | |
| void Button::setA(int a) { this->a = a; } | |
| void Button::setB(int b) { this->b = b; } | |
| ostream &operator<<(ostream &out, Button button) { | |
| out << "π " << button.id << ": a=" << button.a << ", b=" << button.b << ", area=" << button.getArea(); | |
| return out; | |
| } | |
| ostream &operator<<(ostream &out, Button *button) { | |
| out << "π " << button->id << ": a=" << button->a << ", b=" << button->b << ", area=" << button->getArea(); | |
| return out; | |
| } | |
| int main() { | |
| // Reference is an alias to a variable | |
| int a = 8; | |
| int &b = a; | |
| b = 9; | |
| cout << "a: " << a << " b: " << b << endl; | |
| //////////////////////////////////////////////////////////////////////////////// | |
| // Store pointers in vector | |
| // This won't work. | |
| vector<Button *> bs0; | |
| cout << string(80, '=') << endl; | |
| cout << "Testing bs0, objects with references" << endl; | |
| for (auto i = 0; i < 8; i++) { | |
| // DON'T do this | |
| // Allocated on stack | |
| Button button {i}; | |
| // Pass address to an object on stack | |
| bs0.push_back(&button); | |
| // button object deleted! | |
| } | |
| cout << "bs0: " << endl; | |
| for (auto button : bs0) { | |
| cout << button << endl; | |
| } | |
| cout << string(80, '=') << endl; | |
| //////////////////////////////////////////////////////////////////////////////// | |
| // Store objects in vector | |
| // This works, but creates tooo many objects | |
| cout << "Testing bs1, objects" << endl; | |
| vector<Button> bs1; // vector will take care of garbage collection | |
| for (auto i = 0; i < 8; i++) { | |
| Button button {i, i}; // will be allocated on stack and deleted at the end of current loop | |
| // when adding objects, vector makes a copy | |
| // bs1.push_back(button); | |
| // explicitly move instead of copy | |
| bs1.push_back(move(button)); | |
| // or better | |
| // bs1.push_back(Button{i, i}); | |
| } | |
| cout << "bs1: " << endl; | |
| for (auto button : bs1) { | |
| cout << button << endl; | |
| } | |
| cout << string(80, '=') << endl; | |
| //////////////////////////////////////////////////////////////////////////////// | |
| // Store pointers in vector | |
| // This works, just make sure to delete all objects at the end | |
| cout << "Testing bs2, pointers" << endl; | |
| vector<Button *> bs2; // need to free memory of objects in the vector | |
| for (auto i = 0; i < 8; i++) { | |
| Button *button = new Button(i); | |
| bs2.push_back(button); | |
| } | |
| cout << "bs2: " << endl; | |
| for (auto button : bs2) { | |
| cout << button << endl; | |
| } | |
| cout << string(80, '=') << endl; | |
| cout << string(80, '=') << endl; | |
| // Using vector of objects involves object copying | |
| cout << "bs1 before direct modification: " << endl; | |
| for (auto button: bs1){ | |
| button.setA(99); | |
| } | |
| cout << "bs1 after direct modification: " << endl; | |
| for (auto button : bs1) { // implicit copying occurs | |
| cout << button << endl; | |
| } | |
| cout << string(80, '=') << endl; | |
| cout << "bs1 before modification with reference: " << endl; | |
| for (auto &button : bs1) { | |
| button.setA(99); | |
| } | |
| cout << "bs1 after modification with reference: " << endl; | |
| // You can use `const` qualifier to prevent changing object | |
| for (auto const &button : bs1) { | |
| cout << button << endl; | |
| } | |
| cout << string(80, '=') << endl; | |
| // free memory for vector of pointers | |
| for (auto p: bs2){ | |
| delete p; | |
| } | |
| bs2.clear(); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef __pt__ | |
| #define __pt__ | |
| #include <iostream> | |
| class Button{ | |
| int id; | |
| int a; | |
| int b; | |
| public: | |
| Button(); | |
| Button(int x); | |
| Button(int a, int b); | |
| // copy constructor | |
| Button(const Button &old); | |
| // move constructor | |
| Button(Button &&old); | |
| ~Button(); | |
| int getArea() const; | |
| void setA(int a); | |
| void setB(int b); | |
| // object to string operators | |
| friend std::ostream &operator<<(std::ostream &out, Button button); | |
| friend std::ostream &operator<<(std::ostream &out, Button *button); | |
| }; | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment