| [Il linguaggio di programmazione] C rende facile spararti su un piede; C++ lo rende più difficile, ma quando lo fai, ti fa saltare via l'intera gamba.
Bjarne Stroustrup |
class Ratio {
private:
// attributi
int num, den;
public:
// costruttori
explicit Ratio();
explicit Ratio(int num, int den);
// metodi: getter, setter
int getnum() const;
int getden() const;
#if 0
// metodi: stampa, somma, sottrazione, prodotto, rapporto
void show(std::ostream & os) const;
Ratio add(const Ratio & rhs) const;
Ratio sub(const Ratio & rhs) const;
Ratio mul(const Ratio & rhs) const;
Ratio div(const Ratio & rhs) const;
bool islesser(const Ratio & rhs) const;
#else
// oppure
friend std::ostream & operator<<(std::ostream & os, const Ratio & value);
Ratio operator+(const Ratio & rhs) const;
Ratio operator-(const Ratio & rhs) const;
Ratio operator*(const Ratio & rhs) const;
Ratio operator/(const Ratio & rhs) const;
Ratio & operator+=(const Ratio & rhs);
Ratio & operator-=(const Ratio & rhs);
Ratio & operator*=(const Ratio & rhs);
Ratio & operator/=(const Ratio & rhs);
bool operator<(const Ratio & rhs) const;
#endif
};
int main() {
Ratio r1(1, 3), r2(1, 6);
r1.add(r2).show(std::cout);
// oppure
std::cout << r1+r2 << std::endl;
return 0;
}
#include<cstdint>
class Bitset {
private:
// attributi
// static const unsigned m_size = 1024;
// uint8_t m_data[128]; // 8 bit*128=1024 bit
// std::array m_data;
// std::vector m_data;
std::valarray m_data; // scelta consigliata
public:
// costruttori
explicit Bitset();
#if 0
// metodi: streaming, unione, intersezione, differenza
std::ostream & show(std::ostream & os) const;
Bitset union(const Bitset & rhs) const;
Bitset intersect(const Bitset & rhs) const;
Bitset difference(const Bitset & rhs) const;
bool islesser(const Bitset & rhs) const;
#else
// oppure
friend std::ostream & operator<<(std::ostream & os, const Bitset & value);
Bitset operator+(const Bitset & rhs) const;
Bitset operator*(const Bitset & rhs) const;
Bitset operator-(const Bitset & rhs) const;
Bitset & operator+=(const Bitset & rhs);
Bitset & operator*=(const Bitset & rhs);
Bitset & operator-=(const Bitset & rhs);
bool operator<(const Bitset & rhs) const;
#endif
};
int main() {
Bitset bs1, bs2;
bs1.add(bs2).show();
// oppure
std::cout << bs1+bs2 << std::endl;
return 0;
}
class Complex {
private:
// attributi
float m_real, m_imag;
public:
// costruttori
explicit Complex();
explicit Complex(float re, float im);
// metodi: getter, setter
float real() const;
float imag() const;
#if 0
// metodi: streaming, somma, sottrazione, prodotto, rapporto
friend std::ostream & show(std::ostream & os) const;
Complex add(const Complex & rhs) const;
Complex sub(const Complex & rhs) const;
Complex mul(const Complex & rhs) const;
Complex div(const Complex & rhs) const;
#else
// oppure
friend std::ostream & operator<<(std::ostream & os, const Complex & z);
Complex operator+(const Complex & rhs) const;
Complex operator-(const Complex & rhs) const;
Complex operator*(const Complex & rhs) const;
Complex operator/(const Complex & rhs) const;
Complex & operator+=(const Complex & rhs);
Complex & operator-=(const Complex & rhs);
Complex & operator*=(const Complex & rhs);
Complex & operator/=(const Complex & rhs);
// metodi: pow, root, cos, sin, exp, log
friend Complex pow(unsigned n);
friend Complex root(unsigned n);
friend Complex cos(const Complex & z);
friend Complex sin(const Complex & z);
friend Complex exp(const Complex & z);
friend Complex log(const Complex & z);
#endif
// conversione di tipo
operator std::complex() const;
};
int main() {
Complex z1, z2;
z1.add(z2).show();
// oppure
std::cout << z1+z2 << std::endl;
return 0;
}
class Vector3() {
private:
float m_x, m_y, m_z;
public:
// costruttori
explicit Vector3();
explicit Vector3(float x, float y, float z);
// metodi: getter, setter
float x();
float y();
float z();
#if 0
// metodi: streaming
std::ostream & show(std::ostream & os);
// metodi: somma, sottrazione, prodotto, rapporto
Vector3 add(const Vector3 & rhs) const;
Vector3 sub(const Vector3 & rhs) const;
float ps(const Vector3 & rhs) const;
Vector3 pv(const Vector3 & rhs) const;
#else
// oppure:
friend std::ostream & operator<<(std::ostream & os, const Vector3 & rhs);
Vector3 operator+(const Vector3 & rhs) const;
Vector3 operator-(const Vector3 & rhs) const;
float operator*(const Vector3 & rhs) const;
Vector3 operator^(const Vector3 & rhs) const;
Vector3 operator+=(const Vector3 & rhs);
Vector3 operator-=(const Vector3 & rhs);
float operator*=(const Vector3 & rhs);
Vector3 operator^=(const Vector3 & rhs);
#endif
};
int main() {
Vector3 v1, v2;
v1.add(v2).show();
// oppure
std::cout << v1+v2 << std::endl;
return 0;
}
class Polynomial {
private:
// unsigned m_degree;
// float * m_coeff;
// std::array m_coeff;
// std::vector m_coeff;
std::valarray m_coeff; // scelta consigliata
public:
// costruttori
explicit Polynomial(unsigned degree);
Polynomial(const Polynomial & rhs); // copy constructor
Polynomial(Polynomial && rhs); // move constructor (solo C++11)
Polynomial & operator=(const Polynomial & rhs); // copy assignment
Polynomial & operator=(const Polynomial && rhs); // move assignment (solo C++11)
~Polynomial();
#if 0
// metodi: streaming
std::ostream & show(std::ostream & os);
// metodi: somma, sottrazione, prodotto, rapporto
Polynomial add(const Polynomial & rhs) const;
Polynomial sub(const Polynomial & rhs) const;
Polynomial mul(const Polynomial & rhs) const;
Polynomial div(const Polynomial & rhs) const;
#else
friend std::ostream & operator<<(std::ostream & os, const Polynomial & v);
Polynomial operator+(const Polynomial & rhs) const;
Polynomial operator-(const Polynomial & rhs) const;
Polynomial operator*(const Polynomial & rhs) const;
Polynomial operator/(const Polynomial & rhs) const;
Polynomial & operator+=(const Polynomial & rhs);
Polynomial & operator-=(const Polynomial & rhs);
Polynomial & operator*=(const Polynomial & rhs);
Polynomial & operator/=(const Polynomial & rhs);
#endif
float eval(float x);
};
int main() {
Polynomial p1(3), p2(3);
std::cout << p1 << std::endl;
std::cout << p2 << std::endl;
std::cout << p1+p2 << std::endl;
std::cout << p1-p2 << std::endl;
std::cout << p1*p2 << std::endl;
std::cout << p1/p2 << std::endl;
std::cout << p1+=p2 << std::endl;
std::cout << p1-=p2 << std::endl;
std::cout << p1*=p2 << std::endl;
std::cout << p1/=p2 << std::endl;
{ // Rule of three
// copy costructor
Polynomial p3(p1);
Polynomial p4=p2;
Polynomial p5(std::copy(p1));
Polynomial p6=std::copy(p2);
// operator= (copy assignment)
p3=p1;
p3=std::copy(p1);
// destructor
}
{ // Rule of five (completion)
// initialization (move costructor, solo C++11)
Polynomial p3(std::move(p1));
Polynomial p4=std::move(p2);
// function argument passing: f(std::move(a)); f pass by value
p3.add(std::move(p1));
// function return: return a; inside a function such as T f(), where a is of type T which has a move constructor.
return p3;
// operator= (move assignment, solo C++11)
p3=std::move(p1);
}
return 0;
}
class Interpolate {
private:
float * m_coeff;
std::vector m_coeff;
public:
// costruttori
explicit Interpolate(std::vector & x, std::vector & y);
// metodi: streaming
std::ostream & show(std::ostream & os);
// metodi: compute (in overload)
float compute(float x);
std::vector compute(std::vector & x);
};
int main() {
std::vector x = { 0.0, 0.50, 1.0, 1.50, 2.0, };
std::vector y = { 0.0, 0.25, 1.0, 2.25, 4.0, };
Interpolate i(x, y);
std::cout << i.compute(1.25) << std::endl;
return 0;
}
class Integrate {
private:
public:
// costruttori
explicit Integrate();
Integrate(float x, float y, float z);
Integrate(const Integrate & v);
// metodi: getter, setter
float x() const;
float y() const;
float z() const;
// metodi: streaming
std::ostream & show(std::ostream & os);
// metodi: somma, sottrazione, prodotto, rapporto
Integrate pv(const Integrate & rhs);
};
int main() {
Integrate i;
std::cout << i << std::endl;
return 0;
}
class Matrix {
private:
unsigned m_size;
float * m_mat;
public:
// costruttori
Matrix(unsigned n);
Matrix(const Matrix & m); // copy constructor
Matrix(Matrix && m); // move constructor
~Matrix(); // destructor
// metodi: altro
float det() const;
float trace() const;
Matrix inv() const;
#if 0
// metodi: streaming, somma, sottrazione, prodotto
std::ostream & show(std::ostream & os);
Matrix add(const Matrix & rhs) const;
Matrix sub(const Matrix & rhs) const;
Matrix mul(const Matrix & rhs) const;
#else
// oppure
friend std::ostream & show(std::ostream & os, const Matrix & value);
Matrix operator+(const Matrix & rhs) const;
Matrix operator-(const Matrix & rhs) const;
Matrix operator*(const Matrix & rhs) const;
Matrix & operator+=(const Matrix & rhs);
Matrix & operator-=(const Matrix & rhs);
Matrix & operator*=(const Matrix & rhs);
float & operator()(unsigned row, unsigned col);
#endif
};
int main() {
Matrix matrix(4);
std::cout << matrix << std::endl;
std::cout << matrix1+matrix2 << std::endl;
std::cout << matrix1-matrix2 << std::endl;
std::cout << matrix1*matrix2 << std::endl;
std::cout << (matrix1^matrix2) << std::endl;
return 0;
}
class Counter {
private:
unsigned m_value;
public:
// costruttori
explicit Counter();
// metodi: getter
unsigned value() const;
#if 0
// streaming
std::ostream show(std::ostream & os) const;
// metodi: altro
void inc();
void dec();
#else
// oppure
friend std::ostream & operator<<(std::ostream & os, const Counter & v);
Counter & operator++();
Counter & operator++(int);
Counter & operator--();
Counter & operator--(int);
#endif
};
int main() {
Counter c;
std::cout << c << std::endl;
std::cout << ++c << std::endl;
std::cout << c++ << std::endl;
std::cout << --c << std::endl;
std::cout << c-- << std::endl;
return 0;
}
class Survey {
private:
// unsigned m_size;
// unsigned m_value[4];
std::vector m_value; // scelta consigliata
// std::valarray m_value;
public:
// costruttori
explicit Survey();
explicit Survey(unsigned n);
// metodi: getter
unsigned size() const;
void vota(size_t x);
#if 0
// streaming
std::ostream show(std::ostream & os) const;
// metodi: altro
#else
// oppure
friend std::ostream & operator<<(std::ostream & os, const Survey & v);
unsigned & operator[](unsigned index);
#endif
};
int main() {
Survey s(3);
s.vota(2);
s.vota(1);
s.vota(2);
s.vota(1);
s.vota(0);
s.vota(1);
std::cout << s << std::endl;
std::cout << s[0] << std::endl;
std::cout << s[1] << std::endl;
std::cout << s[2] << std::endl;
return 0;
}
class String {
private:
unsigned m_length;
char * m_cstr; // scelta consigliata
public:
// costruttori
explicit String();
explicit String(const char * str);
// metodi: getter
unsigned length() const;
unsigned size() const;
}