Advertisement
metalni

OOP Labs 6 Kvadrat i Pravoagolnik

Jun 2nd, 2020
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class Kvadrat {
  7.     protected:
  8.         double a;
  9.     public:
  10.         Kvadrat();
  11.         Kvadrat(const double length);
  12.         Kvadrat(const Kvadrat &orig);
  13.         const double perimetar();
  14.         const double plostina();
  15.         void pecati();
  16. };
  17. Kvadrat::Kvadrat(){
  18.     this->a = 0;
  19. }
  20. Kvadrat::Kvadrat(const double length){
  21.     this->a = length;
  22. }
  23. Kvadrat::Kvadrat(const Kvadrat &orig){
  24.     this->a = orig.a;
  25. }
  26. const double Kvadrat::perimetar(){
  27.     return 4.0 * this->a;
  28. }
  29. const double Kvadrat::plostina(){
  30.     return this->a * this->a;
  31. }
  32. void Kvadrat::pecati(){
  33.     cout << "Kvadrat so dolzina a=" << this->a <<" ima plostina P=" << this->plostina() << " i perimetar L=" << this->perimetar() << endl;
  34. }
  35.  
  36. //pravoagolnik
  37. class Pravoagolnik : public Kvadrat {
  38.     private:
  39.         double x;
  40.         double y;
  41.     public:
  42.         Pravoagolnik();
  43.         Pravoagolnik (const Kvadrat &k, double x, double y) : Kvadrat(k) {
  44.             this->x = x;
  45.             this->y = y;
  46.         }
  47.         Pravoagolnik(const Pravoagolnik &orig);
  48.         const double perimetar();
  49.         const double plostina();
  50.         void pecati();
  51. };
  52.  
  53. Pravoagolnik::Pravoagolnik(){
  54.     this-> x = 0;
  55.     this-> y = 0;
  56. }
  57. Pravoagolnik::Pravoagolnik(const Pravoagolnik &orig){
  58.     this->x = orig.x;
  59.     this->y = orig.y;
  60. }
  61. const double Pravoagolnik::perimetar(){
  62.     return (2.0 * (this->a + this->x)) + (2.0 * (this->a + this->y));
  63. }
  64. const double Pravoagolnik::plostina(){
  65.     return (this->a + this->x) * (this->a + this->y);
  66. }
  67. void Pravoagolnik::pecati(){
  68.     if(this->x == this->y){
  69.         this->a += this->x;
  70.         Kvadrat::pecati();
  71.         return;
  72.     }
  73.     cout << "Pravoagolnik so strani: " << this->a + this->x << " i " << this->a + this->y << " ima plostina P=" << this->plostina() << " i perimetar L=" << this->perimetar() << endl;
  74. }
  75.  
  76.  
  77.  
  78. //main
  79. int main() {
  80.     int n;
  81.     double a,x,y;
  82.     Kvadrat * kvadrati;
  83.     Pravoagolnik * pravoagolnici;
  84.    
  85.     cin>>n;
  86.    
  87.     kvadrati = new Kvadrat [n];
  88.     pravoagolnici = new Pravoagolnik [n];
  89.    
  90.     for (int i=0;i<n;i++){
  91.         cin>>a;
  92.        
  93.         kvadrati[i] = Kvadrat(a);
  94.     }
  95.    
  96.     for (int i=0;i<n;i++){
  97.         cin>>x>>y;
  98.        
  99.         pravoagolnici[i]=Pravoagolnik(kvadrati[i],x,y);
  100.     }
  101.    
  102.     int testCase;
  103.     cin>>testCase;
  104.    
  105.     if (testCase==1){
  106.         cout<<"===Testiranje na klasata Kvadrat==="<<endl;
  107.         for (int i=0;i<n;i++)
  108.             kvadrati[i].pecati();
  109.     }
  110.     else {
  111.         cout<<"===Testiranje na klasata Pravoagolnik==="<<endl;
  112.         for (int i=0;i<n;i++)
  113.             pravoagolnici[i].pecati();
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement