Simple2012

Untitled

Oct 20th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include "circle.h"
  2. #include "shape.h"
  3. #include "cylinder.h"
  4. #include "parallelpiped.h"
  5. #include "rectangle.h"
  6. #include "roundedRectangle.h"
  7. #include <vector>
  8. #include <iostream>
  9.  
  10. void GetData(std::vector<shape *> v, size_t size){
  11.     double Area, TotalArea = 0;
  12.     for(int i = 0; i < size ; i++)
  13.     {
  14.         if ( v[i] == 0 )
  15.             continue;
  16.         Area = (v[i])->GetArea();
  17.         TotalArea += Area;
  18.         std::cout << "Colour: " << (v[i])->GetColour() << std::endl;
  19.         std::cout << "Area: " << Area << std::endl;
  20.     }
  21.     std::cout << "Total area: " << TotalArea << std::endl;
  22. }
  23. void InitiateValues(std::vector<shape *> v){
  24.     for (int i = 0; i < v.size(); i++){
  25.         if (v[i] == circle)
  26.             (v[i])->SetRadius(13);
  27.         else if (v[i] == cylinder){
  28.             (v[i])->SetRadius(13);
  29.             (v[i])->SetHeight(7);
  30.         }
  31.         else if(v[i] == rectangle){
  32.             (v[i])->SetWidth(20);
  33.             (v[i])->SetHeight(18);
  34.         }
  35.         else if(v[i] == parallelpiped){
  36.             (v[i])->SetWidth(20);
  37.             (v[i])->SetHeight(18);
  38.             (v[i])->SetLength(12);
  39.         }
  40.         else if(v[i] == roundedRectangle){
  41.             (v[i])->SetWidth(20);
  42.             (v[i])->SetHeight(18);
  43.             (v[i])->SetRadius(4);
  44.         }
  45.  
  46.     }
  47. }
  48.  
  49. /*  polymorphic area calculation of the total area of all shapes in the array,
  50.     as well as displaying information about their colours.
  51.     Needless to say that the function GetData must not be changed in
  52.     case of adding some new shape to the array or removing an old one. */
  53.  
  54. int main(int argc, char const *argv[]){
  55.     std::vector<shape *> derivedClassHolder;
  56.     derivedClassHolder.push_back(new circle);
  57.     derivedClassHolder.push_back(new cylinder);
  58.     derivedClassHolder.push_back(new parallelpiped);
  59.     derivedClassHolder.push_back(new rectangle);
  60.     derivedClassHolder.push_back(new roundedRectangle);
  61.  
  62.     InitiateValues(derivedClassHolder);
  63.  
  64.     GetData(derivedClassHolder, derivedClassHolder.size());
  65.  
  66.     for (int i = 0; i < derivedClassHolder.size(); i++)
  67.     {
  68.         delete derivedClassHolder[i];
  69.     }
  70.  
  71.     /*for(int i = 0; i < (int)derivedClassHolder.size() ; i++)
  72.     {
  73.         ((circle*)derivedClassHolder[i])->SetRadius(14);
  74.     }*/
  75.     std::cin.get();
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment