okrąg template

nomen omen, kolejny prosty kod, dobry jako ćwiczenie
i mała uwaga. Tak, wiem że M_PI siedzi w bibliotece matematycznej i nie trzeba tego definiować, ale mój kompilator najwyraźniej ma w tej kwestii zupełnie inne zdanie ;)
klik:



kod jest mam nadzieję zrozumiały ;D
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <iostream>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #define M_PI 3.14159265358979323846
  6.  
  7. using namespace std;
  8.  
  9. template <class T>
  10. class okrag
  11. {
  12. private:
  13. T r;
  14. public:
  15. okrag();
  16. okrag(T);
  17. T pole(){return M_PI*r*r;}
  18. T obwod(){return 2*M_PI*r;}
  19. T set_promien(T);
  20. };
  21.  
  22.  
  23.  
  24. template <class T>
  25. okrag<T>::okrag()
  26. {
  27. r=1;
  28. return;
  29. }
  30.  
  31. template <class T>
  32. T okrag<T>::set_promien(T a)
  33. {
  34. r=a;
  35.  
  36. }
  37.  
  38.  
  39. int main()
  40. {
  41.  
  42. okrag<float> o;
  43.  
  44. o.set_promien (3);
  45.  
  46.  
  47. cout <<"\nobwod: " << o.obwod()<<endl;
  48. cout <<"pole: " << o.pole()<<endl;
  49.  
  50.  
  51. system("PAUSE");
  52. return 0;
  53.  
  54. }

Komentarze

Popularne posty z tego bloga

[c++] Lista wskaznikowa

[c++] słowniki - haszowanie - haszowanie otwarte

[ANSI C][LINUX] Wysyłanie i obsługa sygnałów