Advertisement
FRiTZZY

TP_T13_Z1

Aug 2nd, 2015
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. /* TP_Tutorijal_13 Zadatak_1 */
  2. #include <iostream>
  3. #include <cmath>
  4. #include <string>
  5. #include <stdexcept>
  6.  
  7.  
  8. class Lik {
  9. protected:
  10.         std::string naziv;
  11. public:
  12.         Lik (std::string ime) : naziv (ime) {}
  13.         virtual ~Lik() {}
  14.         virtual double DajObim() const = 0;
  15.         virtual double DajPovrsinu() const = 0;
  16.         virtual void IspisiAtribute () const =0;
  17.  
  18. };
  19.  
  20. class Trougao : public Lik {
  21.         double a, b, c;
  22. public:
  23.         Trougao (std::string ime, double a, double b, double c) : Lik (ime), a (a), b (b), c (c) {
  24.                 if (a < 0 || b < 0 || c < 0 )
  25.                         throw std::domain_error ("Ilegalne duzine stranica");
  26.                 if (a + b <= c || a + c <= b || b + c <= a)
  27.                         throw std::domain_error ("Ilegalne duzine stranica");
  28.         }
  29.         double DajObim() const override { return a + b + c; }
  30.         double DajPovrsinu() const override {
  31.                 double s (DajObim() / 2);
  32.                 return std::sqrt (s * (s - a) * (s - b) * (s - c) );
  33.         }
  34.         void IspisiAtribute () const override {
  35.                 std::cout << naziv << std::endl;
  36.                 std::cout << "Stranice trougla su: " << a << ' ' << b << ' ' << c << std::endl;
  37.                 std::cout << "Obim kruga: " << DajObim() << std::endl;
  38.                 std::cout << "Povrsina kruga: " << DajPovrsinu() << std::endl;
  39.         }
  40. };
  41.  
  42. class Krug : public Lik {
  43.         double x, y, r;
  44. public:
  45.         Krug (std::string ime, double x, double y, double r) : Lik (ime), x (x), y (y), r (r) {
  46.                 if (r < 0)
  47.                         throw std::domain_error ("Ilegalan poliprecnik!");
  48.         }
  49.         double DajObim() const override { return 2 * r * std::acos (-1.); }
  50.         double DajPovrsinu() const override { return r * r * std::acos (-1.); }
  51.         void IspisiAtribute () const override {
  52.                 std::cout << naziv << std::endl;
  53.                 std::cout << "Radijus kruga: " <<  r  << "\nCentar kruga: " << x << ' ' << y << std::endl;
  54.                 std::cout << "Obim kruga: " << DajObim() << std::endl;
  55.                 std::cout << "Povrsina kruga: " << DajPovrsinu() << std::endl;
  56.         }
  57. };
  58.  
  59. class Pravougaonik : public Lik {
  60.         double a, b;
  61. public:
  62.         double DajObim() const override { return (a+b)*2; }
  63.         double DajPovrsinu() const override { return a*b; }
  64.         void IspisiAtribute () const override {
  65.                 std::cout << naziv << std::endl;
  66.                 std::cout << "Stranice pravougaonika: " <<  a << ' ' << b << std::endl;
  67.                 std::cout << "Obim pravougaonika: " << DajObim() << std::endl;
  68.                 std::cout << "Povrsina pravougaonika: " << DajPovrsinu() << std::endl;
  69.         }
  70. };
  71.  
  72. int main()
  73. {
  74.  
  75.         return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement