473,775 Members | 2,206 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sharing information in a class

Dan
Hello,

I have trouble with class calling. I am calling getvolume() with succes in
the function CreateCircle but it do not want to call it in ShowCircle()
function. I am staying in the same class. I thought that was the point of
encapsulation. When the function ShowCircle() is called I get very large
number -1.07374e+008
can anyone help me ?
class Circle
{
public:

Circle (int radius){
itsRadius = 0; }
void CreateCircle();
void ShowCircle();

void SetVolume( float radius){
volume = PI * pow(radius,2); }
float GetVolume(){ return volume; }

private:
float itsRadius;
float volume;
};
void Circle::CreateC ircle() //Draw a circle
{ char color;
float itsRadius, volume;

cout<<"Enter the radius in cm: " ;
cin>> itsRadius;

SetVolume(itsRa dius);
cout<<" Volume: " << GetVolume() <<endl;
}
void Circle::ShowCir cle()
{ int i;
cout <<" Volume is : " <<GetVolume() <<" cm cube " <<endl;
}

Jul 22 '05
42 3211
"Dan" <le*********@ya h00.c0m> wrote:

Hi Dan. You seem to be suffering some serious misconceptions about
object-oriented design. I really recommend you buy a GOOD book
(not a book written before 1999, and not a silly book). There are
lists of recommended books at http://www.accu.org/ .

Also, you are having trouble enough with one class, let alone
a hierarchy of classes. I suggest you try writing "class Circle"
without deriving it from Shape, and get everything working.
Then you can write "class Triangle" (without deriving it from Shape)
and get everything working. Then you can look at the two classes
and move their common features into the class Shape.
class Circle: public Shape
{
public:
void DrawCircle();
void SetVolume(){
cout<<"Enter the radius in cm: " ; cin>> itsRadius;
volume = (4/3) * pow(itsRadius,3 ) * PI ;}
void ShowCircle(Circ le* s, int* count);

protected:
float itsRadius;
};
You seem to think that 'class Circle' is an umbrella for all things
related to circles. This is wrong. A Circle object is ONE CIRCLE.
This one circle has some properties: a centre and a radius. There
are things you can do with this one circle: eg. change its radius,
draw it, colour it in, move it, etc.

Your function "ShowCircle " seems to be a function that draws
_different_ circles. Showing other circles is not something that
you do with one circle. You should change this to be a function
that shows the one circle, eg.

void ShowCircle(void )
{ cout << "this circle has radius: "<< itsRadius << endl; }

and then if you want a function to display many circles, create
a free function:

void ShowCircles(Cir cle *ptr, int n_circles)
{ for (int i = 0; i < n_circles; ++i) ptr[i].ShowCircle(); }

This is GOOD design. I don't know why you think it violates
encapsulation.

Now let me return to your Shape class:
class Shape
{
public:
void CreateShape();
What is this function supposed to do ?? A shape object is a shape
(that already exists). How can you create an object that already exists?
If this function is not meant to apply to one shape, then it should
not be a member of this class.
void Shape::CreateSh ape()
{
Circle draw1[10];
Cylinder draw2[10];
Triangle draw3[10];
This sticks out like a sore thumb. All of the code that relates
to the Shape class should make no mention of Circle, Cylinder, etc.
If it does, you need to rethink your design.

You appear to be creating a Shape and then trying to convert that
Shape into a Circle or whatever, based on what the user wants.
That is WRONG. You cannot do that in C++. What you must do is find
out what the user wants, and then create a Circle, Cylinder, etc.

(Note - in order to do this, you need to use language features which
you haven't used in your program so far (so I'm guessing you wouldn't
have any idea how to do this). Objects are either one type, or another.
A Circle is NOT a shape object. If a function has to return a Shape by
value, or take a Shape by value, you cannot give it a Circle.
You have to use a pointer or a reference to a Shape (which can, in fact,
point or refer to a Circle).
float ChooseColor();
void DisplayShape();

float GetVolume(){ return volume; }

protected:
float volume;
};


"volume" should not really be a member variable. It's bad design
(although not as awful as the things I pointed out above).
The reason it is bad is that in all of your derived classes (eg.
Circle), then whenever you change "radius" you also have to remember
to change "volume". It would be better if there were a function
to calculate the volume based on the radius.

Finally, the topic of this thread was "sharing information in a class".
Classes do not have information. Objects have information. An object's
member functions have access to all the information of that ONE OBJECT.
A class is a set of instructions that say how to make an object, and what
that object can do once it has been created.
What don't you understand about that?

Here is some better code. Study the differences between this code and yours.

#include <iostream>
using namespace std;

class Shape
{
public:
virtual double CalculateVolume () { return 0; }
virtual void Show()
{ cout << "Shape volume: " << CalculateVolume () << endl; }
};

// The word 'virtual' means that if we have a pointer/reference to
// a Shape, and then call CalculateVolume (), then if the Shape is
// actually a Circle, then the circle version of CalculateDouble ()
// will be called.

class Circle: public Shape
{
private:
double radius;
public:
Circle(double r): radius(r) {}
double CalculateVolume () { return 3.14159 * radius * radius; }
};

class Square: public Shape
{
private:
double side_length;
public:
Square(double len): side_length(len ), colour(0) {}
double CalculateVolume () { return side_length * side_length; }
int colour;
virtual void Show()
{ Shape::Show(); cout << "Square colour: " << colour << endl; }
};

Shape *GetNewShape()
{
int n;
for(;;)
{
cout << "Enter 1 for Circle or 2 for Square (0 to quit)" << endl;
cin >> n;
if (n == 1)
return new Circle(6.0);
if (n == 2)
return new Square(3.0);
if (n == 0)
return 0;
}
}

int main(void)
{
Circle circle(5.);
Square square(4.);

Shape *shape_ptr;

shape_ptr = &circle;
shape_ptr->Show();

shape_ptr = &square;
shape_ptr->Show(); // same function, different results

for (;;)
{
shape_ptr = GetNewShape();
if (shape_ptr == 0)
break;

shape_ptr->Show();
delete shape_ptr; // we must now destroy the shape we "new"'d
}

return 0;

// "circle" and "square" get destroyed automatically because they weren't
// created via "new"
}
If you want to update this example to allow user input of the
radius and colour, for example, you could add another virtual
member function UpdatePropertie s(). This would take one existing
circle and allow the user to change its radius (but NOT allow
the user to change its type).
Jul 22 '05 #41

"Dan" <le*********@ya h00.c0m> wrote in message
news:6s******** ******@news20.b ellglobal.com.. .

Jesus, look at the code I wrote

circle[count_circle] = CreateCircle();

look at the code you wrote

circle[count_circle]=circle.CreateC ircle();
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
Jul 22 '05 #42
Dan

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;
}

