Advertisement
marcomoltisanti

Ereditarieta

Feb 12th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. class A {
  2.     int x;
  3. public:
  4.     A(int _x) : x(_x) {}
  5.     virtual int f(int) =0;
  6. };
  7.  
  8. class E {
  9.     float coeff;
  10. public:
  11.     E(double c) {coeff = (float) c;}
  12.     double getCoeff() {return (double) coeff;}
  13.     int somma(int[], int);
  14. };
  15.  
  16. int E::somma(int v[], int n) {
  17.     int somma = 0;
  18.     for(int i=0; i < n; i++)
  19.         somma += v[i];
  20.     return somma;
  21. }
  22.  
  23. //IL COSTRUTTORE CREA UN'ISTANZA DI E, E PASSA IL PARAMETRO d
  24. // ALL'ATTRIBUTO COEFF;
  25. class B : public A{
  26.     E e;
  27. public:
  28.     B(int x, double d) : A(x), e(d) {}
  29.     float getCoeff() { return (float) e.getCoeff();}
  30.     E getE() {return e;}
  31.  
  32. };
  33.  
  34. //attrib: - z int[*]
  35. //metodi
  36. // + C(x: int, d: double, q: int)
  37. // + f(x: int) : int
  38. // + ultimo() : int
  39. // il costruttore crea un array di lunghezza x e poi
  40. // inizializza la locazione di indice i con il valore (2*q+i+1)
  41. // f restituisce il metodo somma con parametro z
  42. // ultimo restituisce l'ultimo elemento di z
  43. class C : public B{
  44.     int* z;
  45.     int n;
  46. public:
  47.     C(int x, double d, int q) :B(x,d) {
  48.         //B(x, d);
  49.         n = x;
  50.         z = new int[x];
  51.         for(int i=0; i < x; i++) {
  52.             z[i] = 2*q+i+1;
  53.         }
  54.     }
  55.  
  56.     int f(int x) { return getE().somma(z, x);}
  57.     int ultimo() {return z[n-1];}
  58.  
  59.  
  60. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement