I've created a queue (thanks to vector library) of object pointors.
std::vector<Polygon*>queue;
My class Polygon is the mother class of all the others, basicaly the aim of it is to draw a shape. So the polygon is drawing a polygon, the rectangle a rectangle... with both * and # chosen at random. It actually perfectly works.
So I wanted to create several of these drawings and put them in a queue and when the program ends display all the shapes (using a FIFO). At first it seems to works, but diging a bit I realized that the drawing where different.
For example the rectangle will still be a rectangle, with the same dimension, but the patern of * and # will be different. So it means that the figure has not been saved, but its parameters have been. And the queue is rebuilding those.
What I would like to do is to save the same shape, what did I do wrong ?
/*
* File: main.cpp
* Author: vfortineau
*
* Created on may 12nd 2015, 10:25
*/
#include <cstdlib>
#include <iostream>
#include <vector>
#include "Polygon.h"
#include "Rectangle.h"
#include "Square.h"
#include "IsoscelesTriangle.h"
#include "ReversedTriangle.h"
#include "Diamond.h"
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
int choice;
int _height;
int _width;
int _posi;
int i=0;
Polygon* polymorph;
std::vector<Polygon*>queue;
while (1)
{
cout << "\n\tPlease type one of the following command :\n" << endl;
cout << "\t********************************************\n"<<\
"\t* 1 - Upright isosceles triangle *\n" << \
"\t* 2 - Inverted isosceles triangle *\n" << \
"\t* 3 - Rectangle *\n" << \
"\t* 4 - Square *\n"<< \
"\t* 5 - Diamond *\n"<< \
"\t* 6 - Polygone *\n" << \
"\t* 0 - Exit Program *\n" << \
"\t********************************************" << endl;
cin >> choice;
switch(choice)
{
case 1:
cout << "Choose the size of the height (please type an integer) : \n";
cin >> _height;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new IsoscelesTriangle(_height, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 2:
cout << "Choose the size of the height (please type an integer) : \n";
cin >> _height;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new ReversedTriangle(_height, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 3:
cout << "Choose the size of the height (please type an integer) : \n";
cin >> _height;
cout << "Choose the size of the width (please type an integer) : \n";
cin >> _width;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new Rectangle(_height, _width, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 4:
cout << "Choose the size of the sides (please type an integer) : \n";
cin >> _height;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new Square(_height, _width, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 5:
cout << "Choose the size of the vertical diagonal (please type an integer) : \n";
cin >> _height;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new Diamond(_height, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 6:
cout << "Choose the size of the height (please type an integer) : \n";
cin >> _height;
cout << "Choose the size of the width (please type an integer) : \n";
cin >> _width;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new Polygon(_height, _width, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 0:
while(not queue.empty())
{
i++;
cout << endl << "\t Figure " << i << endl;
cout << "\t---------\n\n";
queue[0]->drawShape();
queue.erase(queue.begin());
}
return 0;
}
}
return 0;
}
I'm joining as an example a version of a drawShape() function but I don't think the problem is coming from here.
void IsoscelesTriangle::drawShape()
{
srand(time(NULL));
int random=0;
for(int i=0; i<height; i++)
{
position();
for(int j=0; j<height-(i+1); j++)
{
std::cout << " ";
}
for (int k=0; k<(2*i+1); k++)
{
random=rand()%2;
if (random==0) std::cout << "* ";
else std::cout << "# ";
}
std::cout << std::endl;
}
}
height is a protected member of Polygon, and position a public function of Polygon.
Aucun commentaire:
Enregistrer un commentaire