1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
class Shapes
{
protected:
string name;
string type;
string color;
Border outline;
public:
//constructors
Shapes()
{
name = "*";
type = "*";
color = "*";
}
Shapes(string n, string t, string c,string bC,int bW)
{
outline.setBorder(bC,bW);
setName(n);
setType(t);
setColor(c);
}
//setters
void setShape(string n, string t, string c,string bC,int bW)
{
setName(n);
setType(t);
setColor(c);
outline.setBorder(bC,bW);
}
void setName(string n)
{
name = n;
}
void setType(string t)
{
type = t;
}
void setColor(string c)
{
color = c;
}
//getters
string getName()
{
return name;
}
string getType()
{
return type;
}
string getColor()
{
return color;
}
virtual string toString()
{
string s;
ostringstream oss;
oss <<"Name :"<<getName()<<endl;
oss <<"Type :"<<getType()<<endl;
oss <<"Border Color :"<<outline.getBorderColor()<<endl;
oss <<"Border Width :"<<outline.getBorderWidth()<<endl;
oss <<"Shape Color :"<<getColor()<<endl;
s = oss.str();
return s;
}
};
class Rectangle : public Shapes
{
protected:
int length;
int width;
public:
//constructors
Rectangle()
{
length =0;
width =0;
}
Rectangle(string n, string t, string c,string bC,int bW,int l, int w)
:Shapes(n,t,c,bC,bW)
{
setLength(l);
setWidth(w);
}
//setters
void setRectangle(string n, string t, string c,string bC,int bW,int l, int w)
{
Shapes::setShape(n,t,c,bC,bW);
setLength(l);
setWidth(w);
}
void setLength(int l)
{
length = l;
}
void setWidth(int w)
{
width = w;
}
//getters
int getLength()
{
return length;
}
int getWidth()
{
return width;
}
//method to toString
string toString()
{
string s;
ostringstream oss;
oss <<"Rectangle";
oss <<"\tlength"<<length;
oss <<"\twidth"<<width;
s = oss.str();
return s;
}
};
class Canvas
{
private:
vector<Shapes> listOfshapes;
public:
//constructor
Canvas()
{
}
//method to add a shape
void addShape(Shapes shap)
{
listOfshapes.push_back(shap);
}
void display()
{
for(vector<Shapes>::iterator iter =listOfshapes.begin()
;iter != listOfshapes.end();iter++)
{
cout << iter->toString()<<endl;
}
}
};
/*class Canvas
{
private:
Shapes *s[100];
int count;
public:
Canvas()
{
count = 0;
}
void addShape(Shapes *f)
{
s[count] = f;
count+=1;
}
void display()
{
int j=0;
while(j<count)
{
cout << endl << s[j]->toString();
j+=1;
}
}
};*/
int main()
{
Rectangle r1("my Rectangle","teet","blue","freen",3,4,5);
Canvas c;
c.addShape(r1); //use this when canvas is made of vector
//c.addShape(&r1); //use this when canvas is made of array
c.display();
return 0;
}
|