6. Mai 2015
Daniel Hentzen
dhentzen@student.ethz.ch
Complex number1;
Complex number2;
number1.set(1,0);
number2.set(4.93,-1);
void Complex::set(double r, double i)
{
real = r;
imag = i;
}
class Complex
{
private:
double real;
double imag;
public:
void set(double r, double i);
};
#include "complex.h"
void Complex::set(double r, double i);
{
real = r;
imag = i;
}
Student stud = {"John", "Doe", 1324142}
Complex c; //implizit
Complex d = Complex(); //explizit
Complex::Complex(double r, double i)
{
real = r;
imag = i;
}
Complex c1 = Complex(1.0,2.0);
Complex c2(1.0, 2.0);
Complex c1; //no longer valid
Complex::~Complex()
{
// destroy the world.
}
Complex c;
...
c.~Complex();
Complex c(8.3, 2.4);
Complex d(0.5, 4.1);
//Member Funktionen, die die arithmetischen Operationen implementieren
c.add(d);
c.sub(d);
c.mult(d);
c.div(d);
Complex e;
e = c + d;
e = c - d;
e = c * d;
e = c / d;
Complex a;
Complex b;
// Aufruf als Operator
Complex s1 = a + b;
// oder normal als Funktion
Complex s2 = a.operator+(b);
int j = 5, k = 6;
const int i = 1;
i = 2; // ERROR
const int *i = &j;
i = &k; //ERROR
*i = k; // ok
const Complex getSum(const Complex a) const;
const Stock & topval(const Stock &s) const; // Prototyp, Funktion verändert Objekt nicht (const)! const Stock & Stock::topval(const Stock &s) const { if (s.total_val > total_val) return s; else return *this; //aufrufendes Objekt }
top = stock1.topval(stock2);
top = stock2.topval(stock1);
void myFunction(){...} //keine member Funktion!
class MyClass{...}; //keine member Klasse!
class Complex
{
public:
friend void myFunction(); //Prototyp friend Funktion
friend class MyClass; //friend Klasse
};
Complex c = Complex(3.0, 1.5);
Complex d = c * 2.3; // ok, d = c.operator*(2.3);
Complex e = 2.3 * c; // Problem!
friend Complex operator*(double m, const Complex &c);
Complex operator*(double m, const Complex &c)
{
Complex result;
result.real = m * c.real;
result.imag = m * c.imag;
return result;
}