Compare commits

...

5 Commits

Author SHA1 Message Date
a2680f18b4 finished all tasks 2026-06-29 21:18:48 +02:00
6303f5f224 namespace fix 2026-04-29 17:14:37 +02:00
b685a3dfae Aufgabe 6: Verkehrsmittel 2026-04-29 17:08:29 +02:00
23f603b64b small fix 2026-04-17 08:03:52 +02:00
65abdc34a9 Aufgabe 3-5 2026-04-15 20:25:59 +02:00
36 changed files with 1095 additions and 19 deletions

View File

@ -5,9 +5,11 @@ using namespace std;
class A
{
public:
void a() {}
};
virtual void a() {} //virtual hinzugefügt -> Aufruf der Funktion in C für ac->a();
virtual ~A() {}//hinzugefügt -> Aufruf der Deskstruktoren con B und C
};
class B : public A
{
public:
@ -21,12 +23,12 @@ public:
{
info("~B()");
}
void a()
void a()
{
s += "1";
info("B::a()");
}
virtual void b()
void b() //virtual entfernt -> Aufruf dieser Funktion statt der Funktion in C für bc->b();
{
s += "2";
info("B::b()");
@ -80,7 +82,7 @@ int main()
A a;
a.a();
cout << "vvv " << B::liefereIdStat() << endl;
B b;
const B b; //const hinzugefügt -> Aufruf von: void b() const
b.b();
cout << "www " << b.liefereIdStat() << endl;
C* c[2];

View File

@ -22,7 +22,8 @@ Vektor addVektor(Vektor vektor1, Vektor vektor2)
Koerper bewegeKoerper(Koerper koerper, Vektor gesamtkraft, float dt)
{
// Beschleunigung = bewegung / masse
// Beschleunigung = Kraft / masse
// Geschwindigkeit = Beschleunigung * Zeit
koerper.geschwindigkeit.x += dt * gesamtkraft.x / koerper.masse;
koerper.geschwindigkeit.y += dt * gesamtkraft.y / koerper.masse;

View File

@ -1,10 +1,36 @@
#include <stdio.h>
#include "summe.h"
#include <vector>
#include <iostream>
#include <string>
int main()
{
const int N = 4;
const float daten[N] = {1.0f,2.0f,3.0f,4.0f};
float summe = berechneSumme(daten,N);
printf("Summe = %f\n", summe);
{
using namespace std;
string name;
int anzahlWerte;
int summe;
vector<int> werte;
cout << "Bitte gebe deinen Namen ein: ";
cin >> name;
cout << "Hallo " << name << "!\nGebe die Anzahl deiner Werte an: ";
cin >> anzahlWerte;
if(anzahlWerte<= 0) return 0;
for( int i = 0; i < anzahlWerte; i++)
{
int eingelesen;
cout << "Wert " << i + 1 << " : ";
cin >> eingelesen;
werte.push_back(eingelesen);
}
summe = berechneSumme(werte);
cout << "Die Summe deiner Werte beträgt: " << summe << "\n\nAuf Wiedersehen, " << name;
}

View File

@ -1,8 +1,13 @@
#include "summe.h"
float berechneSumme(const float* const wert, int anzahlWerte)
int berechneSumme(std::vector<int> werte)
{
return (anzahlWerte) ? *wert+berechneSumme(wert+1, anzahlWerte-1) : 0;
int summe = 0;
for(int i = 0; i < (int)werte.size(); i++)
{
summe += werte[i];
}
return summe;
}

View File

@ -1,12 +1,13 @@
#ifndef __SUMME_H
#define __SUMME_H
#include <vector>
/**
* Berechnet die Summe ueber eine Menge von Werten.
* @param wert Werte
* @param anzahlWerte Anzahl der Werte
* @return Summe
*/
float berechneSumme(const float* const wert, int anzahlWerte);
int berechneSumme(std::vector<int> werte);
#endif

View File

@ -0,0 +1,19 @@
#ifndef FLUGKURVE02_H
#define FLUGKURVE02_H
struct Vektor{
float x;
float y;
};
struct Koerper{
float masse;
Vektor position;
Vektor geschwindigkeit;
};
Vektor mulVektor(const Vektor &vektor, const float &skalar);
Vektor addVektor(const Vektor &vektor1, const Vektor &vektor2);
Koerper bewegeKoerper(const Koerper &koerper, const Vektor &gesamtkraft, const float &dt);
void ausgabeKoerper(const Koerper &koerper);
#endif

View File

@ -0,0 +1,44 @@
#include <iostream>
#include "flugkurve02.h"
using namespace std;
Vektor mulVektor(const Vektor &vec, const float &skalar)
{
Vektor ergebnis;
ergebnis.x = skalar * vec.x;
ergebnis.y = skalar * vec.y;
return ergebnis;
}
Vektor addVektor(const Vektor &vektor1, const Vektor &vektor2)
{
Vektor result;
result.x = vektor1.x + vektor2.x;
result.y = vektor1.y + vektor2.y;
return result;
}
Koerper bewegeKoerper(const Koerper &koerper, const Vektor &gesamtkraft, const float &dt)
{
Koerper ergebnis;
ergebnis.masse = koerper.masse;
// Beschleunigung = Kraft / masse
// Geschwindigkeit = Beschleunigung * Zeit
ergebnis.geschwindigkeit.x = koerper.geschwindigkeit.x + dt * gesamtkraft.x / koerper.masse;
ergebnis.geschwindigkeit.y = koerper.geschwindigkeit.y + dt * gesamtkraft.y / koerper.masse;
// Neue Position
ergebnis.position.x = koerper.position.x + dt * ergebnis.geschwindigkeit.x;
ergebnis.position.y = koerper.position.y + dt * ergebnis.geschwindigkeit.y;
return ergebnis;
}
void ausgabeKoerper(const Koerper &koerper)
{
cout << "(" << koerper.position.x << "," << koerper.position.y << ") ";
cout << "Geschwindigkeit: (" << koerper.geschwindigkeit.x << ", " << koerper.geschwindigkeit.y << ")" << endl;
}

View File

@ -4,4 +4,53 @@
#include "flugkurve03.h"
using namespace std;
//to be implemented
Vektor Vektor::mul(const float &skalar)
{
this->x *= skalar;
this->y *= skalar;
return *this;
}
Vektor Vektor::add(const Vektor &vektor_to_add)
{
this->x += vektor_to_add.x;
this->y += vektor_to_add.y;
return *this;
}
Koerper Koerper::bewegen(const Vektor &beschleunigung, const float &dt)
{
this->position.x += this->geschwindigkeit.x * dt;
this->position.y += this->geschwindigkeit.y * dt;
this->geschwindigkeit.x += beschleunigung.x * dt;
this->geschwindigkeit.y += beschleunigung.y * dt;
return *this;
}
Vektor Koerper::liefereGeschwindigkeit()
{
return this->geschwindigkeit;
}
Vektor Koerper::lieferePosition()
{
return this->position;
}
float Koerper::liefereMasse()
{
return this->masse;
}
string Koerper::text()
{
stringstream ausgabe;
ausgabe << "(" << this->position.x << "," << this->position.y << ")";
return ausgabe.str();
}

View File

@ -1,4 +1,30 @@
#include <string>
using namespace std;
// to be defined
class Vektor{
public:
float x;
float y;
Vektor() : x(0), y(0) {}
Vektor(float x, float y) : x(x), y(y) {}
Vektor mul(const float &skalar);
Vektor add(const Vektor &vektor_to_add);
};
class Koerper{
public:
float masse;
Vektor position;
Vektor geschwindigkeit;
Koerper() : masse(0), position(0,0), geschwindigkeit(0,0){}
Koerper(float m, Vektor pos, Vektor gesch) : masse(m), position(pos), geschwindigkeit(gesch) {}
Koerper bewegen(const Vektor &beschleunigung, const float &dt);
Vektor liefereGeschwindigkeit();
Vektor lieferePosition();
float liefereMasse();
string text();
};

View File

@ -0,0 +1,13 @@
#include "Luftfahrzeug.h"
#include "Verkehrsmittel.h"
#include <string>
#include <sstream>
string Luftfahrzeug::text()
{
stringstream ausgabe;
ausgabe << "Luftfahrzeug - Position x: " << x << "; Position y: " << y << "; max. Höhe: " << max_hoehe;
return ausgabe.str();
}

View File

@ -0,0 +1,15 @@
#pragma once
#include <string>
#include "Verkehrsmittel.h"
class Luftfahrzeug : public Verkehrsmittel{
int max_hoehe;
int z;
public:
Luftfahrzeug() : max_hoehe(0), z(0) {}
Luftfahrzeug(int max_hoehe) : max_hoehe(max_hoehe), z(0) {}
Luftfahrzeug(int max_hoehe, int z) : max_hoehe(max_hoehe), z(z) {}
string text();
};

14
6_Verkehrsmittel/PKW.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include "Verkehrsmittel.h"
#include "Radfahrzeug.h"
#include <string>
class PKW : public Radfahrzeug{
const int anzahl_tueren;
public:
PKW() : Radfahrzeug(4), anzahl_tueren(4) {}
PKW(int anzahl_raeder, int anzahl_tueren) : Radfahrzeug(anzahl_raeder), anzahl_tueren(anzahl_tueren) {}
string text();
};

View File

@ -0,0 +1,11 @@
#include "Radfahrzeug.h"
#include <sstream>
string Radfahrzeug::text()
{
stringstream ausgabe;
ausgabe << "Radfahrzeug - Position x: " << x << "; Position y: " << y << "; Anzahl Räder: " << anzahl_raeder;
return ausgabe.str();
}

View File

@ -0,0 +1,14 @@
#pragma once
#include "Verkehrsmittel.h"
#include <string>
class Radfahrzeug : public Verkehrsmittel{
protected:
const int anzahl_raeder;
public:
Radfahrzeug() : anzahl_raeder(4) {}
Radfahrzeug(int anzahl_raeder) : anzahl_raeder(anzahl_raeder) {}
string text();
};

View File

@ -0,0 +1,18 @@
#include "Verkehrsmittel.h"
#include <sstream>
#include <string>
void Verkehrsmittel::bewege(int zielx, int ziely)
{
x = zielx;
y = ziely;
}
string Verkehrsmittel::text()
{
stringstream ausgabe;
ausgabe << "Verkehrsmittel - Position x: " << x << "; Position y: " << y;
return ausgabe.str();
}

View File

@ -0,0 +1,18 @@
#pragma once
#include <string>
#include <iostream>
using namespace std;
class Verkehrsmittel{
protected:
int x;
int y;
public:
Verkehrsmittel () : x(0), y(0) {}
Verkehrsmittel(int x, int y) : x(x), y(y) {}
void bewege(int zielx, int ziely);
string text();
};

View File

@ -7,7 +7,7 @@
int main()
{
//Verkehrsmittel
Verkehrsmittel verkehrsmittel;
Verkehrsmittel verkehrsmittel;
cout << verkehrsmittel.text() << endl;
assert(verkehrsmittel.text().compare("Verkehrsmittel - Position x: 0; Position y: 0") == 0 && "String must be: Verkehrsmittel - Position x: 0; Position y: 0");
verkehrsmittel.bewege(10,10);

13
6_Verkehrsmittel/pkw.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "PKW.h"
#include "Verkehrsmittel.h"
#include "Radfahrzeug.h"
#include <sstream>
string PKW::text()
{
stringstream ausgabe;
ausgabe << "PKW - Position x: " << x << "; Position y: " << y << "; Anzahl Räder: " << anzahl_raeder << "; Anzahl Türen: " << anzahl_tueren;
return ausgabe.str();
}

22
7_Musiker/code/Klavier.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <string>
#include "Musikinstrument.h"
class Klavier : public Musikinstrument
{
public:
Klavier() : Musikinstrument("Klavier")
{
std::cout << "Konstruktor : Klavier() : " << std::endl;
}
virtual ~Klavier()
{
std::cout << "Destruktor : ~Klavier() : " << std::endl;
}
void spielen() const
{
std::cout << "Das Klavier klimpert" << std::endl;
}
};

52
7_Musiker/code/Musiker.h Normal file
View File

@ -0,0 +1,52 @@
#pragma once
#include <string>
#include <iostream>
#include "Musikinstrument.h"
class Musiker
{
std::string const name;
Musikinstrument const* instrument;
public:
Musiker(const std::string& n) : name(n), instrument(nullptr)
{
std::cout << "Konstruktor : Musiker(string name) : " << name << std::endl;
}
Musiker() : name("Name unbekannt"), instrument(nullptr)
{
std::cout << "Konstruktor : Musiker() : " << name << std::endl;
}
virtual ~Musiker()
{
std::cout << "Destruktor : ~Musiker() : " << name << std::endl;
delete instrument;
}
void weiseInstrumentZu(Musikinstrument const* instrumentToAssign)
{
if(instrument != nullptr)
{
std::cout << "Der Musiker " << name << " hat bereits ein Instrument zugewiesen bekommen. Das alte Instrument wird durch das neue ersetzt" << std::endl;
delete instrument;
}
instrument = instrumentToAssign;
std::cout << "Der Musiker " << name << " erhält das Instrument " << instrument->liefereName() << std::endl;
}
void spielen() const
{
if(instrument != nullptr)
{
std::cout << name << " spielt " << instrument->liefereName() << ": " <<std::endl;
instrument->spielen();
}
else
{
std::cout << "Der Musiker " << name << " hat kein Instrument zugewiesen bekommen und kann daher nicht spielen." << std::endl;
}
}
std::string liefereName() const { return name; }
};

View File

@ -0,0 +1,28 @@
#pragma once
#include <string>
#include <iostream>
class Musikinstrument
{
std::string const name;
public:
Musikinstrument(const std::string& n) : name(n)
{
std::cout << "Konstruktor : Musikinstrument(string name) : " << name << std::endl;
}
Musikinstrument() : name("Irgendein Musikinstrument")
{
std::cout << "Konstruktor : Musikinstrument() : " << name << std::endl;
}
virtual ~Musikinstrument() { std::cout << "Destruktor : ~Musikinstrument() : " << name << std::endl; }
virtual void spielen() const
{
std::cout << "Irgendein Musikinstrument erklingt irgendwie." << std::endl;
}
std::string liefereName() const { return name; }
};

View File

@ -0,0 +1,38 @@
#pragma once
#include <string>
#include <iostream>
#include <vector>
#include "Musiker.h"
class Orchester
{
std::vector<Musiker const*> musiker;
public:
Orchester()
{
std::cout << "Konstruktor : Orchester() : " << std::endl;
}
virtual ~Orchester()
{
std::cout << "Destruktor : ~Orchester() : " << std::endl;
for(Musiker const* m : musiker)
{
delete m;
}
}
void spielen() const
{
for(Musiker const* m : musiker)
{
m->spielen();
}
}
void hinzufuegen(Musiker const* m)
{
musiker.push_back(m);
}
};

22
7_Musiker/code/Trompete.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <string>
#include "Musikinstrument.h"
class Trompete : public Musikinstrument
{
public:
Trompete() : Musikinstrument("Trompete")
{
std::cout << "Konstruktor : Trompete() : " << std::endl;
}
virtual ~Trompete()
{
std::cout << "Destruktor : ~Trompete() : " << std::endl;
}
void spielen() const
{
std::cout << "Die Trompete trötet" << std::endl;
}
};

View File

@ -0,0 +1,18 @@
#include "Atomkern.h"
const Atomkern Atomkern::PROTON = Atomkern("p", 1, 1);
const Atomkern Atomkern::NEUTRON = Atomkern("n", 1, 0);
std::ostream& operator << (std::ostream& os, const Atomkern& atomkern)
{
os << atomkern._elementsymbol << "(" << atomkern._massenzahl << "," << atomkern._ordnungszahl << ")";
return os;
}
Atomkern operator * (int factor, const Atomkern& atomkern)
{
Atomkern ergebnis;
ergebnis._ordnungszahl = atomkern._ordnungszahl * factor;
ergebnis._massenzahl = atomkern._massenzahl * factor;
return ergebnis;
}

View File

@ -0,0 +1,116 @@
#pragma once
#include <string>
#include <sstream>
#include <iostream>
#include <ostream>
class Atomkern
{
public:
static const Atomkern PROTON;
static const Atomkern NEUTRON;
std::string _elementsymbol;
int _ordnungszahl;
int _massenzahl;
Atomkern(std::string elementsymbol, int massenzahl, int ordnungszahl)
{
this->_elementsymbol = elementsymbol;
this->_ordnungszahl = ordnungszahl;
this->_massenzahl = massenzahl;
}
Atomkern(int massenzahl, int ordnungszahl)
{
this->_elementsymbol = " ";
this->_ordnungszahl = ordnungszahl;
this->_massenzahl = massenzahl;
}
Atomkern() // Standardatomkern: Wasserstoff
{
this->_elementsymbol = "H";
this->_ordnungszahl = 1;
this->_massenzahl = 1;
}
~Atomkern() {}
std::string symbol() const
{
return this->_elementsymbol;
}
int ordnungszahl() const
{
return this->_ordnungszahl;
}
int massenzahl() const
{
return this->_massenzahl;
}
Atomkern operator + (const Atomkern& atomkern_to_add) const
{
Atomkern ergebnis;
ergebnis._ordnungszahl = this->_ordnungszahl + atomkern_to_add._ordnungszahl;
ergebnis._massenzahl = this->_massenzahl + atomkern_to_add._massenzahl;
return ergebnis;
}
void operator += (const Atomkern& atomkern_to_add)
{
this->_ordnungszahl += atomkern_to_add._ordnungszahl;
this->_massenzahl += atomkern_to_add._massenzahl;
}
Atomkern operator - (const Atomkern& atomkern_to_subtract) const
{
Atomkern ergebnis;
ergebnis._ordnungszahl = this->_ordnungszahl - atomkern_to_subtract._ordnungszahl;
ergebnis._massenzahl = this->_massenzahl - atomkern_to_subtract._massenzahl;
return ergebnis;
}
void operator -= (const Atomkern& atomkern_to_subtract)
{
this->_ordnungszahl -= atomkern_to_subtract._ordnungszahl;
this->_massenzahl -= atomkern_to_subtract._massenzahl;
}
Atomkern operator * (const int factor) const
{
Atomkern ergebnis;
ergebnis._ordnungszahl = this->_ordnungszahl * factor;
ergebnis._massenzahl = this->_massenzahl * factor;
return ergebnis;
}
int operator == (const Atomkern& atomkern_to_compare) const
{
if (this->_ordnungszahl == atomkern_to_compare._ordnungszahl && this->_massenzahl == atomkern_to_compare._massenzahl)
{
return 1;
}
else
{
return 0;
}
}
};
std::ostream& operator<<(std::ostream& os, const Atomkern& atomkern);
Atomkern operator*(int factor, const Atomkern& atomkern);

View File

@ -5,7 +5,7 @@
*/
#include "PrueffolgeReaktor.h"
#include "Atomkern.h"
#include "../Atomkern/Atomkern.h"
#include "Reaktor.h"
using namespace std;

View File

@ -0,0 +1,39 @@
#pragma once
#include "../Atomkern/Atomkern.h"
class Reaktor
{
int _massenzahl_summe;
int _ordnungszahl_summe;
public:
Reaktor() : _massenzahl_summe(0), _ordnungszahl_summe(0) {}
~Reaktor() {}
Reaktor& operator < (const Atomkern& atomkern_to_add)
{
this->_massenzahl_summe += atomkern_to_add.massenzahl();
this->_ordnungszahl_summe += atomkern_to_add.ordnungszahl();
return *this;
}
Reaktor& operator > (const Atomkern& atomkern_to_subtract)
{
this->_massenzahl_summe -= atomkern_to_subtract.massenzahl();
this->_ordnungszahl_summe -= atomkern_to_subtract.ordnungszahl();
return *this;
}
void operator ~ ()
{
this->_massenzahl_summe = 0;
this->_ordnungszahl_summe = 0;
}
Atomkern operator * () const
{
return Atomkern(this->_massenzahl_summe, this->_ordnungszahl_summe);
}
};

View File

@ -0,0 +1,66 @@
#include <iostream>
#include <vector>
using namespace std;
struct Produkt {
virtual ~Produkt() {}
virtual string info() = 0;
};
struct Fabrik {
virtual ~Fabrik() {}
virtual Produkt* erzeuge(string const&) = 0;
};
class KonkretesProdukt : public Produkt {
private:
string const _info;
public:
KonkretesProdukt(const string& i) : _info(i) {}
virtual string info() {return _info;}
};
class KonkreteFabrik : public Fabrik {
private:
vector<Produkt*> produkte;
public:
~KonkreteFabrik() {
for(Produkt* p : produkte) {
cout << "Lösche Produkt " << p->info() << endl;
delete p;
}
}
Produkt* erzeuge(const string& s) {
Produkt* newProduct = new KonkretesProdukt(s);
produkte.push_back(newProduct);
return newProduct;
}
};
int main() {
Fabrik* fabrik = new KonkreteFabrik();
Produkt* p1 = fabrik->erzeuge("p1");
cout << "Erzeugtes Produkt: " << p1->info() << endl;
Produkt* p2 = fabrik->erzeuge("p2");
cout << "Erzeugtes Produkt: " << p2->info() << endl;
delete fabrik;
}
/*
Erwartete Ausgabe:
Erzeugtes Produkt: p1
Erzeugtes Produkt: p2
Lösche Produkt p1
Lösche Produkt p2
*/

View File

@ -0,0 +1,50 @@
#include <iostream>
#include <string>
using namespace std;
template <typename T>
struct A {
const T x;
A(T x) : x(x) {}
};
template<typename T>
ostream& operator<<(ostream& os, const A<T>& a) {
os << a.x;
return os;
};
template<typename T, typename S>
struct B : public A<T> {
const S y;
const A<S> a;
B(const T& t, const S& s1, const S& s2) : A<T>(t), y(s1), a(s2) {}
};
template<typename T, typename S>
ostream& operator<<(ostream& os, const B<T,S>& b) {
os << b.x <<" "<< b.y<<" "<<b.a<<endl;
return os;
};
int main() {
A<int> a = 1;
cout << a << endl;
B<string,float> b = {"xyz", 2.6, 1.8};
cout << b << endl;
}
/*
Erwartete Ausgabe:
1
xyz 2.6 1.8
*/

View File

@ -0,0 +1,62 @@
#include <iostream>
using namespace std;
class A
{
public:
~A() {}
void const a() {}
};
class P
{
public:
int x;
int y;
P() : x(0), y(0) {}
P(int x, int y) : x(x), y(y) {}
};
ostream& operator << (ostream& os, P p)
{
os << "(" << p.x << "," << p.y << ")";
return os;
}
class B : public A
{
P p;
public:
B(const P& p) : p(p) {}
protected:
void const b()
{
cout << "B::p = " << p;
}
};
class C : public B
{
public:
C(const P& p) : B(p) {}
void const a()
{
B::b();
}
};
int main()
{
P p(19,20);
cout << "p=" << p << endl;
C c(p);
cout << "c.a() liefert: ";
c.a();
}

View File

View File

@ -0,0 +1,34 @@
#include <iostream>
using namespace std;
struct A
{
void f()
{
cout << "A::f\n";
g();
h();
}
virtual void g() { cout << "A::g\n"; }
void h() { cout << "A::h\n"; }
};
struct B : A
{
void g() { cout << "B::g\n"; }
virtual void h() { cout << "B::h\n"; }
};
struct C : B
{
virtual void g() { cout << "C::g\n"; }
virtual void h() { cout << "C::h\n"; }
};
int main()
{
B b;
C c;
B *z = new C;
b.f();
z->g();
z->h();
c.f();
delete z;
}

View File

@ -0,0 +1,94 @@
#include <iostream>
using namespace std;
struct A
{
static int n;
const int id;
char *x;
A(int i) : id(++n)
{
cout << "A(int) " << id << "\n";
x = new char(i);
}
A(const A &a) : id(++n)
{
cout << "A(const A&) " << id << "\n";
x = new char(*(a.x));
}
A(A &&a) : id(++n)
{
cout << "A(A&&) " << id << "\n";
x = a.x;
a.x = nullptr;
}
virtual ~A()
{
cout << "~A() " << id << "\n";
if (x != nullptr)
{
delete x;
}
}
A &operator=(const A &a)
{
cout << "operator=(constA&) " << id << " " << a.id << endl;
if (&a != this)
{
if (x == nullptr)
{
x = new char(*(a.x));
}
else
{
*x = *(a.x);
}
}
return *this;
}
A &operator=(A &&a)
{
cout << "operator=(constA&&) " << id << " " << a.id << endl;
if (&a != this)
{
if (x != nullptr)
{
delete x;
}
x = a.x;
a.x = nullptr;
}
return *this;
}
};
int A::n = 0;
ostream &operator<<(ostream &os, const A &a)
{
os << a.id << " ";
if (a.x != nullptr)
{
os << *(a.x);
}
return os;
}
int main()
{
A a1 = {'X'};
cout << a1 << "\n"
<< endl;
A a2 = {a1};
cout << a2 << "\n"
<< endl;
A a3 = {move(a1)};
cout << a1 << endl;
cout << a3 << endl;
a1 = move(a3);
cout << a1 << endl;
cout << a3 << endl;
a3 = a1;
cout << a1 << endl;
cout << a3 << endl;
}

View File

@ -0,0 +1,69 @@
#include <iostream>
#include <memory>
#include <stdexcept>
using namespace std;
struct A
{
int i;
A(int i) : i(i) { cout << "A()\n"; }
A(A &&a) : i(a.i) { cout << "A(A&&)\n"; }
~A() { cout << "~A()\n"; }
};
ostream &operator<<(ostream &os, const unique_ptr<A> &z)
{
if (z == nullptr)
{
os << "nullptr";
}
else
{
os << z->i;
}
return os;
}
void e(int i)
{
if (i == 0)
{
throw exception();
}
else
{
throw invalid_argument("");
}
}
void f1()
{
A *z = new A(1);
e(0);
delete z;
cout << "Ende f1()\n";
}
void f2()
{
unique_ptr<A> z(new A(2));
e(1);
cout << "Ende f2()\n";
}
int main()
{
try { f1();}
catch(const exception& e) {cout << "S\n";}
catch(const invalid_argument& e) {cout << "T\n";}
try {f2();}
catch(const invalid_argument& e) {cout << "T\n";}
catch(const exception& e) {cout << "S\n";}
unique_ptr<A> z1(new A{9});
cout << z1 << endl;
unique_ptr<A> z2(move(z1));
cout << z1 << endl;
cout << z2 << endl;
}

View File

@ -0,0 +1,59 @@
#include <iostream>
using namespace std;
struct A
{
int i;
A(int i) : i(i) {}
A(const A &a) : i(a.i) {}
A &operator=(int i)
{
this->i = i;
return *this;
}
A &operator-()
{
i = -i;
return *this;
}
A operator-() const
{
return A{-i};
}
A &operator-(const A &a)
{
i -= a.i;
return *this;
}
A operator-(const A &a) const
{
return A{i - a.i};
}
};
ostream &operator<<(ostream &os, const A &a)
{
os << a.i;
return os;
}
int main()
{
A a1{1};
const A a2{2};
A a3{-a1};
cout << a1 << " " << a3 << endl;
A a4{-a2};
cout << a2 << " " << a4 << endl;
a4 = 4;
a1 = -a4;
cout << a1 << " " << a4 << endl;
A a5 = a1-a4;
cout << a1 << " " << a4 << " " << a5 << endl;
const A a6{11};
cout << -(a2-a6);
}

View File

@ -0,0 +1,18 @@
#include <iostream>
#include <stdexcept>
using namespace std;
void f() { throw invalid_argument("1"); }
void g() { throw runtime_error("2"); }
int main() {
try { f(); }
try { g(); }
catch(const runtime_error& e) { cout << "3 " << e.what() << " "; }
catch(const invalid_argument& e) { cout << "4 " << e.what() << " "; }
catch(const exception& e) { cout << "5 " << e.what() << " "; }
//catch(const exception& e) { cout << "6 " << e.what() << " "; }
catch(const invalid_argument& e) { cout << "7 " << e.what() << " "; }
catch(const runtime_error& e) { cout << "8 " << e.what() << " "; }
catch(const exception& e) { cout << "6 " << e.what() << " "; }
}