Introduction to C – Data Types, Casting, Classes – Part3


Data Types

Implicit casting

  • automatic Casting in numbers “from smaller to larger” Data Types

Ranking:

unsigned long long ("größter Wert") long long long double double float unsigned long long unsigned (int) int short bool
  • casting not const to const, Credentials
T->T& T&->T T->const T T*->const T* T&->const T&

Explzite typecast

  • mit static_cast<>
  • in elementary (simple) Data Types
bool i = true; int y = static_cast<int> i; //y ist 1
  • to constants casten, with const_cast<>
  • critical casting with reinterpret_cast<>
struct A{} a; struct B{} b; struct A *pa = &a; struct B *pb = pa; //Fehler, nicht derselbe Datentyp, obwohl selber Struct-Inhalt struct B *pb = reinterpret_cast<B*>(pa); //ist ok

Inline Functions

  • Performance Vorteile, no copy of the transfer parameters
  • difficult to debug with errors
  • Function call is substituted by the content function by compiler
#define max(i,j) (i > j) ? i : j
m = max(3, 4); //Compiler ersetzt zu:  m = (3 > 4) ? 3 : 4;

Classes

The advantage of classes to structures (struct)

  1. Constructor / destructor runs automatically, where memory management, may be remembered more difficult
  2. at Struct Memory is created by the compiler, new Class runtime
  3. Inheritance possible
  4. possible for both rights management with private /public/protected

Memory

class A{ int a;//existiert pro Object der Klasse int f();//ecistiert 1 mal insgesamt auf dem Stack
 A(){};//Konstruktor
 ~A(){};//Destruktor
 }

Naming

  • Member Functions not as name Membervariablen
class A{ int a;  float a();
}

Static members / functions with static

  • are one per class in memory
  • exist separate instance of objects
  • static functions are not able to not-static variables and functions to access
class A{
 static int a;//existiert einmal pro Klasse
}
int b = A::a; //ohne Objekt von A möglich

Initializer List of Constructors

  • possible only with constructors
class A{ int a, b;
 A(int ai, int bi) : a(ai), b(bi){};//Konstruktor mit Initializer List
 }

corresponds:

class A{ int a, b;
 A(int ai, int bi){a = ai; b = bi};
 }

Copy Constructor

  • is automatically created, copied Member Variables
  • Attention, there are the addresses of the member variables copied, changes in both values ​​are affected, not if desired, Function override: A::A(const X&);
class A{ int a;
 }

A einA;

A zweiA (einA); //Copy Konstruktor
//ODER
A zweiA = einA; //Copy Konstruktor
//ODER
A zweiA A(einA); //Copy Konstruktor

 

Contents:

Introduction to C - Advantages, Compiler, Namespaces - Teil1

Introduction to C - Pointer, Pointer,References - Part2

Introduction to C - Data Types, Casting, Classes - Part 3

Introduction to C - Functions and Operator Overloading (Overloading) - Part4

Introduction to C - Classes: Heredity, Adjustment, Friends, abstract - Part5