vtk/vtk.html qt5_8_snippets.html


Snippets for C++

Nur als Erinnerungsstütze für C++...

Polymorphe Exceptions

#include <iostream.h>

const int ciDefaultSize = 10;

class CExcpBoundary {};

class CExcpSize
{

public:

CExcpSize( int iSize ):m_iSize( iSize ){}

virtual ~CExcpSize(){}

virtual int getSize(){ return m_iSize; }

virtual void showErrMsg()
{ std::cout << "Error: Wrong size! Received: " << m_iSize << std::endl; }

protected:

int m_iSize;

};

class CExcpOversized : public CExcpSize
{

public:

CExcpOversized( int iSize ):CExcpSize( iSize ){}

virtual void showErrMsg()
{ std::cout << "Error: Oversized! Received: " << CExcpSize::m_iSize << std::endl; }

};

class CExcpTooSmall : public CExcpSize
{

public:

CExcpTooSmall( int iSize ):CExcpSize( iSize ){}

virtual void showErrMsg()
{ std::cout << "Error: Too small! Received: " << CExcpSize::m_iSize << std::endl; }

};

class CExcpNull : public CExcpTooSmall
{

public:

CExcpNull( int iSize ):CExcpTooSmall( iSize ){}

virtual void showErrMsg()
{ std::cout << "Error: Is null! Received: " << CExcpSize::m_iSize << std::endl; }

};

class CExcpNegative : public CExcpSize
{

public:

CExcpNegative( int iSize ):CExcpSize( iSize ){}

virtual void showErrMsg()
{ std::cout << "Error: Is negative! Received: " << CExcpSize::m_iSize << std::endl; }

};

class CMyArray
{

public:

CMyArray( int iNewSize = ciDefaultSize );

CMyArray( const CMyArray &x );

virtual ~CMyArray(){ delete [] m_pArrayType; }

CMyArray& operator=( const CMyArray& );

int& operator[]( int iOffset );

const int& operator[]( int iOffset ) const;

int getSize() const { return m_iArraySize; }

friend ostream& operator<< (ostream&, const CMyArray& );

private:

int *m_pArrayType;

int m_iArraySize;

};

CMyArray::CMyArray( int iSize ):
m_iArraySize( iSize ),
m_pArrayType( NULL )
{

if( iSize < 0 )throw CExcpNull( iSize );

else if( iSize == 0 )throw CExcpNull( iSize );

else if( iSize < 42 )throw CExcpTooSmall( iSize );

else if( iSize == 0 )throw CExcpOversized( iSize );

m_pArrayType = new int[ iSize ];

for( int n = 0; n < iSize; ++n ){ m_pArrayType[ n ] = 0; }

}

int& CMyArray::operator[] ( int iOffset )
{

if( iOffset >= 0 && iOffset < getSize() )return m_pArrayType[ iOffset ];

throw CExcpBoundary();

return m_pArrayType[ iOffset ];

}

const int& CMyArray::operator[] ( int iOffset ) const
{

if( iOffset >= 0 && iOffset < getSize() )return m_pArrayType[ iOffset ];

throw CExcpBoundary();

return m_pArrayType[ iOffset ];

}

int main()
{

try{

int iArraySize = 0;

std::cout << "Enter new array size: ";

std::cin >> iArraySize;

CMyArray arr( iArraySize );

for( int n = 0; n < 100; ++n )

{

arr[ n ] = n;

std::cout << "array[ " << n << " ] ok.." << std::endl;

}

}

catch( CExcpBoundary )
{

std::cout << "wrong input, try again..." < std::endl;

}

catch( CExcpSize& e ) //here the polymorph exception!!
{

e.showErrMsg();

}

catch( ... )
{

std::cout << "catched an unkonwn exception!" < std::endl;

}

std::cout << "done.\n";

return 0;

}

Zeiger auf Funktionen

#include <iostream.h>

void setData( int&, int & );

void square( int&, int & );

void cube( int&, int & );

void swap( int&, int & );

void showData( int, int );

int main()
{

void (* pFunction) ( int&, int& );

bool bExit = false;

int iChoice;

int iVal[] = { 1, 2 };

while( bExit == false )
{

std::cout << "[0: set data]....[1: square]....[2: cube]....[3: swap].........(9: EXIT)";

std::cin >> iChoice;

switch( iChoice )

{

case 0: pFunction = setData;
break;

case 1: pFunction = square;
break;

case 2: pFunction = cube;
break;

case 3: pFunction = swap;

case 9: bExit = true;
break;

default:

std::cout << "out of order" << std::endl;

}

if( bExit )break;

showData( iVal[ 0 ], iVal[ 1 ] );

pFunction( iVal[ 0 ], iVal[ 1 ] );

showData( iVal[ 0 ], iVal[ 1 ] ); }

return 0;

}

}

