Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "circle.h"
- #include "shape.h"
- #include "cylinder.h"
- #include "parallelpiped.h"
- #include "rectangle.h"
- #include "roundedRectangle.h"
- #include <vector>
- #include <iostream>
- void GetData(std::vector<shape *> v, size_t size){
- if (size != shape::Count)
- return;
- double Area, TotalArea = 0;
- for(size_t i = 0; i < size ; i++)
- {
- if ( v[i] == 0 )
- continue;
- Area = (v[i])->GetArea();
- TotalArea += Area;
- std::cout << "Colour: " << (v[i])->GetColour() << std::endl;
- std::cout << "Area: " << Area << std::endl;
- }
- std::cout << "Total area: " << TotalArea << std::endl;
- }
- void InitiateValues(std::vector<shape *> v){
- for (size_t i = 0; i < v.size(); i++){
- if (v[0])
- (((circle*)v[0]))->SetRadius(13);
- else if (v[1]){
- (((cylinder*)v[1]))->SetRadius(13);
- (((cylinder*)v[1]))->SetHeight(7);
- }
- else if(v[2]){
- (((parallelpiped*)v[2]))->SetWidth(20);
- (((parallelpiped*)v[2]))->SetHeight(18);
- }
- else if(v[3]){
- (((parallelpiped*)v[3]))->SetWidth(20);
- (((parallelpiped*)v[3]))->SetHeight(18);
- (((parallelpiped*)v[3]))->SetLength(12);
- }
- else if(v[4]){
- (((roundedRectangle*)v[4]))->SetWidth(20);
- (((roundedRectangle*)v[4]))->SetHeight(18);
- (((roundedRectangle*)v[4]))->SetRadius(4);
- }
- }
- }
- /* polymorphic area calculation of the total area of all shapes in the array,
- as well as displaying information about their colours.
- Needless to say that the function GetData must not be changed in
- case of adding some new shape to the array or removing an old one. */
- int main(int argc, char const *argv[]){
- std::vector<shape *> derivedClassHolder;
- derivedClassHolder.push_back(new circle);
- derivedClassHolder.push_back(new cylinder);
- derivedClassHolder.push_back(new parallelpiped);
- derivedClassHolder.push_back(new rectangle);
- derivedClassHolder.push_back(new roundedRectangle);
- // Give width, height etc.. some values
- InitiateValues(derivedClassHolder);
- GetData(derivedClassHolder, derivedClassHolder.size());
- for (size_t i = 0; i < derivedClassHolder.size(); i++)
- {
- delete derivedClassHolder[i];
- }
- /*for(int i = 0; i < (int)derivedClassHolder.size() ; i++)
- {
- ((circle*)derivedClassHolder[i])->SetRadius(14);
- }*/
- std::cin.get();
- }
Advertisement
Add Comment
Please, Sign In to add comment