# Object Oriented Programming --- ## Environment checking ```cpp= #include<iostream> using namespace std; int main(void){ cout << "Hello, World!" << endl; return 0; } ``` --- ## Beacon :::info - A beacon system - Print I'm Fine every 1 seconds ::: [Sleep Reference](https://stackoverflow.com/questions/158585/how-do-you-add-a-timed-delay-to-a-c-program) ```cpp= #include <ctime> void sleep(float seconds){ clock_t startClock = clock(); float cps = CLOCKS_PER_SEC; float duration = seconds * cps; while(true){ if (clock() > startClock+duration) break; } return; } ``` ---- ```cpp= #include <iostream> #include <ctime> using namespace std; void sleep(float seconds){ clock_t startClock = clock(); float cps = CLOCKS_PER_SEC; float duration = seconds * cps; while(true){ if (clock() > startClock+duration) break; } return; } int main (void){ while(true){ cout << "I'm Fine" << endl; sleep(1.0); } } ``` --- ## Here come's the PM :::info ![](https://i.imgur.com/O1ygk7l.png) ::: ---- ```cpp= #include <iostream> #include <ctime> using namespace std; void sleep(float seconds){ clock_t startClock = clock(); float cps = CLOCKS_PER_SEC; float duration = seconds * cps; while(true){ if (clock() > startClock+duration) break; } return; } int main (void){ int time = 0; while(true){ if (time % 1 == 0) cout << "I'm Fine" << endl; if (time % 2 == 0){ cout << "Sensor Fine" << endl; cout << "ADCS Fine" << endl; cout << "EPS Fine" << endl; cout << "Antenna Fine" << endl; cout << "GPS Receiver Fine" << endl; cout << "OBC Fine" << endl; cout << "Payload Fine" << endl; cout << "Structure Fine" << endl; } sleep(1.0); time ++; } } ``` --- ## Readability :::info ![](https://i.imgur.com/JvhXCea.png) ::: ```cpp= #include <iostream> #include <ctime> using namespace std; void sleep(float seconds){ clock_t startClock = clock(); float cps = CLOCKS_PER_SEC; float duration = seconds * cps; while(true){ if (clock() > startClock+duration) break; } return; } void checkSensorStatus(){ cout << "Sensor Fine" << endl; } void checkADCSStatus(){ cout << "ADCS Fine" << endl; } void checkEPSStatus(){ cout << "EPS Fine" << endl; } void checkAntennaStatus(){ cout << "Antenna Fine" << endl; } void checkGPSReceiverStatus(){ cout << "GPS Receiver Fine" << endl; } void checkOBCStatus(){ cout << "OBC Fine" << endl; } void checkPayloadStatus(){ cout << "Payload Fine" << endl; } int main (void){ bool flag = 0; while(true){ cout << "I'm Fine" << endl; if (flag){ checkSensorStatus(); checkADCSStatus(); checkEPSStatus(); checkAntennaStatus(); checkGPSReceiverStatus(); checkOBCStatus(); checkPayloadStatus(); } sleep(1.0); flag = !flag; } } ``` --- ## Timing ```cpp= #include <iostream> using namespace std; int main (void){ int time = 0; while(true){ if (time % 2 == 0){ cout << "2"; } if (time % 3 == 0){ cout << "3"; } sleep(1); time ++; } for (int a = 0; ; a++){ if (a % 2 == 0){ cout << "2"; } if (a % 3 == 0){ cout << "3"; } sleep(1); } return 0; } ``` --- ## Class ```cpp= #include <iostream> using namespace std; class Subsystem{ public: Subsystem() { cout << "print while created" << endl; } }; int main (void){ cout << "Do something first" << endl; Subsystem EPS = Subsystem(); cout << "Do something second" << endl; cin.get(); return 0; } ``` ---- ## Function ```cpp #include <cstring> class Subsystem{ public: string name; Subsystem(string subsystem_name) { name = subsystem_name; cout << name << " created" << endl; } void checkStatus(){ cout << name << " Fine" << endl; } }; ``` ---- ```cpp #include <cstring> int main (void){ cout << "Do something first" << endl; Subsystem EPS = Subsystem("EPS"); cout << "Do something second" << endl; cin.get(); return 0; } ``` ---- ## Constructors ```cpp= #include <cstring> #include <iostream> using namespace std; class Subsystem{ public: string name; Subsystem(string subsystem_name) { name = subsystem_name; cout << name << " created" << endl; } Subsystem(){ cout << "I am an empty subsystem" << endl; } void checkStatus(){ cout << name << " Fine" << endl; } }; int main (void){ cout << "Do something first" << endl; Subsystem EPS = Subsystem("EPS"); Subsystem epty = Subsystem(); /* ESP <- Subsystem class -> call constructor (Subsystem()(yellow)); Subsystem() <- calling method (Call while created) */ cout << "Do something second" << endl; cin.get(); return 0; } ``` ## Change our code :::info ![](https://i.imgur.com/lu3FWpy.png) ::: ```cpp= #include <cstring> #include <iostream> using namespace std; class Subsystem{ public: string name; Subsystem(string subsystem_name) { name = subsystem_name; cout << name << " created" << endl; } void checkStatus(){ cout << name << " Fine" << endl; } }; int main (void){ cout << "Do something first" << endl; Subsystem *EPS = new Subsystem("EPS"); EPS -> checkStatus(); cout << "Do something second" << endl; delete EPS; cin.get(); return 0; } ``` ---- ## Future use ```cpp= #include <cstring> #include <iostream> #include <ctime> #include <vector> using namespace std; void sleep(float seconds){ clock_t startClock = clock(); float cps = CLOCKS_PER_SEC; float duration = seconds * cps; while(true){ if (clock() > startClock+duration) break; } return; } class Subsystem{ public: string name; int temp; Subsystem(string subsystem_name){ name = "I'm "; } Subsystem(string subsystem_name, int subsystem_temp) { name = subsystem_name; temp = system_temp; cout << name << " created" << endl; } void checkStatus(){ cout << name << " Fine" << endl; } void checkTemp(){ cout << name << " " << temp << " degree c" << endl; } }; int main (void){ vector<Subsystem> subsystems; subsystems.push_back(Subsystem("EPS", 25)); subsystems.push_back(Subsystem("OBC", 22)); subsystems.push_back(Subsystem("ADCS", 21)); subsystems.push_back(Subsystem("Payload", 45)); subsystems.push_back(Subsystem("Antenna", 22)); subsystems.push_back(Subsystem("Sensor", 23)); subsystems.push_back(Subsystem("GPS Reciever", 23)); Subsystem beacon = Subsystem("Beacon"); int time = 0; while(true){ beacon.checkStatus(); if (time % 2 == 0){ for (Subsystem subsystem : subsystems){ subsystem.checkStatus(); } } if (time % 3 == 0){ for (Subsystem subsystem : subsystems){ subsystem.checkTemp(); } } sleep(1); time ++; } return 0; } ```
{"metaMigratedAt":"2023-06-16T07:22:34.300Z","metaMigratedFrom":"YAML","title":"Introduction to C++ OOP","breaks":true,"GA":"UA-208228992-1","contributors":"[{\"id\":\"bdcee32f-5dc2-4add-94fa-e418d7247ad0\",\"add\":8311,\"del\":311}]"}
    381 views