Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* TP_Tutorijal_13 Zadatak_1 */
- #include <iostream>
- #include <cmath>
- #include <string>
- #include <stdexcept>
- class Lik {
- protected:
- std::string naziv;
- public:
- Lik (std::string ime) : naziv (ime) {}
- virtual ~Lik() {}
- virtual double DajObim() const = 0;
- virtual double DajPovrsinu() const = 0;
- virtual void IspisiAtribute () const =0;
- };
- class Trougao : public Lik {
- double a, b, c;
- public:
- Trougao (std::string ime, double a, double b, double c) : Lik (ime), a (a), b (b), c (c) {
- if (a < 0 || b < 0 || c < 0 )
- throw std::domain_error ("Ilegalne duzine stranica");
- if (a + b <= c || a + c <= b || b + c <= a)
- throw std::domain_error ("Ilegalne duzine stranica");
- }
- double DajObim() const override { return a + b + c; }
- double DajPovrsinu() const override {
- double s (DajObim() / 2);
- return std::sqrt (s * (s - a) * (s - b) * (s - c) );
- }
- void IspisiAtribute () const override {
- std::cout << naziv << std::endl;
- std::cout << "Stranice trougla su: " << a << ' ' << b << ' ' << c << std::endl;
- std::cout << "Obim kruga: " << DajObim() << std::endl;
- std::cout << "Povrsina kruga: " << DajPovrsinu() << std::endl;
- }
- };
- class Krug : public Lik {
- double x, y, r;
- public:
- Krug (std::string ime, double x, double y, double r) : Lik (ime), x (x), y (y), r (r) {
- if (r < 0)
- throw std::domain_error ("Ilegalan poliprecnik!");
- }
- double DajObim() const override { return 2 * r * std::acos (-1.); }
- double DajPovrsinu() const override { return r * r * std::acos (-1.); }
- void IspisiAtribute () const override {
- std::cout << naziv << std::endl;
- std::cout << "Radijus kruga: " << r << "\nCentar kruga: " << x << ' ' << y << std::endl;
- std::cout << "Obim kruga: " << DajObim() << std::endl;
- std::cout << "Povrsina kruga: " << DajPovrsinu() << std::endl;
- }
- };
- class Pravougaonik : public Lik {
- double a, b;
- public:
- double DajObim() const override { return (a+b)*2; }
- double DajPovrsinu() const override { return a*b; }
- void IspisiAtribute () const override {
- std::cout << naziv << std::endl;
- std::cout << "Stranice pravougaonika: " << a << ' ' << b << std::endl;
- std::cout << "Obim pravougaonika: " << DajObim() << std::endl;
- std::cout << "Povrsina pravougaonika: " << DajPovrsinu() << std::endl;
- }
- };
- int main()
- {
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement