Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

C++ for Java Developers (JavaZone 2017)

2,183 views

Published on

Introduction to Modern C++ for programmers with a Java background.

Published in: Software
  • Hello! Get Your Professional Job-Winning Resume Here - Check our website! https://vk.cc/818RFv
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

C++ for Java Developers (JavaZone 2017)

  1. 1. @pati_gallardo Segmentation fault (core dumped)
  2. 2. C++ for Java Developers Android Edition Patricia Aas JavaZone 2017 @pati_gallardo
  3. 3. Who am I? @pati_gallardo
  4. 4. Patricia Aas Programmer - mainly in C++ Currently : Vivaldi Technologies Previously : Cisco Systems, Knowit, Opera Software Master in Computer Science - main language Java Twitter : @pati_gallardo
  5. 5. What Do I Want You To Remember? @pati_gallardo
  6. 6. Don’t do This! Banana * b = new Banana(); EVER @pati_gallardo
  7. 7. Why Should You Care? @pati_gallardo
  8. 8. Portable Languages Tools of the Trade Java JavaScript Python C++ @pati_gallardo
  9. 9. "Here be dragons" @pati_gallardo
  10. 10. Interesting Parts of Android @pati_gallardo
  11. 11. SurfaceFlinger The Compositor Composes the final frame of Android Which consists of many surfaces from many clients @pati_gallardo
  12. 12. Binder IPC Mechanism around which Android is built Requires kernel support Does context switch on IPC calls @pati_gallardo
  13. 13. Zygote Prototype App process Uses Linux features to isolate apps and use memory efficiently @pati_gallardo
  14. 14. Let’s Learn some Modern C++ (11-17) @pati_gallardo
  15. 15. Creating An Object @pati_gallardo
  16. 16. Your First Object #include "Banana.h" int main() { Banana b; } @pati_gallardo
  17. 17. Hello World @pati_gallardo
  18. 18. Your First Program #include <iostream> #include <string> int main() { std::string s("Hello "); std::cout << s << "World!"; } @pati_gallardo
  19. 19. Includes - Java Import Including your own headers #include "Banana.h" Including library headers #include <string> @pati_gallardo
  20. 20. Namespaces - Java Packages namespace fruits { class Banana { }; } @pati_gallardo
  21. 21. fruits::Banana b; using namespace fruits; Banana b; Using namespace - Java Import static @pati_gallardo
  22. 22. Using Namespace #include <iostream> #include <string> using namespace std; int main() { string s("Hello World!"); cout << s; } @pati_gallardo
  23. 23. Values, Objects and Referring to them @pati_gallardo
  24. 24. Anything Can Be a Value int main() { Banana b; int meaning = 42; auto pi { 3.14 } std::string s = "Hi!"; std::mutex my_mutex; } @pati_gallardo
  25. 25. Referring to Things ● Reference ● Pointer ● Value @pati_gallardo
  26. 26. C++ Reference & A C++ reference is not a Java pointer It has to reference an object It is never null It can never reference another object SAFE : Banana & b @pati_gallardo
  27. 27. C++ Pointer * A C++ pointer is not a Java pointer It’s often called a “Raw Pointer” It’s a raw memory address UNSAFE : Banana * b @pati_gallardo
  28. 28. C++ Value In C++ everything is a value, even objects When passed by value it is COPIED* Can only pass-by-value if copying is supported* SAFE : Banana b * Sometimes the original value can be reused @pati_gallardo
  29. 29. Range based For vector<Banana> bananas; for (auto & banana : bananas) cout << banana; @pati_gallardo
  30. 30. Const : The Many Meanings ● Object ● Function ● Local variable ● Member ● Pointer @pati_gallardo
  31. 31. const - Related to Final Variables in Java More things can be const than can be final Java : final Banana ptr; // ptr is final C++: Banana * const ptr; // ptr is const C++: const Banana * ptr; // object is const C++: const Banana * const ptr; // object & ptr is const @pati_gallardo
  32. 32. Immutable View Of An OBJECT const Banana & banana; But the object is mutable - just not by you Mark functions that don’t mutate as const @pati_gallardo
  33. 33. Parameter Passing, Return Values & Lambdas @pati_gallardo
  34. 34. Parameter Passing Pass by const ref : const Banana & banana Pass by reference : Banana & banana Pass by value : Banana banana Pass by pointer - be very careful @pati_gallardo
  35. 35. Auto return type auto meaning() { return 42; } @pati_gallardo
  36. 36. Return by Value Banana fruit() { return Banana(); } @pati_gallardo
  37. 37. Structured Bindings std::tuple<int, int> point(); auto [x, y] = point(); @pati_gallardo
  38. 38. Lambda and Captures auto meaning = [](){ return 42; } auto life = 42; auto meaning = [life]() { return life; } auto meaning = [=]() { return life; } auto meaning = [&]() { life++; return life;} @pati_gallardo
  39. 39. Memory : Lifetime & Ownership @pati_gallardo
  40. 40. Where is it? Stack Banana b; Heap auto b = make_unique<Banana>(); Banana * b = new Banana(); @pati_gallardo
  41. 41. Stack - Similar to “Try With Resources” Destroyed when exiting scope Deterministic Garbage Collection @pati_gallardo
  42. 42. Loving the Stack #include <iostream> #include <string> using namespace std; int main() { { string s("Hello World!"); cout << s; } // <- GC happens here! } @pati_gallardo
  43. 43. Hold a Value on the Stack that Controls The Lifetime of Your Heap Allocated Object using namespace std; auto myBanana = make_unique<Banana>(); auto ourBanana = make_shared<Banana>(); Smart Pointers @pati_gallardo
  44. 44. Structure @pati_gallardo
  45. 45. Class Declaration in Header File Class Definition in Cpp File Banana.h & Banana.cpp A header is similar to an interface Function declarations Member variables @pati_gallardo
  46. 46. Classes @pati_gallardo
  47. 47. No Explicit Root Superclass Implicitly defined member functions Don’t redefine them if you don’t need to The defaults are good Rule-of-zero (Zero except for the constructor) @pati_gallardo
  48. 48. class B { public: // Constructor B(); // Destructor ~B(); // Copy constructor B(const B&); // Copy assignment op B& operator=(const B&); // Move constructor B(B&&); // Move assignment op B& operator=(B&&); }; Implicitly Defined Functions @pati_gallardo
  49. 49. All Classes Are Final by Default By extension : All methods are final by default @pati_gallardo
  50. 50. Virtual Functions Pure virtual - Interface virtual void bootFinished() = 0; Use override void bootFinished() override; @pati_gallardo
  51. 51. Multiple Inheritance is Allowed Turns out the ‘Diamond Problem’ is mostly academic ( and so is inheritance ;) ) @pati_gallardo
  52. 52. All Classes Are “Abstract” Interface : only pure virtual methods Abstract Class : some pure virtual methods Plain Old Class : no pure virtual methods @pati_gallardo
  53. 53. Structs A C++ Struct is a Class Where all members are public by default @pati_gallardo
  54. 54. Static : The Many Meanings “There can be only one” Function in cpp file Local variable in a function Member / function in a class @pati_gallardo
  55. 55. Static member / function in a Class Pretty much the same as Java @pati_gallardo
  56. 56. Static Function in a Cpp File The function is local to the file @pati_gallardo
  57. 57. Static Variable in a Function Global variable Only accessible in this function Same variable across calls @pati_gallardo
  58. 58. Containers and Standard Types @pati_gallardo
  59. 59. Use std::String - never char * Or whatever string your project uses :D @pati_gallardo
  60. 60. Std::Vector Is Great std::vector has great performance Don’t use raw arrays Prefer std::vector or std::array @pati_gallardo
  61. 61. Use std::Algorithms using namespace std; vector<int> v { 1337, 42, 256 }; auto r = find_if(begin(v), end(v), [](int i){ return i == 42; }); @pati_gallardo
  62. 62. The Standard Library & co @pati_gallardo
  63. 63. Many Common Libraries The Standard Library Boost (Pre-Standard) Qt (Gui and Portable) ++ Use the Libraries @pati_gallardo
  64. 64. To Summarize @pati_gallardo
  65. 65. Do Modern C++ Use the Stack Use Values Use References Use Const Use the Libraries But Most Importantly... What to Remember @pati_gallardo
  66. 66. Don’t do This! Banana * b = new Banana(); EVER @pati_gallardo
  67. 67. #include "Banana.h" int main() { Banana b; } @pati_gallardo
  68. 68. Make Everyone Feel Safe To Be Themselves @pati_gallardo
  69. 69. C++ for Java Developers Android Edition Patricia Aas, Vivaldi Technologies @pati_gallardo Photos from pixabay.com
  70. 70. Vivaldi Swag Patricia Aas, Vivaldi Technologies @pati_gallardo Photos from pixabay.com

×