Inhalt:
class whatever {
private:
// ...
protected:
// ...
public:
// ...
};
Die verschiedenen Bereiche geben an wer Zugriff hat, wobei
Zugriff hier lesen und schreiben bei Attributen und
aufrufen bei Methoden bedeutet. Im Detail:
class foo : public bar {
...
};
Bei Vererbung kann angegeben werden, wie die geerbten Attribute
und Methoden weitergegeben werden:
class Complex {
private:
double re, im;
public:
Complex(double r=0.0, double i=0.0){ re=r; im=i; }
friend void print_Complex(Complex &x);
};
// Friend-Funktion von Complex, gehoert nicht zur Klasse Complex
// aber darf auf private Members zugreifen
//
void print_Complex(Complex &c)
{
cout << c.re << "+" << c.im << "i" << endl;
}
int main(void)
{
Complex x;
print_Complex(x);
class Complex {
double re, im;
public:
Complex(double r=0.0, double i=0.0){ re=r; im=i; }
void print(void){ printf("(%.2f+%.2fi)", re, im); }
// Deklaration Member-Funktion:
Complex add(Complex op);
// Deklaration globale (-> friend) Funktion:
friend Complex sub_Complex(Complex a, Complex b);
};
// Definition Member-Funktion:
Complex Complex::add(Complex op)
{
return Complex(re + op.re, im + op.im);
}
// Definition globale Funktion:
Complex sub_Complex(Complex a, Complex b)
{
return Complex(a.re - b.re, a.im - b.im);
}
// Hauptprogramm:
int main(void)
{
Complex a(1,0), b(0,1), c(0,0), d(0,0);
printf("a="); a.print(); printf(", ");
printf("b="); b.print(); printf("\n");
// Abwendung Member-Funktion:
c = a.add(b);
printf("c="); c.print(); printf("\n");
// Anwendung globale Funktion:
d = sub_Complex(a,b);
printf("d="); d.print(); printf("\n");
return 0;
}
Ausgabe:
a=(1.00+0.00i), b=(0.00+1.00i) c=(1.00+1.00i) d=(1.00+-1.00i)(Quellcode)
class Complex {
...
// Deklaration Member-Funktion:
Complex operator+(Complex op);
// Deklaration globale (-> friend) Funktion
friend Complex operator-(Complex a, Complex b);
};
// Member-Funktion:
Complex Complex::operator+(Complex op)
{
return Complex(re + op.re, im + op.im);
}
// Globale Funktion:
Complex operator-(Complex a, Complex b)
{
return Complex(a.re - b.re, a.im - b.im);
}
int main(void)
{
Complex a(1,0), b(0,1), c(0,0);
c = a + b;
printf("c="); c.print(); printf("\n");
d = a - b;
printf("d="); d.print(); printf("\n");
return 0;
}
| Overloadable operators |
|---|
+ - * / = < > += -= *= /= << >> <<= >>= == != <= >= ++ -- % & ^ ! | ~ &= ^= |= && || %= [] () , ->* -> new delete new[] delete[] |
| Expression | Operator | Member function | Global function |
|---|---|---|---|
| @a | + - * & ! ~ ++ -- | A::operator@() | operator@(A) |
| a@ | ++ -- | A::operator@(int) | operator@(A,int) |
| a@b | + - * / % ^ & | < > == != <= >= << >> && || , | A::operator@ (B) | operator@(A,B) |
| a@b | = += -= *= /= %= ^= &= |= <<= >>= [] | A::operator@ (B) | - |
| a(b, c...) | () | A::operator() (B, C...) | - |
| a->x | -> | A::operator->() | - |
(Quelle)
class Complex {
...
// Achtung!
// Keine Member-Funktion, sondern hier nur inline Deklariert!!!
//
friend ostream& operator<<(ostream &os, Complex &c) {
os << c.re << "+" << c.im << "i";
return os; // Für weitere Ausgaben
}
};
int main(void)
{
int i = 17;
double f = 4.2;
Complex a(1,0), b(0,1), c(0,0), d(0,0);
c = a + b;
d = a - b;
cout << "i=" << i << endl;
cout << "f=" << f << endl;
cout << "c=" << c << endl;
cout << "d=" << d << endl;
Ausgabe:
i=17 f=4.2 c=1+1i d=1+-1i(Quellcode)
#include <iostream>
using namespace std;
class String {
...
};
class String {
...
};
#include "hersteller1.h" #include "hersteller2.h" String s; // Welche String-Klasse wird verwendet?
namespace Hersteller1 {
class String {
...
};
}
namespace Hersteller2 {
class String {
...
};
}
#include "hersteller1.h" #include "hersteller2.h" using namespace Hersteller1; String s1; Hersteller2::String s2;
#include <complex>
int main(void)
{
complex<double> a(1.0, 0.0);
complex<double> b(0.0, 1.0);
complex<double> c(0.0, 0.0);
c = a + b;
cout << a << "+" << b << "=" << c << endl;
Ausgabe:
(1,0)+(0,1)=(1,1)
(Quellcode)
class Complex {
double re, im;
public:
Complex(double r=0.0, double i=0.0){ re=r; im=i; }
Complex operator+(Complex op);
friend ostream& operator<<(ostream &os, Complex &c);
};
Complex Complex::operator+(Complex op);
{
// Achtung, folgende Funktionen werden im Folgenden aufgerufen,
// und muessen damit definiert sein:
//
// 1) double operator+(double, double)
// 2) Complex(double, double)
//
return Complex(re + op.re, im + op.im);
}
ostream& operator<<(ostream &os, Complex &c);
{
// Achtung, folgende Funktion wird im Folgenden aufgerufen,
// und muß damit definiert sein:
//
// ostream &operator<<(ostream &, double)
//
os << c.re << "+" << c.im << "i";
return os;
}
int main(void)
{
Complex a(1.0, 0.0);
Complex b(0.0, 1.0);
Complex c(0.0, 0.0);
c = a + b;
cout << a << "+" << b << "=" << c << endl;
}
template <typename DATA>
class Complex {
DATA re, im;
public:
Complex<DATA>(DATA r=0.0, DATA i=0.0){ re=r; im=i; }
Complex<DATA> operator+(Complex<DATA> op);
...
};
template <typename DATA>
Complex<DATA> Complex<DATA>::operator+(Complex<DATA> op)
{
// Achtung, folgende Funktionen werden im Folgenden aufgerufen,
// und muessen damit definiert sein:
//
// 1) DATA operator+(DATA, DATA)
// 2) Complex(DATA, DATA)
//
return Complex<DATA>(re + op.re, im + op.im);
}
int main(void)
{
Complex<double> a(1.0, 0.0);
Complex<double> b(0.0, 1.0);
Complex<double> c(0.0, 0.0);
c = a + b;
template <typename DATA> class Complex;
template <typename DATA> ostream& operator<< /* hier kein <DATA> */ (ostream &os, Complex<DATA> &c);
template <typename DATA>
class Complex {
...
friend ostream& operator<<<DATA>(ostream &os, Complex<DATA> &c);
};
template <typename DATA>
ostream& operator<< /*nix!*/ (ostream &os, Complex<DATA> &c)
{
// Achtung, folgende Funktion wird im Folgenden aufgerufen,
// und muß damit definiert sein:
//
// ostream &operator<<(ostream &, DATA)
//
return os << c.re << "+" << c.im << "i";
}
int main(void)
{
...
cout << a << " + " << b << " = " << c << endl;
return 0;
}
(Quellcode)