void setData( int& iVal0, int& iVal1 )
{

std::cout << ""1. Wert : ";

std::cin >> iVal0;

std::cout << ""2. Wert : ";

std::cin >> iVal1;

}

void square( int& i0, int& i1 )
{

i0 *= i0;

i1 *= i1;

}

void cube( int& i0, int& i1 )
{

i0 = i0 * i0 * i0;

i1 = i1 * i1 * i1;

}

void swap( int& iX, int& iY )
{

int h = iX;

iX = iY;

iY = h;

}

void showData( int i0, int i1 )
{

std::cout << "x: " << i0 << " y: " << i1 << std::endl;

}

Zeiger auf Elementfunktionen

#include <iostream.h>

class CMammal
{

public:

CMammal( ):m_iAge( 1 ){}

virtual ~CMammal(){}

virtual void speak() const = 0;

virtual void move() const = 0;

protected:

int m_iAge;

};

class CDog : public CMammal
{

public:

virtual void speak() const { std::cout << "bark..." << std::endl; }

virtual void move() const { std::cout << "dog run..." << std::endl; }

};

class CCat : public CMammal
{

public:

virtual void speak() const { std::cout << "miaow..." << std::endl; }

virtual void move() const { std::cout << "cat walk..." << std::endl; }

};

class CPig : public CMammal
{

public:

virtual void speak() const { std::cout << "grunt..." << std::endl; }

virtual void move() const { std::cout << "pig run..." << std::endl; }

};

int main()
{

void (CMammal::*pFunc)() const = 0;

CMammal* ptr = NULL;

bool bExit = false;

int iMethod, iChoice;

while( bExit == false )
{

std::cout << "[0: dog]....[1: cat]....[2: pig].........(9: EXIT)";

std::cin >> iChoice;

switch( iChoice )

{

case 0: ptr = new CDog;
break;

case 1: ptr = new CCat;
break;

case 2: ptr = new CPig;
break;

case 9: bExit = true;
break;

default:

std::cout << "out of order" << std::endl;

}

if( bExit )break;

std::cout << "[0: speak] [1: move] ";

std::cin >> iMethod;

if( iMethod )pFunc = CMammal::speak;

else pFunc = CMammal::move;

( ptr->*pFunc )();

delete ptr; }

return 0;

}

}

Polymorphe Exceptions (TODO!!!)

#include <iostream.h>

void setData( int&, int & );

void square( int&, int & );

void cube( int&, int & );

void swap( int&, int & );

void showData( int, int );

int main()
{

const int ciMaxArray = 5;

void (* pFunctionArray[ ciMaxArray ]) ( int&, int& );

bool bExit = false;

int iChoice;

int iVal[] = { 1, 2 };

for( int i = 0; i < ciMaxArray; ++i )
{

std::cout << "[1: set data]....[2: square]....[3: cube]....[4: swap].........(0: EXIT)";

std::cin >> iChoice;

switch( iChoice )

{

case 1:
pFunctionArray[ i ] = setData;
break;

case 2:
pFunctionArray[ i ] = square;
break;

case 3:
pFunctionArray[ i ] = cube;
break;

case 4:
pFunctionArray[ i ] = swap;

default:
pFunctionArray[ i ] = 0

}
}

for( int i = 0; i < ciMaxArray; ++i )
{

pFunctionArray[ i ]( iVal[ 0 ], iVal[ 1 ] );

showData( iVal[ 0 ], iVal[ 1 ] );

}

return 0;

}

void setData( int& iVal0, int& iVal1 )
{

std::cout << ""1. Wert : ";

std::cin >> iVal0;

std::cout << ""2. Wert : ";

std::cin >> iVal1;

}

void square( int& i0, int& i1 )
{

i0 *= i0;

i1 *= i1;

}

void cube( int& i0, int& i1 )
{

i0 = i0 * i0 * i0;

i1 = i1 * i1 * i1;

}

void swap( int& iX, int& iY )
{

int h = iX;

iX = iY;

iY = h;

}

void showData( int i0, int i1 )
{

std::cout << "x: " << i0 << " y: " << i1 << std::endl;

}