I thought that was not good encapsulation. I was just trying to put whatever is concern with circle in the Circle class. Now you are asking me to
make CreateCircle global right ?
Right! Your way is a bad way. Encapsulation does not mean put everything
to do with a circle in the Circle class. It means making Circle a logical
entity with a clean reusable interface.
A Circle is a concept, it has radius, a center point etc. Asking the user
how big a circle radius is has nothing to do with the concept of a circle,
and should not go in the circle class.
Think about reusability. Suppose you wanted to reuse your Circle class in
a different program, a program that did some calculation with circles but
didn't ask the user about how big the radius was. What would the point of
you CreateCricle method be then? Every circle has a radius, so radius is a
good thing to put in a Circle class. Not every program need to ask the
user how big a circle is, so asking that is not a good thing to put in a Circle
class.
john
Yes Well here it is John,
I should of inplemented Display outside the class, but I will work on that
later:
The trianlge part is out for now. it does not work ..
BUT WOuld any one know how to modify my valadation function ( at the end
of the code) so that the program don't exit , but just return to the menu.
I tried a call to the menu , but it wouls keep finishing to ask the
questions from the function it came from.. So I had to put exit, but , that
is not good. ..
#include <iostream>
#include<conio. h>
#include<cmath>
#include <algorithm>
using namespace std;
const double PI = 3.14159;
void Validate(float &);
int Validate_positi ve(float &);
class Point {
int x,y,c;
public:
Point(int x1, int y1, int c1) {x = x1; y = y1; c = c1;}
Point() { x = y = c = 0;}
void get(int &u, int &v, int &w) {
u = x; v = y;w = c; }
};
class Shape {
Point p1;
public:
Shape() { }
Shape(Point p) : p1(p)
{ }
void get(int &a, int &b, int &c) {
p1.get(a,b,c); }
};
class Circle: public Shape
{
public:
Circle(){};
Circle(Point p, float r) : Shape(p) {radius = r;SetArea(); }
void SetArea() {area= PI * radius * radius; }
float GetArea(){ return area; }
void display() {
int x,y,c;
get(x,y,c);
cout <<"This circle has center : ("<<x<<","<<y<< ","<<c<<") and radius :"<<
radius<<" and area:" <<area<<endl; }
private:
float radius;
float area;
};
class Cylinder: public Shape {
public:
Cylinder(){};
Cylinder(Point p, float r, float l) : Shape(p) {radius = r; height = l;
SetArea(); }
void SetArea() {area= PI * radius * radius * height; }
float GetArea(){ return area; }
void display() {
int x,y,c;
get(x,y,c);
cout <<"This cylinder has center : ("<<x<<","<<y<< ","<<c<<") and radius :"<<
radius <<endl;
cout<< " and height:" << height <<" and volume: " <<area <<endl; }
private:
float height;
float radius;
float area;
};
/*
class Triangle: public Shape {
public:
Triangle(){};
Triangle(Point p, Point p1, Point p2,) : Shape(p){}
/*float b,h;
void SetArea() {
int x,y,c;
get(x,y,c);
b = sqrt( (pow(p1.x - p.x,2)) + (pow(p1.y -p.y,2)) );
h = sqrt( ( pow(p1.x - p2.x, 2) + pow(p1.y - p2.y ,2) ) - (
ow(b,2) ) );
area = (b * h) /2 ; }
float GetArea(){ return 333;} //area; }
void display() {
int x,y,c;
get(x,y,c);
cout <<"This triangle has points : ("<<p.x<<","<<p .y<<","<<p.c<<" ) and
"<<endl;
cout <<"This triangle has points : ("<<p1.x<<","<< p1.y<<","<<p1.c <<") and
"<<endl;
cout <<"This triangle has points : ("<<p2.x<<","<< p2.y<<","<<p2.c <<") and
area :"<< area <<endl; }
private:
float area;
Point p;
Point p1;
Point p2;
};
*/
void CreateCircle(Ci rcle& oneCircle) {
float x,y,c, z;
cout<<" Enter the Center Point of the circle, X-Axis: " ;
cin>> x ;
Validate(x);
Validate_positi ve(x);
cout<<endl;
cout<<" Enter the Center Point of the circle, Y-Axis: " ;
cin>> y ;
Validate(y);
Validate_positi ve(y);
cout<<" Enter the Color of that point: " ;
cin>> c ;
Validate(c);
Validate_positi ve(c);
cout<<" Enter the radius of the circle: " ;
cin>> z;
Validate(z);
Validate_positi ve(z);
Point center(x,y,c);
oneCircle = Circle(center, z);
}
void DisplayCircle(C ircle *myCircles, int *countCircles)
{
for (int i = 0; i < *countCircles; i++)
{cout<<" Here is circle # " << i+1 <<endl;
myCircles[i].display();
}
}
/*
void CreateTriangle( Triangle& oneTriangle) {
float x1,y1,c1,x2,y2, c2,x3,y3,c3;
//Point 1
cout<<" Enter the Lower Left point, X-Axis: " ;
cin>> x1 ;
cout<<endl;
cout<<" Enter the Lower Left point, Y-Axis: " ;
cin>> y1 ;
cout<<endl;
cout<<" Enter the Color of that point: " ;
cin>> c1 ;
cout<<endl;
Point p1((x1,y1,c1));
//Point 2
cout<<" Enter the Lower Right point, X-Axis: " ;
cin>> x2 ;
cout<<endl;
cout<<" Enter the Lower Right point, Y-Axis: " ;
cin>> y2 ;
cout<<endl;
cout<<" Enter the Color of that point: " ;
cin>> c2 ;
cout<<endl;
Point p2((x2,y2,c2));
//Point 3
cout<<" Enter the Upper point, X-Axis: " ;
cin>> x3 ;
cout<<endl;
cout<<" Enter the Upper Left point, Y-Axis: " ;
cin>> y3 ;
cout<<endl;
cout<<" Enter the Color of that point: " ;
cin>> c3 ;
cout<<endl;
Point p3((x3,y3,c3));
// t[*count_triangle].SetArea(tri);
//Point center(x,y,c);
//oneCircle = Circle(center, z);
//Point points(x,y,c);
oneTriangle = Triangle(p1,p2, p3);
}
void DisplayTriangle (Triangle *myTriangles, int *countTriangles )
{
for (int i = 0; i < *countTriangles ; i++)
{cout<<" Here is triangle # " << i+1 <<endl;
myTriangles[i].display();
}
}
*/
void CreateCylinder( Cylinder& oneCylinder) {
float x,y,c, z,l;
cout<<" Enter the Center Point of the cylinder, X-Axis: " ;
cin>> x ;
cout<<endl;
cout<<" Enter the Center Point of the cylinder, Y-Axis: " ;
cin>> y ;
cout<<" Enter the Color of that point: " ;
cin>> c ;
cout<<" Enter the radius of the cylinder: " ;
cin>> z;
cout<<" Enter the lenght of the cylinder: " ;
cin>> l;
Point center(x,y,c);
oneCylinder = Cylinder(center , z,l);
}
void DisplayCylinder (Cylinder *myCylinders, int *countCylinders )
{
for (int i = 0; i < *countCylinders ; i++)
{cout<<" Here is cylinder # " << i+1 <<endl;
myCylinders[i].display();
}
}
void menu();
void CreateCircle(Ci rcle& oneCircle);
void DisplayCircle(C ircle , int );
//void CreateTriangle( Triangle& oneTriangle);
//void DisplayTriangle (Triangle , int );
void CreateCylinder( Cylinder& oneCylinder);
void DisplayCylinder (Cylinder *myCylinders, int *countCylinders );
int main()
{
menu();
getch();
return 0;
}
void menu()
{ char choice;
Circle myCircles [10];
int countCircles = 0;
// Triangle myTriangles [10];
// int countTriangles = 0;
Cylinder myCylinders [10];
int countCylinders = 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 >> choice;
switch (choice)
{
case '1':
CreateCircle(my Circles[countCircles]);
countCircles++;
break;
case '2':
// CreateTriangle( myTriangles[countTriangles]);
// countTriangles+ +;
break;
case '3':
CreateCylinder( myCylinders[countCylinders]);
countCylinders+ +;
break;
case '4':
DisplayCircle(& myCircles[0], &countCircle s);
break;
case '5':
// DisplayTriangle (&myTriangles[0], &countTriangles );
break;
case '6':
DisplayCylinder (&myCylinders[0], &countCylinders );
break;
case '7': cout<<"Thank you for having used this system, Bye Bye!!!" ;
break;
default: cout<<"Error: Invalid option, Please try again" <<endl;
}
}while ( (choice != '7') ) ;
}
template<class T>
bool isbetween(T c, T l, T u)
{
if(l > u) swap(l, u);
return c >= l && c <= u;
}
void Validate(float &c) //Validate to see if it is a letter
{
if (isbetween<char >(c, 64, 122))
{
cout << "You have type an integer, Please try again with a positive
number! " ;
cout << "Do NOT type a letter " ;
exit(1);
}
}
int Validate_positi ve(float &c)
{
if (c < 0)
{ cout<< "You have type a negative integer, Please try again with a
positive number! " <<endl;
exit(1); } //exits the program and starts over with a positive number
return 0;
}