Objects layout in memory

Слайд 2

Permutation with using standard function

Permutation with using standard function

Слайд 3

Next Lexicographic permutation

Next Lexicographic permutation

Слайд 4

Upgraded solution

Upgraded solution

Слайд 5

Get permutation by sequence number

Get permutation by sequence number

Слайд 6

Objects layout in memory

Objects layout in memory

Слайд 7

Alignment struct Currency { char firstCurrency; double firstValue; char secondCurrency; double

Alignment

struct Currency
{
    char firstCurrency;
    double firstValue;
    char secondCurrency;
    double secondValue;
    char baseCurrency;
    int baseCurrencyId;
};

Слайд 8

Alignment struct Currency { double firstValue; double secondValue; int baseCurrencyId; char

Alignment

struct Currency
{
    double firstValue;
    double secondValue;
int baseCurrencyId;
char firstCurrency;
char secondCurrency;
    char baseCurrency;
};

Слайд 9

Inheritance class Base { int base; char otherB; }; class Derived

Inheritance

class Base
{
    int base;
    char otherB;
};
class Derived : public Base
{
    char derived;
    int otherD;
};
class Composed
{
    Base base;
    char

composed;
    int otherC;
};
Слайд 10

Multiple Inheritance class BaseA { int fieldA; }; class BaseB {

Multiple Inheritance

class BaseA
{
    int fieldA;
};
class BaseB
{
    int fieldB;
};
class Derived : public BaseA, public BaseB
{
    int fieldD;
};

Слайд 11

Multiple Inheritance Derived* pDer = new Derived; BaseA* pBaseA = pDer; BaseB* pBaseB = pDer;

Multiple Inheritance

Derived* pDer = new Derived;
BaseA* pBaseA = pDer;
BaseB* pBaseB = pDer;

Слайд 12

Multiple Inheritance

Multiple Inheritance

Слайд 13

VIRTUAL TABLE

VIRTUAL TABLE

Слайд 14

Non-virtual multiple inheritance -fdump-class-hierarchy

Non-virtual multiple inheritance

-fdump-class-hierarchy

Слайд 15

Virtual Inheritance -fdump-class-hierarchy

Virtual Inheritance

-fdump-class-hierarchy

Слайд 16

Is it right output? pA is B: B::foo() B::bar() A::baz() pA is C: С::foo() B::bar() A::baz()

Is it right output?
pA is B:
B::foo()
B::bar()
A::baz()
pA is C:
С::foo()
B::bar()
A::baz()

Слайд 17

pA is B: B::foo() B::bar() A::baz() pB is C: C::foo() C::bar() A::baz() Right output:

pA is B: B::foo() B::bar() A::baz() pB is C: C::foo() C::bar() A::baz()

Right output:

Слайд 18

Virtual table C++ uses a special form of late binding known

Virtual table

C++ uses a special form of late binding known as

the virtual table. The virtual table is a lookup table of functions used to resolve function calls in a dynamic/late binding manner.
Слайд 19

Proof: g ++ -fdump-class-hierarchy option

Proof: g ++ -fdump-class-hierarchy option

Слайд 20

Virtual table

Virtual table

Слайд 21

Слайд 22

Dynamic_cast

Dynamic_cast

Слайд 23

Dynamic_cast Syntax: dynamic_cast ( expression ) Example: class Base{ virtual void

Dynamic_cast

Syntax:
dynamic_cast < new_type > ( expression )
Example:
class Base{
virtual void Print() {

cout << "Base::print"; }
void SpecificPrint() { cout << "Specific function of the Base class"; } };
class Derived : Base{
void Print() { cout << "Derived::print"; }
void SpecificPrint() { cout << "Specific function of the Derived class"; } };
//downcast
Base* base = new Derived;
Derived* derived = dynamic_cast(base); or Derived derived = dynamic_cast(*base);
derived->SpecificPrint(); // call Derived::SpecificPrint()
//upcast
Derived* derived = new Derived;
Base* base = static_cast(derived);