Sie sind auf Seite 1von 19
C++ Miscellaneous Roman Podraza Institute of Computer Science Warsaw University of Technology Object-Oriented
C++ Miscellaneous Roman Podraza Institute of Computer Science Warsaw University of Technology Object-Oriented
C++ Miscellaneous Roman Podraza Institute of Computer Science Warsaw University of Technology
C++ Miscellaneous
Roman Podraza
Institute of Computer Science
Warsaw University of Technology

Object-Oriented Programming

B.Sc. Programme in Electrical and Computer Engineering

B.Sc. Programme in Electrical and Computer Engineering Project is co-financed by European Union within European
B.Sc. Programme in Electrical and Computer Engineering Project is co-financed by European Union within European

Project is co-financed by European Union within European Social Fund

Namespaces • Namespace is a independent context area, where names (identifiers) are separated from other

Namespaces

• Namespace is a independent context area, where names (identifiers) are separated from other namespaces.

– The namespace identifier is (can be) used as a part of scope resolved name

namespace Education { class university {

};

class high_school{

};

}

Education::university PW; // name university (of a class) // from namespace Education

Namespaces • A using declaration gives access to all names from a namespace using namespace

Namespaces

• A using declaration gives access to all names from a namespace

using namespace Education; high_school British_College;

• Namespaces can be nested.

namespace A {

int n;

namespace B { int m = 10;

}

n = B::m + 1;

}

A::B::m = A::n * 22;

Namespaces • Namespace can be anonymous and can be used instead of static global namespace

Namespaces

• Namespace can be anonymous and can be used instead of static global

namespace { int index = 0; // index cannot be accessed beyond the file

}

• Library headers do not have .h suffix and are declared in namespace std.

#include <iostream>

using namespace std;

Casting • Static casting ( static_cast< type > ) – Relatively safe – Can be

Casting

• Static casting ( static_cast<type> )

– Relatively safe

– Can be applied only for related types (e.g. between pointers, enumeration and integral, floating-point and integral)

double x, y = 2.6;

x = static_cast<double> (static_cast<int>(y) + 1); //relatively safe

enum peer {king, prince, earl} a; enum animal {horse, frog, snake} b;

a = static_cast<peer>(frog);

Casting • Reinterpreted casting ( reinterpret_cast< type > ) – Between any types – System

Casting

• Reinterpreted casting ( reinterpret_cast<type> )

– Between any types

– System dependent and unsafe

double x; int i = reinterpret_cast<int>(&x);

Casting • Constant casting ( const_cast< type > ) – This type of casting manipulates

Casting

• Constant casting ( const_cast<type> )

– This type of casting manipulates the constness of an object, either to be set or to be removed. For example

• in order to pass a const argument to a function that expects a non-constant parameter

• or to remove attribute of being constant

Casting class Date { bool buffer_ok; string buffer; void calc_buf_value(); // public: // string text_rep()

Casting

class Date { bool buffer_ok; string buffer; void calc_buf_value(); // public:

// string text_rep() const;

}

string Date::text_rep() const { if (buffer_ok == false) { Date* th = const_cast<Date*>(this); //remove constness th -> calc_buf_value(); th -> buffer_ok = true;

}

return buffer;

}

// Date d1; const Date d2; string s1 = d1.text_rep(); string s2 = d2.text_rep(); // undefined behavior

8

Casting • Dynamic casting ( dynamic_cast< type > ) – can be used only with

Casting

• Dynamic casting ( dynamic_cast<type> )

– can be used only with pointers and references to objects; its purpose is to ensure that the result of the type conversion is a valid complete object of the requested class

dynamic_cast<pointer_type>(pointer) works with classes

having virtual functions (requires polymorphic argument - run-time checking is feasible)

– if the conversion is inappropriate, the NULL pointer is returned

Casting class Base { // virtual void f(); // }; class Derived: public Base {

Casting

class Base { // virtual void f(); //

};

class Derived: public Base { //

};

void fcn (Base * ptr)

{

Derived *dptr = dynamic_cast<Derived*>(ptr); // assignes NULL if ptr points on object of Base class

//

}

typeid • typeid – allows to check the type of an expression: • typeid (expression)

typeid

• typeid

– allows to check the type of an expression:

typeid (expression)

– This operator returns a reference to a constant object of type type_info that is defined in the standard header file <typeinfo>.

– This returned value can be compared with another one using operators == and != or can serve to obtain a null-terminated character sequence representing the data type or class name by using its name() member.

typeid #include <iostream> #include <typeinfo> using namespace std; class CBase { virtual void f(){} };

typeid

#include <iostream> #include <typeinfo> using namespace std;

class CBase { virtual void f(){} }; // polymorphic class class CDerived : public CBase {};

int main () { Base* a = new Base; Base* b = new Derived;

cout << "a is: " << typeid(a).name() << '\n'; cout << "b is: " << typeid(b).name() << '\n';

cout << "*a is: " << typeid(*a).name() << '\n'; cout << "*b is: " << typeid(*b).name() << '\n'; if (typeid(*b) == typeid (Derived))

{

//

}

return 0;

}

Mutable members • mutable – The keyword mutable is used to allow a particular data

Mutable members

• mutable

– The keyword mutable is used to allow a particular data member of const object to be modified.

– This is particularly useful if most of the members should be constant but a few need to be updateable.

Mutable members class person { public: person (const char* pname, int page); void bday() {

Mutable members

class person { public:

person (const char* pname, int page); void bday() { ++age;}

private:

const char* name; mutable int age; // always can be modified

}

const person smb ("G.Bush", 23); // smb.bday(); //OK, smb.age is mutable

Standard Template Libraries (STL) • Simple (linear) containers: templates parameterized by type of their elements

Standard Template Libraries (STL)

• Simple (linear) containers: templates parameterized by type of their elements

– vector

– stack

– list

– queue

– dequeue

– priority queue

Standard Template Libraries (STL) • Map (associative collection, dictionary) – multimap (many values with one

Standard Template Libraries (STL)

• Map (associative collection, dictionary)

– multimap (many values with one key)

– set (key has no value associated with it)

– multiset

• String (non STL)

– valarray (vector of values for numeric processing)

– bitset

• Hashing maps

Standard Template Libraries (STL) • An iterator is any object that, pointing to some element

Standard Template Libraries (STL)

• An iterator is any object that, pointing to some element in a range of elements (such as an array or a container), has the ability to iterate through the elements of that range using a set of operators (at least, the increment (++) and dereference (*) operators).

• STL containers define appropriate iterators.

Standard Template Libraries (STL) • Algorithms (often apply iterators) – for_each(), find(), count(), equal() -

Standard Template Libraries (STL)

• Algorithms (often apply iterators)

for_each(), find(), count(), equal() - non-modifying

operations

copy(), swap(), replace(), replace_if(), remove() -

modifying operations

sort(), nth_element(), binary_search(), merge() - sorted

collections

Thank You Project is co-financed by European Union within European Social Fund
Thank You Project is co-financed by European Union within European Social Fund
Thank You
Thank You
Thank You Project is co-financed by European Union within European Social Fund
Thank You Project is co-financed by European Union within European Social Fund

Project is co-financed by European Union within European Social Fund