# OO? ---- ## encapsulation - Use without knowing whats going on. ## Polymorphism - Use without modifing alot of codes. --- # Let's do it ---- ## Class ---- ### How ```c= #include<iostream> using namespace std; // Interface class Demo{ public: int a; int b; int doSomething(); }; // Often hidden int Demo::doSomething(){ return a + b; } int main(void) { Demo t; t.a = 11; t.b = 22; cout << endl; cout << t.doSomething() << endl; cout << endl; return 0; } ``` ---- ## Intro ---- ```cpp= // Interface class Demo{ public: int a; int b; int doSomething(); private: int doNothing(); } ``` - class `Demo` -> name of class - public -> `public` variables/functions -> variables/functions that everyone can use - private -> `private` variables/functions -> variables/functions only the class can use ---- ```cpp= // Often hidden int Demo::doSomething(){ return a + b; } ``` - Demo -> the class the function is inside - doSomething() -> the function you are trying to write your code ---- ```cpp= int main(void) { Demo t; t.a = 11; t.b = 22; cout << endl; cout << t.doSomething() << endl; cout << endl; return 0; } ``` - Demo t -> like a data type - `t.a`/`t.b`/`t.doSomething()` -> get it's variable/function --- ### Let's try ---- #### Create an calculator ```cpp= class Calculator{ /* * Your codes */ } /* * Your codes */ int main (void){ Calculator calculator; cin << calculator.firstNumber << calculator.secondNumber ; cout << calculator.add() << endl; cout << calculator.minus() << endl; cout << calculator.time() << endl; return 0; } ``` --- ## Inheritance ---- ### What - While you don't want to do copy pastes - It's actually very abstract. ---- ### How ---- ```cpp= class Father{ }; class Child : public Father{ }; ``` - A Child have every thing from father ---- ### What's protected? - Public - Private - Protected ---- ### What's protected? - Public -> Everyone can use - Private -> Noone can use - Protected -> The family can use --- #### Polymorphism ---- ```cpp= #include<iostream> #include<cstring> using namespace std; class Demo{ public: void speak(); protected: string greetings = "Default Demo says hi"; }; void Demo::speak(){ cout << "Hello, I'm Demo: \n\t"; cout << this -> greetings << endl; } class MathDemo : public Demo{ public: MathDemo(); }; MathDemo::MathDemo(void){ this -> greetings = "I'm a Math Demo!"; } class ChineseDemo : public Demo{ public: ChineseDemo(); }; ChineseDemo::ChineseDemo(){ this -> greetings = "Chinese demo says hi!"; } int main(void){ Demo demos[100]; demos[0] = Demo(); demos[1] = MathDemo(); demos[2] = ChineseDemo(); for (int i = 0; i < 3; i++){ demos[i].speak(); } return 0; } ``` ---- ### See what's interesting? ```cpp= int main(void){ Demo demos[100]; demos[0] = Demo(); demos[1] = MathDemo(); demos[2] = ChineseDemo(); for (int i = 0; i < 3; i++){ demos[i].speak(); } return 0; } ``` - You are available to do something in one line. --- ## Multiple functions with same name ---- 1. Overloading 2. Overriding ---- ## Difference ---- ### Overloading unexisted function - Same function name - Different function dectorator ---- ### Overriding Existed function - Same function name - Same function dectorator ---- - Overloading - Make a new function with the original function lives - Overriding - Rewrite the old function ---- #### Simple Test ```cpp= #include<iostream> #include<cstring> using namespace std; class Demo{ public: virtual void speak(); }; void Demo::speak(){ cout << "Hello, I'm Demo: \n\t A Defualt demo" << endl; } class MathDemo : public Demo{ public: virtual void speak(); void speak(int); }; void MathDemo::speak(){ cout << "Hello, I'm Demo: \n\t A math demo" << endl; } void MathDemo::speak(int number){ cout << "Math Demo can speak number : " << number << endl; } class ChineseDemo : public Demo{ public: virtual void speak(); }; void ChineseDemo::speak(){ cout << "Chinese demo says hi!" << endl; } int main(void){ Demo demo; MathDemo mathDemo; ChineseDemo chineseDemo; demo.speak(); mathDemo.speak(); mathDemo.speak(3); chineseDemo.speak(); return 0; } ``` ---- ### What can we overload? Operators!! ---- ### Example ```cpp= #include<iostream> #include<cmath> using namespace std; class PolarCoordinate { public: PolarCoordinate(int radius, double theta); int len(); double area(); private: int r; double theta; }; PolarCoordinate::PolarCoordinate(int radius, double theta){ this -> r = radius; this -> theta = theta; } int PolarCoordinate::len(){ return r; } double PolarCoordinate::area(){ return this -> r * cos(this -> theta) / 2; } ostream& operator<< (ostream& os, PolarCoordinate& coordinate){ os << "Length: " << coordinate.len() << ", Area: " << coordinate.area(); return os; } int main(void){ PolarCoordinate coordinate(3, 3.1415926/3); cout << coordinate.len() << ", " << coordinate.area() << endl; cout << coordinate << endl; return 0; } ``` --- ## Create a class ---- ### Workflow 1. What public/private do we need? 2. Operations? --- ## Interface ---- ### What Tells you how to create your class So that someone else can do polymorphism ---- #### Example ```cpp= class Box { public: // pure virtual function virtual double getVolume() = 0; private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; ``` ---- #### Try? 1. Everyone creates your own class that can run this main function 2. The class should have the interface Box ```cpp= int main(void){ Box box = new YourBox(); cout << box.getVolume << endl; return 0; } ``` --- ## Let's try with cooperations! ----
{"metaMigratedAt":"2023-06-17T01:34:44.465Z","metaMigratedFrom":"YAML","title":"Simple OO","breaks":true,"GA":"UA-208228992-1","slideOptions":"{\"theme\":\"solarized\",\"transition\":\"fade\"}","contributors":"[{\"id\":\"bdcee32f-5dc2-4add-94fa-e418d7247ad0\",\"add\":6505,\"del\":77}]"}
    382 views