Maybe the way to go for your program (I'm thinking of the longer array
version you have) is to throw away the Shape class and have one ShapeHolder
object which holds the arrays
class ShapeHolder
{
public:
CreateShape();
private:
Circle draw1[10];
Cylinder draw2[10];
Triangle draw3[10];
int count_circle;
int count_cylinder;
int count_triangle;
};
void menu()
{
ShapeHolder holder; // the one and only shape holder object
...
}
Just a thought, you don't have to go that way. But the main point is that
you cannot put those arrays inside the CreateShape method, because then they
cannot exist outside of it.
I did what you said, I kept everything in one area, which is actually easier
to understand and put everything in one menu.
I have one more problem and hopefully the last one.
I created the array of everything I want. I initialized the constructor. BUt
I cannot seem to bind those two together . This is my big problem now.
Class Point initialize the center point .
alors the getvolume and getarea does not work, but I should be able to fix
that
#include <iostream>
#include <conio.h>
#include <cmath>
using namespace std;
const double PI = 3.14159;
class Point { // initialize the Center point only
int x,y, color;
public:
Point(int a=0, int b=0, int c=0){
x=a; y=b; color=c;
}
};
class Shape //base class for any shape
{
public:
Shape(Point p)
1(p) {}
Shape ()
{
_centerpoint = 0;
_radius = 0;
}
Shape (int c, int r)
{
_centerpoint = c;
_radius = r;
}
~Shape(){}
int GetCenterPoint()
{
return _centerpoint;
}
void SetCenterPoint(int centerpoint)
{
_centerpoint = centerpoint;
}
int GetRadius()
{
return _radius;
}
void SetRadius(int Radius)
{
_radius = Radius;
}
protected:
Point p1;
int _centerpoint, _radius;
};
class Circle: public Shape
{
public:
Circle(){};
Circle(Point p, float r=0) : Shape(p) {radius = r;}
void CreateCircle(Shape *a, int * count, Circle *v);
void DisplayCircle(Shape *a, int * count, Circle *v);
void SetArea( float radius){
area = PI * pow(radius,2); }
float GetArea(){ return area; }
private:
float area;
float radius;
};
class Triangle: public Shape
{
public:
Triangle(){};
Triangle(Point p) : Shape(p) {}
void CreateTriangle(Shape *a, int * count, Triangle *v);
void DisplayTriangle(Shape *a, int * count, Triangle *v);
void SetArea( float radius){
area = PI * pow(radius,2); }
float GetArea(){ return area; }
private:
float area;
float radius;
};
class Cylinder: public Shape
{
public:
Cylinder(){};
Cylinder(Point p, float r=0, float h=0) : Shape(p) {radius = r, height =
h;}
void CreateCylinder(Shape *a, int * count, Cylinder *v);
void DisplayCylinder(Shape *a, int * count, Cylinder *v);
void SetVolume( float radius){
volume = height * PI * pow(radius,2); }
float GetVolume(){ return volume; }
private:
float volume;
float height;
float radius;
};
void menu()
{ char abc;
Shape circle[10];
Shape triangle[10];
Shape cylinder[10];
Circle area[10];
Triangle area_1[10];
Cylinder volume1[10];
Circle cir;
Triangle tri;
Cylinder cyl;
int count_circle =0;
int count_triangle =0;
int count_cylinder =0;
do {
cout << endl << endl;
cout << " Shape Management System "<<endl;
cout << "============================================== "<<endl;
cout << " 1: Create a Circle "<<endl;
cout << " 2: Create a Triangle "<<endl;
cout << " 3: Create a Cylinder "<<endl;
cout << " 4: Display all Circles "<<endl;
cout << " 5: Display all Triangles "<<endl;
cout << " 6: Display all Cylinders "<<endl;
cout << " 7: Quit "<<endl;
cout << "============================================== "<<endl;
cout << " Your choice please: ";
cin >> abc;
switch (abc)
{
case '1':
cir.CreateCircle( &circle[0], &count_circle, &area[0]);
break;
case '2':
tri.CreateTriangle( &triangle[0], &count_triangle, &area_1[0]);
break;
case '3':
cyl.CreateCylinder( &cylinder[0],&count_cylinder, &volume1[0]);
break;
case '4':
cir.DisplayCircle( &circle[0],&count_circle, &area[0]);
break;
case '5':
tri.DisplayTriangle( &triangle[0],&count_triangle, &area_1[0]);
break;
case '6':
cyl.DisplayCylinder( &cylinder[0],&count_cylinder, &volume1[0]);
break;
case '7': cout<<"Thank you for having used this system, Bye Bye!!!";
break;
default: cout<<"Error: Invalid option, Please try again" ;
}
}while ( (abc != '7') ) ;
}
int main()
{
menu();
getch();
return 0;
}
void Circle::CreateCircle( Shape* s, int * count, Circle* volume)
{
int value;
// float p1, p2;
// char color;
cout<<" Enter the Center Point of the circle: " <<endl;
cin>> value;
s[*count].SetCenterPoint(value);
// p1 = s[*count].SetCenterPoint(value);
// cout<<" Enter the color for this point: " <<endl;
// cin>> value;
// s[*count].SetColor(color);
cout<<" Enter the radius of the circle: " <<endl;
cin>> value;
s[*count].SetRadius(value);
// p2 = s[*count].SetRadius(value);
// cout<<" Enter the color for this point: " <<endl;
// cin>> value;
// s[*count].SetColor(color);
//Circle point1(p1, p2);
(*count) ++;
}
void Triangle::CreateTriangle(Shape *s, int *count, Triangle* volume)
{
int value;
cout<<" Enter the CenterPoint: " <<endl;
cin>> value;
s[*count].SetCenterPoint(value);
cout<<" Enter Radius: " <<endl;
cin>> value;
s[*count].SetRadius(value);
cout<<" Enter seconds: " <<endl;
cin>> value;
volume[*count].SetArea(value);
(*count) ++;
}
void Cylinder::CreateCylinder(Shape *s, int *count, Cylinder* volume)
{
int value;
cout<<" Enter the CenterPoint: " <<endl;
cin>> value;
s[*count].SetCenterPoint(value);
cout<<" Enter Radius: " <<endl;
cin>> value;
s[*count].SetRadius(value);
cout<<" Enter seconds: " <<endl;
cin>> value;
volume[*count].SetVolume(value);
(*count) ++;
}
void Circle:
isplayCircle(Shape *s, int * count, Circle* area)
{
if ( (*count) <= 0)
{ cout<< "There are no Circles to display! Please choose another option "
<<endl; }
else
{
for (int i=0; i< (*count); i++)
{
cout<<" Here is the area for circle #" << i+1 <<endl;
cout<<"CenterPoint: " <<s
.GetCenterPoint() <<endl <<"Radius: " <<
s.GetRadius() <<endl <<"Area: " << area.GetArea() <<endl;
cout <<endl;
}
}
}
void Triangle:isplayTriangle(Shape *s, int * count, Triangle* area)
{
if ( (*count) <= 0)
{ cout<< "There are no Triangles to display! Please choose another option
" <<endl; }
else
{
for (int i=0; i< (*count); i++)
{
cout<<" Here is the area for triangle #" << i+1 <<endl;
cout<<"CenterPoint: " <<s.GetCenterPoint() <<endl <<"Radius: " <<
s.GetRadius() <<endl <<"Area: " << area.GetArea() <<endl;
cout <<endl;
}
}
}
void Cylinder:isplayCylinder(Shape *s, int * count, Cylinder* volume)
{
if ( (*count) <= 0)
{ cout<< "There are no Cylinders to display! Please choose another option
" <<endl; }
else
{
for (int i=0; i< (*count); i++)
{
cout<<" Here is the area for cylinder #" << i+1 <<endl;
cout<<"CenterPoint: " <<s.GetCenterPoint() <<endl <<"Radius: " <<
s.GetRadius() <<endl <<"Volume: " << volume.GetVolume() <<endl;
cout <<endl;
}
}
}