Facebook
From MIkMIk, 5 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 186
  1. ??????????????????????????????????NAGLOWEK
  2. // stdafx.h: dołącz plik do standardowych systemowych plików dołączanych,
  3. // lub specyficzne dla projektu pliki dołączane, które są często wykorzystywane, ale
  4. // są rzadko zmieniane
  5. //
  6.  
  7. #pragma once
  8.  
  9. #include "targetver.h"
  10.  
  11. #include <stdio.h>
  12. #include <tchar.h>
  13.  
  14.  
  15. class Figure
  16. {
  17. public:
  18.         virtual double area() = 0;
  19. };
  20.  
  21. class Circle : public Figure
  22. {
  23. public:
  24.         double r;
  25.         double area();
  26.         Circle();
  27. };
  28.  
  29. class Rectangle : public Figure
  30. {
  31. public:
  32.         double a, b;
  33.         double area();
  34.         Rectangle();
  35. };
  36.  
  37. class Square : public Figure
  38. {
  39. public:
  40.         double a;
  41.         double area();
  42.         Square();
  43. };
  44. // TODO: W tym miejscu odwołaj się do dodatkowych nagłówków wymaganych przez program
  45.  
  46.  
  47. ////////////////////////////////////////////////////////////////////CPPP
  48.  
  49. // SEM3LAB5_fig.cpp : Defines the entry point for the console application.
  50. //
  51.  
  52. #include "stdafx.h"
  53. #include <iostream>
  54.  
  55. using namespace std;
  56.  
  57. int main()
  58. {
  59.         int size;
  60.         cout << "podaj ilosc figur" << endl;
  61.         cin >> size;
  62.         Figure ** wsk_fig;
  63.         wsk_fig = new Figure*[size];
  64.         int licznik = 0;
  65.         char key;
  66.         while (licznik < size)
  67.         {
  68.                 cout << "podaj jaka chcesz dodac figure c - kolo, r - prostokat, s - kwadrat " << endl;
  69.                 cin >> key;
  70.                 switch (key)
  71.                 {
  72.                 case 'c': wsk_fig[licznik] = new Circle; break;
  73.                 case 's': wsk_fig[licznik] = new Square; break;
  74.                 case 'r': wsk_fig[licznik] = new Rectangle; break;
  75.                 default: cout << "zla literka :(" << endl;
  76.                         break;
  77.                 }
  78.                 licznik++;
  79.         }
  80.         double suma_pol = 0;
  81.         for (int i = 0; i < size; i++)
  82.         {
  83.                 suma_pol += wsk_fig[i]->area();
  84.         }
  85.         cout << "suma pol = " << suma_pol << endl;
  86.  
  87.         system("pause");
  88.  
  89.         for (int i = 0; i < size; i++)
  90.         {
  91.                 delete wsk_fig[i];
  92.         }
  93.  
  94.         delete wsk_fig;
  95.     return 0;
  96. }
  97.  
  98. double Circle :: area()
  99. {
  100.         return r * r*3.1415;
  101. }
  102.  
  103. double Rectangle :: area()
  104. {
  105.         return a * b;
  106. }
  107.  
  108. double Square :: area()
  109. {
  110.         return a * a;
  111. }
  112.  
  113. Square :: Square()
  114. {
  115.         cout << "podaj dlugosc boku" << endl;
  116.         cin >> a;
  117. }
  118.  
  119. Rectangle::Rectangle()
  120. {
  121.         cout << "podaj dlugosc bokow" << endl;
  122.         cin >> a;
  123.         cin >> b;
  124. }
  125.  
  126. Circle::Circle()
  127. {
  128.         cout << "podaj promien" << endl;
  129.         cin >> r;
  130. }