Tirgul 3 Agenda Overloading bracket operator Overloading parenthesis
Tirgul 3 - Agenda Overloading bracket operator. Overloading parenthesis operator. Conversions Implicit conversions User defined conversions. The explicit keyword 1
Overloading the bracket operator class Array { private: int _array[3]; public: Array(int array[3]); int& operator[] (const index); 2 }; int& Array: : operator[] (const index) { return _array[index]; }
Overloading the bracket operator int main() { int array[3] = {0, 1, 2}; Array arr(array); arr[2] = 3; // set a value cout << arr[2]; // get a value const Array const. Arr = {1, 2, 3}; cout<<const. Arr[2] //compilation error return 0; } 3
Overloading the bracket operator – const version class Array { private: int _array[3]; public: Array(int array[3]); int& operator[] (const index); int operator[] (const index) const; }; 4
Overloading the bracket operator class Matrix { private: int** _matrix; public: Array(int rows, int cols); }; Problem: there is no operator[][] in cpp 5
Solution: overload parenthesis operator class Matrix { private: int** _matrix; public: double& operator()(const int col, const in row); Matrix(const int rows, const int cols); }; 6
overload parenthesis operator int& Matrix: : operator()(const int col, const int row) { return _matrix[n. Row][n. Col]; } int main() { Matrix matrix; matrix(1, 2) = 3; std: : cout << matrix(1, 2); 7
Implicit conversions An implicit conversion is a conversion from one type to another that doesn't require an explicit typecast. Happened when a function (global, member, operator etc. ) gets X type while it was expecting to get Y type. 8
Implicit conversions - primitives For example: double res = 15 + 5. 785 Done by creating a temporary object of the correct type that's initialized to the value of he implicitly converted object. double temp = (double)15; res = temp + 5. 785; May result in losing information. 9
Implicit conversions - objects The creation of the temporary object is done by a “conversion constructor”. std: : string first. String("Convert"); std: : string second. String = first. String + “a string" Here we concatenated a c style char* to cpp string object. Done by creating a temporary std: : string : Std: : string temp("a string"); //call a constructor 10
Conversion example 11 #include <iostream> using namespace std; class A { A(int) { cout << "In ctor" << endl; } A(const A& a) { cout << "In copy ctor" << endl; } A& operator=(const A& a) { cout << "In operator=" << endl; return *this; } };
void f (A a) { cout << "In f" << endl; } int main() { A a 1 = 37; // calls the ctor that gets int cout << "----------" << endl; A a 2(37); // same cout << "----------" << endl; a 1= 67; // builds a new A and then calls operator = cout << "----------" << endl; f(77); // builds a new A and then send it to f cout << "----------" << endl; return 0; } 12
Conversions danger Unexpected behavior, e. g. : ctor: std: : vector(unsigned int length) void foo(const std: : vector& v). . . foo(300); // Did the user really wanted to send a // vector with 300 cells (all with value of 0)? vector and an unsigned int are not logically the same object ! 13
Explicit Conversion example ! 14 #include <iostream> using namespace std; struct A { explicit A(int) { cout << "In ctor" << endl; } A(const A& a) { cout << "In copy ctor" << endl; } A& operator=(const A& a) { cout << "In operator=" << endl; return *this; } };
Explicit Conversion example void f (A a) { cout << "In f" << endl; } void g() { A a 1 = 37; // won't compile cout << "----------" << endl; A a 2(37); // compile OK cout << "----------" << endl; a 1= 67; // won't compile cout << "----------" << endl; f(77); // won't compile cout << "----------" << endl; f((A)(77)); // compile OK cout << "----------" << endl; } 15
User defined conversions class Fraction {. . . // double --> fraction conversion (C-tor) Fraction(const double d) {. . . } // Fraction --> double conversion // (Conversion operator) operator double() const {. . . } 16
Rational Example ; ) // Rational. h class Rational { public: Rational(int x); operator int() const; const Rational operator+ (const Rational& other); private: int _numerator, _denominator; }; 17
Rational Example ; ) // Rational. cpp #include "Rational. h" #include <iostream> using namespace std; Rational: : Rational(int x) { cout << "Rational from int" << endl; _numerator=x; _denominator=1; } Rational: : operator int() const { cout << "op int (int from Rational)" << endl; return _numerator/_denominator; // There is data loss } 18
Rational Example ; ) const Rational: : operator+ (const Rational& other) { cout << "+" << endl; Rational temp(0); temp. _numerator=_numerator*other. _denominator+ _denominator*other. _numerator; temp. _denominator=_denominator*other. _denominator; // some code that simplifies fraction return temp; } 19
Rational Example? Consider the following: // main. cpp #include "Rational. h" int main() { Rational x(8); int y=9; int r; r=x+y; return 0; error C 2666: 'Rational: : operator +' : 2 overloads have similar conversions 1> e: eva'steachingunitrialsrational. h (14): could be 'const Rational: : operator +(const Rational &)' 1> or 'built-in C++ operator+(int, int)' 1> while trying to match the argument list '(Rational, int)‘ } Solution: explicit casting: 20 r=(int)x+y; // or r=x+(Rational)y;
STL Felxibility Efficiency Simple & Uniform interface Tons of fun 21
C++ standard library A collection of : Functions, Constants, Classes, Objects, Templates Extends the C++ language providing basic functionality to perform several tasks: classes to interact with the operating system data containers and manipulators to operate with them algorithms commonly needed Part of the C++ ISO Standard 22
standard library C Library, e. g: stdlib, stdio C++ Standard Library: Miscellaneous libraries, e. g: new, string C++ Standard Library: Standard Template Library (STL), e. g. : map, vector Features of the C++ Standard Library are declared within the std namespace 23
Main Concepts Function Objects Adaptors Streams Strings 24 Iterators Containers Algorithms
Container classes Two main types: Sequential containers: array – sequence of objects of compile time known length Warps a real array. You can pass it to function at last. list - a linked list of objects. Efficient insertion/deletion in front/end/middle vector - an extendable sequence of objects Efficient insertion/deletion at end Random access deque - double-ended queue Efficient insertion/deletion at front/end Random access Associative containers: map set 25
Concepts and STL A concept is a list of requirements on a type STL defines a hierarchy of concepts for containers, iterators, and element types Concepts for elements: Equality Comparable - types with operator== Less. Than Comparable - types with operator< Assignable - types with operator= and copy Ctor Separate interface from implementation Primitives can also conforms to a concept 26
Links to documentation SGI: http: //www. sgi. com/tech/stl/ http: //www. cplus. com/ref/ 27
- Slides: 27