Peter Kristensen 2012-12-03 Peter Kristensen
Outline 1 What s this thing anyway 2 3 Functions Operators 4 Templates STL A better C 11 Peter Kristensen
Overview What s this thing anyway 1 What s this thing anyway 2 3 Functions Operators 4 Templates STL A better C 11 3 / 33
is... What s this thing anyway Almost a superset of C Object Oriented A better C than C 4 / 33
Some features in What s this thing anyway Namespaces Exceptions Function overloading Operator overloading Run-time type information (RTTI) Templates Standard Template Library 5 / 33
11 What s this thing anyway A major upgrade to Known as 0x because it was supposed to be finalized before 2010 Both clang and gcc supports more than 90% of the functinallity Lambdas For Each Auto types 6 / 33
Compiling What s this thing anyway Same as for C, just use g++ instead of gcc clang++ instead of clang 7 / 33
Overview 1 What s this thing anyway 2 3 Functions Operators 4 Templates STL A better C 11 8 / 33
Like C functions, classes have a declaration, and a definition Syntax resembles C structs The usual suspects Visibility declaration private/public/protected Members variables Methods Static members and methods this keyword 9 / 33
Class sample Class declaration (.h,.hpp,.hh) class A { private: int member1; public: A(); int getmember(); void setmember(int m); ; Class definition (.cc,.cpp,.cxx) A::A() { member1 = 42; int A::getMember() { return member1; void A::setMember(int m member1 = m; //this.member1 = m; 10 / 33
Initializing Can be allocated on stack A obj; // will implicitly call the constructor int k = obj.getmember(); printf("%d\n",k);... or heap A* obj1 = new A(); int g = obj1->getmember(); printf("%d\n",g); delete obj; 11 / 33
Code in the header It s legal to put the implementation in the header file Needed for templates Makes compiling slower 12 / 33
can inherit other classes Call their methods Read members If public or protected Etc. 13 / 33
sample class B : public A { public: B() : A() { void dostuff() { int k = getmember(); // int k = A::getMember(); printf("%d\n",k); ; 14 / 33
Multiple inheritance A class can have multiple parents Need to specify the correct parent for ambiguities. Can be used as interfaces by inheriting from a abstract class 15 / 33
Multiple interfaces sample (1/2) class Human { public: bool gotmilk() { return false; int numberoflegs() { return 2; ; class Horse { public: int numberoflegs() { return 4; ; 16 / 33
Multiple interfaces sample (2/2) class Centaur : public Horse, public Human { public: void about() { bool m = gotmilk(); int n = Horse::numberOfLegs(); // ^ need to be explicit to avoid ambigu printf("milk? %d\n",m); printf("legs: %d\n",n); ; 17 / 33
Virtual inheritance When a method is overridden by a subclass The method is chosen from the static type of the class unless the method is virtual In java, every method is virtual 18 / 33
Virtual inheritance sample (1/2) class Parent { public: virtual int age() { return 42; ; class Child : public Parent { public: virtual int age() { return 12; ; 19 / 33
Virtual inheritance sample (2/2) Child* c = new Child(); Parent* p = c; printf("%d\n",p->age()); 20 / 33
Overview Functions Operators 1 What s this thing anyway 2 3 Functions Operators 4 Templates STL A better C 11 21 / 33
Functions and methods Functions Operators The number, and type of arguments can be overloaded Not the return type int whatup() { return 4; int whatup(int k) { return k; 22 / 33
Default arguments Functions Operators Arguments can have default values Arguments following a argument with a default value, most also have a default value E.g. fun(int a=1, int b) is illegal int whatup2(int k=4) { return k; 23 / 33
Operator overloading Functions Operators Most operators can be overloaded using special method names E.g. operator+(int a) is called by writing x + 42 class Bing { public: int value; Bing() : value(4) { Bing operator+(int a) { Bing b = *this; b.value += a; return b; ; 24 / 33
Overview Templates STL A better C 11 1 What s this thing anyway 2 3 Functions Operators 4 Templates STL A better C 11 25 / 33
Templates Templates STL A better C 11 Compile-time expansion of types and values. Used to create generic classes collections matrix template <typename T,int N> class Vector { T data[n]; ; Vector<float,3> v; T will be replaced by float, and N by 3 26 / 33
Standard template library Templates STL A better C 11 Large collection of reusable components Templates to fit any type Collections vector / list / queue map set Iterators Stream and strings files 27 / 33
Sample Templates STL A better C 11 file:stl.cpp 28 / 33
is a better C Templates STL A better C 11 Use new for allocation E.g. new float[42] creates a array of 42*sizeof(float) bytes Undeclared functions are errors Function style typecast e.g. int(42.0) instead of (int)42.0 Constant strings require space for \0 e.g. char str[5] = "abcde"; is legal C, but illegal in 29 / 33
Templates STL A better C 11 The new version of the standard Should have been finished before 2010 Type inference using auto keyword E.g. for (auto itr=list.begin(); itr!=list.end(); itr++) For Each E.g. for (int k : list)... Lambda functions Unicode strings Initializer list E.g. vector<int> v={1, 4, 2; Much more! 30 / 33
Lambda Templates STL A better C 11 Anonymous function. Can capture scope by reference or by value. auto fun = [](int k) { printf("hello :%d\n", k); fun(42); 31 / 33
Lambda scope capturing Templates STL A better C 11 int hest = 41; auto byval = [=](int k) { return hest+k; ; auto byref = [&](int k) { hest = hest+k; ; byref(1); printf("%d\n", hest); printf("%d\n", byval(1)); 32 / 33
For each Templates STL A better C 11 vector<int> vec = {1,1,2,3,5,8,13,21; for (int k : vec) { printf("%d\n", k); 33 / 33