Commit afdd8e1c authored by Uwe Wienkop's avatar Uwe Wienkop

2020-06-30a

parent 70d6b2b4
using System;
using System.Runtime.CompilerServices;
namespace _01OperatorOverloading
{
......@@ -42,15 +43,33 @@ namespace _01OperatorOverloading
// nur wenn b1.z == b2.z ist, wird b1.n == b2.n ausgeführt
public static bool operator !=(Bruch b1, Bruch b2)
=> !(b1 == b2);
public static Bruch operator <<(Bruch b, int x) => new Bruch(b.z * x, b.n * x);
public static Bruch operator >>(Bruch b, int x) => new Bruch(b.z / x, b.n / x);
// Konvertierungsoperatoren:
public static explicit operator int(Bruch b)
=> b.z / b.n;
public static implicit operator double(Bruch b)
public static explicit operator double(Bruch b)
=> (double)b.z / (double)b.n;
public static implicit operator Bruch(int zahl)
=> new Bruch(zahl);
// Überladung unärer Operatoren:
public static Bruch operator +(Bruch b) => b;
public static Bruch operator -(Bruch b) => new Bruch(-b.z, b.n);
// ACHTUNG: Nur EINE Inkrement-/Dekrement- Implementierung angeben!
// Post- oder Pre-Inkrement/Dekrement werden vom Compiler generiert!
// Die folgenden beiden Zeilen sind also falsch!
//public static Bruch operator ++(Bruch b) { b.z += b.n; return b; }
//public static Bruch operator --(Bruch b) { b.z -= b.n; return b; }
// !!! So geht es richtig:
public static Bruch operator ++(Bruch b) { return new Bruch(b.z + b.n, b.n); }
public static Bruch operator --(Bruch b) { return new Bruch(b.z - b.n, b.n); }
public override string ToString() => $"{z} / {n}";
}
......@@ -83,19 +102,30 @@ namespace _01OperatorOverloading
int k2 = 3 * (int)4.5;
int k = (int)e3; // explizite Konvertierung
double d = e3; // implizite Konvertierung
double d = (double) e3;
Console.WriteLine(k);
Console.WriteLine(d);
double d2 = (int)34;
int k3 = (int)3.5;
Bruch b4 = new Bruch(2, 5); // b4: neuer Speicher #100000
Bruch b5 = new Bruch(2, 5); // b5: neuer Speicher #200000
Bruch b4 = new Bruch(2, 5); // b4: neuer Speicher #100.000
Bruch b5 = new Bruch(2, 5); // b5: neuer Speicher #200.000
if (b4 == b5)
Console.WriteLine("Sind gleich");
else
Console.WriteLine("Sind unterschiedlich");
Bruch b6 = new Bruch(1, 2);
Console.WriteLine(b6++);
Console.WriteLine(++b6);
int x = 55;
Console.WriteLine(x++);
Console.WriteLine(++x);
Bruch b7 = new Bruch(1, 3);
Console.WriteLine(b7 << 2);
}
}
}
......@@ -10,3 +10,4 @@ C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\04 UebgDi Vererbung\obj\Debug
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\04 UebgDi Vererbung\obj\Debug\netcoreapp3.1\04 UebgDi Vererbung.dll
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\04 UebgDi Vererbung\obj\Debug\netcoreapp3.1\04 UebgDi Vererbung.pdb
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\04 UebgDi Vererbung\obj\Debug\netcoreapp3.1\04 UebgDi Vererbung.genruntimeconfig.cache
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\04 UebgDi Vererbung\obj\Debug\netcoreapp3.1\04 UebgDi Vererbung.csprojAssemblyReference.cache
......@@ -10,3 +10,4 @@ C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\05 UebgDi Warenwirtschaft\obj
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\05 UebgDi Warenwirtschaft\obj\Debug\netcoreapp3.1\05 UebgDi Warenwirtschaft.dll
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\05 UebgDi Warenwirtschaft\obj\Debug\netcoreapp3.1\05 UebgDi Warenwirtschaft.pdb
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\05 UebgDi Warenwirtschaft\obj\Debug\netcoreapp3.1\05 UebgDi Warenwirtschaft.genruntimeconfig.cache
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\05 UebgDi Warenwirtschaft\obj\Debug\netcoreapp3.1\05 UebgDi Warenwirtschaft.csprojAssemblyReference.cache
......@@ -10,3 +10,4 @@ C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\05 UebgMo Warenwirtschaft\obj
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\05 UebgMo Warenwirtschaft\obj\Debug\netcoreapp3.1\05 UebgMo Warenwirtschaft.dll
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\05 UebgMo Warenwirtschaft\obj\Debug\netcoreapp3.1\05 UebgMo Warenwirtschaft.pdb
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\05 UebgMo Warenwirtschaft\obj\Debug\netcoreapp3.1\05 UebgMo Warenwirtschaft.genruntimeconfig.cache
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\05 UebgMo Warenwirtschaft\obj\Debug\netcoreapp3.1\05 UebgMo Warenwirtschaft.csprojAssemblyReference.cache
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\05HTMLEngine\obj\Debug\06HTMLEngine.csprojAssemblyReference.cache
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\05HTMLEngine\obj\Debug\06HTMLEngine.csproj.CoreCompileInputs.cache
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.