Advertisement
xSiRON

LAB_ES2

Jan 29th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. /*
  2.     Progettare ed implementare una classe che rappresenta il cerchio.
  3.  
  4.     Fornire le funzionalità di calcolo dell'area e del perimetro
  5.     i metodi di accesso agli attributi.
  6.  
  7.     Successivamente, utilizzare la classe cerchio per definire un cilindro,
  8.     fornendo la funzionalità di calcolo della superficie totale e del
  9.     volume.
  10. */
  11.  
  12. #include <iostream>
  13. #include <sstream>
  14. using namespace std;
  15.  
  16. class Cerchio{
  17. private:
  18.     float r;
  19.  
  20. public:
  21.     Cerchio(float _x) : r(_x) {}
  22.     void setRaggio(float _r) { r = _r; }
  23.     float getRaggio() { return r; }
  24.     float circ(){ return 2 * r * 3.14; }
  25.     float area(){ return r * r * 3.14; }
  26.     string toString();
  27. };
  28.  
  29. string Cerchio::toString(){
  30.     stringstream s;
  31.     s << "Il cerchio ha raggio = " << r << ", area = " << area()
  32.       << ", circonferenza = " << circ() << endl;
  33.     return s.str();
  34. }
  35.  
  36. class Cilindro{
  37. private:
  38.     Cerchio c;
  39.     float y;
  40.  
  41. public:
  42.     Cilindro(float _x = 1, float _y = 1) : c(_x), y(_y) {}
  43.     float volume(){ return c.area() * y; }
  44.     float superficie(){ return 2*c.area() + c.circ()*y; }
  45.     string toString();
  46. };
  47.  
  48. string Cilindro::toString(){
  49.     stringstream s;
  50.     s << "Il cilindro ha raggio = " << c.getRaggio() << ", altezza = " << y
  51.       << ", area = " << superficie() << ", volume = " << volume() << endl;
  52.     return s.str();
  53. }
  54.  
  55. int main()
  56. {
  57.     Cerchio c1(10);
  58.     Cilindro c2(10, 15);
  59.     cout << c1.toString() << endl << c2.toString() << endl;
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement