Informatik C++ Modern and Lucid C++ for Professional Programmers Part 1 Self-study for Introduction to C++ Prof. Peter Sommerlad Director IFS Institute for Software Rapperswil, HS 2017 Thomas Corbat
Additional self-study slides
A first Class - Type Definition class and struct keywords are equivalent struct means public: class means private: visibility does not need to be specified for each element Implementation names memberfunction with class name Object instantiated and member function called 3
Watch out: Semicolon after }; in class definitions Don't forget the semicolon! If you omit the semicolon after a class definition you will get very strange error messages, often far away from where you forgot the semicolon You can try that deliberately, but it will happen anyway! Just remember this! You've been warned 4
Tests for Classes in a Library C++ Library Project CUTE Library Test Project 5
Why C++? Someone on a mailing list Sept. 2013: Microsoft are trying their damnedest to destroy the desktop Windows environment in favour of the "Windows Store" environment (which they evidently see as a moneyspinner as it allows them to commoditize software in the same way as music, video, books, etc.). Net isn't part of that Windows store culture, and I don't see it surviving the upheaval in the long term... C++ (or, at least, Microsoft's chosen subset of it) *is* part of that culture, and I see it as being on the resurgence in the Microsoft world. 6
Why C++ (again) Can be portable across platforms more than with Java and.net but requires care to actually be portable Highly efficient, if applied right can be more efficient than C with a fraction of source code real estate Powerful systems programming tool unfortunately no standard C++11 UI frameworks available 7
C++ is Powerful more powerful concepts than Java, even if it looks similar high-level abstraction capabilities values, functions, operators types, classes, objects generic types and functions with templates compile-time programming (C++ advanced) ability to apply these concepts can result in less code to write: more efficient development But learning C++ requires hard work 8
C++ vs. Java Many similarities in syntax: if(cond){}else{} for(i = 0; i < last, ++i){} for(auto const x:vec){} while(true){} do{}while(false); Type variable; // for defining a variable visibility and scoping rules But they are not the same semantic and rules are different 9
C++ vs. Java: Types and Values All types in C++ describe values references have to be designated explicitly type construction defaults lead to value types user-defined types can work like built-in types: wrt. syntax and performance All user-defined types in Java are objects except: int, char, short, long, boolean, double, float, byte 10
C++ vs. Java: Built-in types Java defines definitive ranges for int, long, etc. and 2bit complement representation no "unsigned" numbers C++ provides a sequence of range inclusion, but no definitive bit count for its integers for hardware diversity char <= short <= int <= long <= long long float <= double <= long double C++ provides unsigned integral types (0.. 2 n-1 ) C++ measures sizes in number of char sizeof(char) == 1, but a char might not be an octet of bits 11
C++ vs. Java: Conversion and Types C++ converts automatically between types if one-step conversion possible, unless prohibited some pitfalls exist for careless programmer almost all casts demonstrate a design problem Java type conversion only happens for objects along their class hierarchy casting required up the hierarchy or for explicit conversion of built-in types 12
C++ vs. Java: true and false C++ represents truth values as type bool bool is an integer type: 5 + true == 6 + false In conditions all values that can be converted to a number or bool can act as truth values, only zero is considered false if (42) doit(); else never_happens(); do { only_once(); } while(0); while(std::cin) { std::cin>>x; std::cout<<x*x;} 13
C++ vs. Java: code organization Java provides computation in Methods always part of a class C++ provides computation in functions some may be a class member return_type function(parameter) {... } #include <iostream> #include <string> int main(){ std::string message; message = "Hello World"; std::cout << message << std::endl; } C++ public class HelloWorld { public static void main(string[] args) { String message = new String(); message = "Hello"; System.out.println(message); } } 14
A note on the self-study material aka my "Lucid C++" book I will provide about a chapter for each week for you to read and prepare. The book is a work in progress. While the chapters for the first half of the semester are quite complete and mature, you should expect some rough edges when we reach mid- November. Any feedback you can give me (except for simple typos, which will be rid later) is really appreciated, e.g., if something is too hard to understand, errors in examples or reasoning. 15