long fakultaet(int n);
#include <iostream>
class punkt {
// private: // Obsolet, da Standard in class
double m_x;
double m_y;
public:
// Setter
void setzeX(double x) {
m_x = x;
}
void setzeY(double y) {
m_y = y;
}
// Getter
double gibX() const {
return m_x;
}
double gibY() const {
return m_y;
}
// Methodendefinition
void verschiebe(double x_v, double y_v);
bool gleich(punkt const& p) {
p.gibX(); // Nur möglich, da gix() const ist.
return p.m_x == m_x && p.m_y == m_y;
}
};
// Funktionsimplementierung außerhalb von Klasse.
// Implementierung kann auch in einer anderen Datei geschehen.
void punkt::verschiebe(double x_v, double y_v) {
m_y += y_v;
m_x += x_v;
}
int main() {
punkt p1, p2, p3; // 3 Objekte
p1.setzeX(5);
std::cout << p1.gibX() << std::endl;
}
#include <iostream>
using namespace std;
class Punkt {
private:
double m_x;
double m_y;
public:
Punkt(double wx = 0.0, double wy = 0.0) : m_x(wx), m_y(wy) {
}
double getX(void) {
return m_x;
}
};
int main() {
Punkt p1{};
cout << p1.getX() << endl;
Punkt p2{2.0};
cout << p2.getX() << endl;
Punkt p3{3.0, 4.0};
cout << p3.getX() << endl;
}
struct
)private
und public
die Sichtbarkeit von Attributen und Methodenprivate
#include <iostream>
using namespace std;
class Punkt {
double m_x;
double m_y;
public:
Punkt() {
m_x = 42.0;
m_y = 21.0;
}
Punkt(double wx = 0.0, double wy = 0.0) : m_x(wx), m_y(wy) { }
double getX(void) {
return m_x;
}
};
int main() {
Punkt p1{};
cout << p1.m_y << endl;
cout << p1.getX() << endl;
Punkt p2{2.0};
cout << p2.getX() << endl;
Punkt p3{3.0, 4.0};
cout << p3.getX() << endl;
}
#include <iostream>
using namespace std;
class Math {
double m_pi = 3; // Pi ist glatt drei! :-)
double m_boltzmann;
public:
Math() = default;
double getPi(void) {
return m_pi;
}
double getBoltzmann(void) {
return m_boltzmann;
}
};
int main() {
Math m;
cout << m.getPi() << endl;
cout << m.getBoltzmann() << endl;
}
#include <iostream>
using namespace std;
class Math {
double m_pi;
public:
Math(double _pi) : m_pi(_pi) {}
Math(Math const & _m) : m_pi(_m.m_pi + 10) {
cout << "Ich bin die Kopie!" << endl;
}
double getPi(void) {
return m_pi;
}
};
int main() {
Math m(3.0);
cout << m.getPi() << endl;
Math k = m;
cout << k.getPi() << endl;
}
#include <iostream>
using namespace std;
class MeinFeld {
int s;
int *feld;
public:
MeinFeld(): MeinFeld(10) {}
MeinFeld(int n): s(n) {
feld = new int[s];
cout << "Habe 'feld' mit " << s << " elementen belegt: " << feld << endl;
}
int size() const { return s; }
~MeinFeld() {
cout << "Gebe 'feld' frei: " << feld << endl;
delete[] feld;
}
};
int main() {
MeinFeld a(42);
return 0;
}
#include <iostream>
using namespace std;
class MyClass {
int m_id;
string m_name;
public:
static int s_nextID;
MyClass(): MyClass("empty") {}
MyClass(string _s): m_id(s_nextID++), m_name(_s) {}
int getID() const { return m_id; }
};
int MyClass::s_nextID = 42;
int main() {
cout << MyClass::s_nextID << endl;
MyClass k1{"NameA"};
MyClass k2{"NameB"};
MyClass k3{"NameC"};
cout << k3.getID() << endl;
return 0;
}
main()
)::
anstatt .
→ MyClass::next_ID
#include <iostream>
using namespace std;
template<typename T>
class Punkt {
private:
T m_x, m_y;
public:
Punkt(): Punkt(0,0) {}
Punkt(T _x, T _y): m_x(_x), m_y(_y) {}
void setX(T _x) { m_x = _x; }
void setY(T _y) { m_y = _y; }
T getX(void) const { return m_x; }
T getY(void) const { return m_y; }
};
int main() {
Punkt<int> p1{3.0,4};
cout << "(" << p1.getX() << "," << p1.getY() << ")" << endl;
Punkt<double> p2{3.0,4.12};
cout << "(" << p2.getX() << "," << p2.getY() << ")" << endl;
Punkt<char> p3{'K','o'};
cout << "(" << p3.getX() << "," << p3.getY() << ")" << endl;
return 0;
}