Jul 22 '05 #43

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1436
by: Magic1812 | last post by:
Magic Software invites you to join us this coming Tuesday (January 13th, 2004) at 12:00 EDT / 17:00 GMT for a FREE live Webinar: Title: Accelerated Application Integration and Information Sharing Date: January 13, 2004 Time: 12:00 PM EST / 17:00 GMT Presenter: Lee Sutton
0
1535
by: Magic1812 | last post by:
Magic Software invites you to join us this coming Tuesday (January 13th, 2004) at 12:00 EDT / 17:00 GMT for a FREE live Webinar: Title: Accelerated Application Integration and Information Sharing Date: January 13, 2004 Time: 12:00 PM EST / 17:00 GMT Presenter: Lee Sutton
16
2256
by: Robert W. | last post by:
I'm building a solution that has 1 component for the Desktop and 1 component for the Pocket PC. (Though this isn't a mobile question). I have a data library that will be shared on both platforms. So I've adopted the approach of having two projects share the same set of files. In my case, I have two similarly named projects: DataObjects DataObjectsCF The former is for the Desktop app and the latter is for the Compact Framework.
5
10525
by: BPearson | last post by:
Hello I would like to have several sites share a single web.config file. To accomplish this, I would point the root of these sites to the same folder. Is there any reason why I might not want to do this? (IIS 5 or 6 In case you're wondering why I would do this, we have many web sites on a single server, and there are groups of sites that need to share common configuration information. I'd like to ease some administration by having one...
5
2434
by: Jeff | last post by:
Hi - I'm creating an installation program that will install a VB.NET program and an instance of MSDE on an unknown desktop (commercial application). The install will include the MSDE redistributable files (and it will launch the MSDE Setup.exe to install MSDE). I've read that "Except on Win 98 and Millennium, file and print sharing must be active"
0
1557
by: Emily | last post by:
Imagine a world where everybody shares and has faith in each other. We have all struggled at one time or another with our jobs or careers and have wondered if there was a better way to make a living. What if the solution was just to help each other, simply by giving and receiving. This would be a radical global experiment in faith. Imagine people all around the world connecting instantaneously and just giving and receiving money to each...
8
4851
by: mc | last post by:
I would like to be able to send from an ASP.NET page an email which when recieved takes the form of a "Sharing Invitation for a RSS Feed" (http://office.microsoft.com/en-us/outlook/HA101595391033.aspx) I've found a MSDN article about it (http://msdn2.microsoft.com/en-us/library/bb176432.aspx) but that example is presented as a vb(a) script from within outlook. Can this functionality be emulated from sending an email from C#? TIA
1
241
by: Jesse Aufiero | last post by:
What is the most sure-fire way to prevent users of my web site from sharing their logon information with others? i want to guarantee that only paying customers are allowed into the site. Thanks
45
3019
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class clsData Private m_objSQLClient As clsSQLClient Private m_objUsers As clsUsers
0
9459
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10115
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10056
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7466
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5365
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4024
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2857
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.