0% found this document useful (0 votes)
52 views

Object-Oriented Programming C++ Miscellaneous: Roman Podraza

This document discusses object-oriented programming concepts in C++ including namespaces, casting, typeid, mutable members, and the Standard Template Library (STL). Namespaces allow logical grouping of related classes and functions. Casting includes static, reinterpret, const, and dynamic casts. typeid returns type information at runtime. mutable allows modification of const class members. The STL includes common containers like vectors and maps, iterators to traverse containers, and algorithms that operate on containers.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Object-Oriented Programming C++ Miscellaneous: Roman Podraza

This document discusses object-oriented programming concepts in C++ including namespaces, casting, typeid, mutable members, and the Standard Template Library (STL). Namespaces allow logical grouping of related classes and functions. Casting includes static, reinterpret, const, and dynamic casts. typeid returns type information at runtime. mutable allows modification of const class members. The STL includes common containers like vectors and maps, iterators to traverse containers, and algorithms that operate on containers.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Object-Oriented Programming

B.Sc. Programme in Electrical and Computer Engineering

C++ Miscellaneous
Roman Podraza
Institute of Computer Science
Warsaw University of Technology

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.
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
2

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;
3

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 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);
5

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 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() 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

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
9

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
// ...
}

10

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.
11

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;
}
12

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.

13

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

14

Standard Template Libraries (STL)


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

vector
stack
list
queue
dequeue
priority queue

15

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
16

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.

17

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

18

Thank You

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

You might also like