473,768 Members | 8,685 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 #1
42 3206
Dan wrote:
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; }
When you construct a Circle, why not initialise the volume as well?
void CreateCircle();
void ShowCircle();

void SetVolume( float radius){
volume = PI * pow(radius,2); }
In this function you set the volume, but 'itsRadius' never changes
(and stays 0). Is that intentional?

What's "PI"? How is it defined?
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;
If you enter the radius, wouldn't it make sense to set the member
variable to have the same value?

Notice, that you're in the member function. You declared two
_LOCAL_ variables here 'itsRadius' and 'volume'. They have the
same names as _member_ variable, and therefore _hide_ the data
members. Somehow I don't think that was your intention.

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


I (and am sure others too) would like to see how you call those
functions. It's not enough to see the class definition and the
implementation, one always has to see how the class is used.

My suspicion is that you use different circles without realising
it.

Victor
Jul 22 '05 #2
Dan
class Circle
{
public:

Circle (int radius){
itsRadius = 0; }
When you construct a Circle, why not initialise the volume as well?

Ok I will
void CreateCircle();
void ShowCircle();

void SetVolume( float radius){
volume = PI * pow(radius,2); }
In this function you set the volume, but 'itsRadius' never changes
(and stays 0). Is that intentional?

There is a function Set volume, that the radius value is return to calculate
the volume


What's "PI"? How is it defined?

It is a constant in the First line of the program before class definition
PI= 3.14;

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;


If you enter the radius, wouldn't it make sense to set the member
variable to have the same value?

Notice, that you're in the member function. You declared two
_LOCAL_ variables here 'itsRadius' and 'volume'. They have the
same names as _member_ variable, and therefore _hide_ the data
members. Somehow I don't think that was your intention.

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


I (and am sure others too) would like to see how you call those
functions. It's not enough to see the class definition and the
implementation, one always has to see how the class is used.

My suspicion is that you use different circles without realising
it.

Victor



Jul 22 '05 #3
Dan
I (and am sure others too) would like to see how you call those
functions. It's not enough to see the class definition and the
implementation, one always has to see how the class is used.


Ok so here is my full program. it compiles but the answer like I says is
wrong:

#include <iostream>
#include <cmath>
using namespace std;

const double PI = 3.14159;

class Point
{ int x,y, color;

public:
Point(int a=0, int b=0, int c=0){
x=a; y=b; color=c; }
};

class Shape
{ protected:
Point p1;

public:
Shape(Point p) :p1(p) {}

Shape(){};
int SetColor();
void CreateShape(); //choosing the shape
void DrawShape();
void DisplayShape();
protected:

};

class Circle : public Shape
{
public:
Circle (){};
Circle(Point p, int r=0) : Shape(p) {itsRadius = r;}
Circle (int radius){
itsRadius = 0;
volume =0;
}
void CreateCircle();
void ShowCircle();

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

private:

float itsRadius;
float volume;
};

void Shape::CreateSh ape()
{ Circle circ;
char choice;
// Circle circle_array[10];
//int count_circle =0;

do {
cout<<" Shape Management System "<<endl;
cout<< " =============== =============== =============== = "<<endl;
cout <<endl <<endl;
cout<<" 1. Create a Circle "<<endl;
cout<<" 2. Create a Cylinder "<<endl;
cout<<" 3. Create a Triangle "<<endl;
cout<<" 4. Go back to main menu "<<endl;
cout << " Your choice please: " ;
cin >> choice;

switch (choice)
{
case '1':
circ.CreateCirc le() ;
break;
case '2':

break;
case '3':

break;
case '4': cout<<"This exit will go back to the previous menu !" <<endl;
break;
default: cout<<"Error: Invalid option, Please try again" <<endl;
break;
}
}while ( (choice != '4') ) ;
}

void Shape::DisplayS hape()
{ Circle circ;

char choice;

do {
cout<<" Shape Management System "<<endl;
cout<< " =============== =============== =============== = "<<endl;
cout <<endl <<endl;
cout<<" 1. Show Circles created "<<endl;
cout<<" 2. Show Cylinders created "<<endl;
cout<<" 3. Show Triangles created "<<endl;
cout<<" 4. Go back to main menu "<<endl;
cout << " Your choice please: " ;
cin >> choice;

switch (choice)
{
case '1':
circ.ShowCircle () ; //SetTime( &s[0], &count, &totalTime[0] );
break;
case '2':

break;
case '3':

break;
case '4': cout<<"This exit will go back to the previous menu !" <<endl;
break;
default: cout<<"Error: Invalid option, Please try again" <<endl;
break;
}

}while ( (choice != '4') ) ;
}

void menu()
{ char choice;
Circle draw;

float *count =0;

do {
cout<<endl <<" Shape Management System "<<endl;
cout<< " =============== =============== =============== = "<<endl;
cout<<" 1. Create Object "<<endl;
cout<<" 2. Display Object Created "<<endl;
cout<<" 3. Quit "<<endl;
cout << " Your choice please: " ;
cin >> choice;

switch (choice)
{
case '1':
draw.CreateShap e(); //SetTime( &s[0], &count, &totalTime[0] );
break;
case '2':
draw.DisplaySha pe();
break;
case '3': cout<<"Thank you for having used this system, Bye Bye!!!";
break;
default: cout<<"Error: Invalid option, Please try again" << endl <<endl;
}

}while ( (choice != '3') ) ;
}

float main()
{
menu();
getch();
return 0;
}

void Circle::CreateC ircle() //Draw a circle
{ Circle cir(0);
char color;
float itsRadius;

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

cir.SetVolume(i tsRadius);

cout<<" Volume: " << cir.GetVolume() <<endl;

color = SetColor();
}

void Circle::ShowCir cle()
{
cout <<" Volume is : " << GetVolume() <<" cm cube " <<endl;

}

int Shape::SetColor () //Functions for the base class
{ char input;

cout<<" Choose a color from the following menu: " <<endl;
cout<<" 1. Blue "<<endl;
cout<<" 2. Green "<<endl;
cout<<" 3. Red "<<endl;
cout<<" 4. Orange "<<endl;
cout<<" 5. Yellow "<<endl;
cout<<" 6. Purple "<<endl;

cin >> input ;
if (input == '1' || input == '2' || input == '3' || input == '4' || input
== '5' || input == '6') return input ;
else
{ cout<<"You have not choosen a color, Please choose a number between 1 to
6 !" <<endl;
}

return 0;
}


Jul 22 '05 #4
Dan wrote:
I (and am sure others too) would like to see how you call those
functions. It's not enough to see the class definition and the
implementatio n, one always has to see how the class is used.

Ok so here is my full program. it compiles but the answer like I says is
wrong:


Of course. Your program contains several logical errors that your
compiler cannot detect. I says you should works on it some more.

#include <iostream>
#include <cmath>
using namespace std;

const double PI = 3.14159;

class Point
{ int x,y, color;

public:
Point(int a=0, int b=0, int c=0){
x=a; y=b; color=c; }
Use initialisation instead of assignment wherever possible.
See FAQ for more explanation on that.
};

class Shape
{ protected:
Point p1;

public:
Shape(Point p) :p1(p) {}

Shape(){};
int SetColor();
void CreateShape(); //choosing the shape
void DrawShape();
void DisplayShape();
protected:

};

class Circle : public Shape
{
public:
Circle (){};
Circle(Point p, int r=0) : Shape(p) {itsRadius = r;}
So, you initialise the base, but not the radius. Why? Do the
same for 'itsRadius' member as you do for 'Shape' base class.


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

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

private:

float itsRadius;
float volume;
};

void Shape::CreateSh ape()
{ Circle circ;
So, 'circ' is a local circle.
char choice;
// Circle circle_array[10];
//int count_circle =0;

do {
cout<<" Shape Management System "<<endl;
cout<< " =============== =============== =============== = "<<endl;
cout <<endl <<endl;
cout<<" 1. Create a Circle "<<endl;
cout<<" 2. Create a Cylinder "<<endl;
cout<<" 3. Create a Triangle "<<endl;
cout<<" 4. Go back to main menu "<<endl;
cout << " Your choice please: " ;
cin >> choice;

switch (choice)
{
case '1':
circ.CreateCirc le() ;
OK, here you "create" a local Circle object. It will be destroyed
(_lost_, that is) as soon as this function exits.
break;
case '2':

break;
case '3':

break;
case '4': cout<<"This exit will go back to the previous menu !" <<endl;
break;
default: cout<<"Error: Invalid option, Please try again" <<endl;
break;
}
}while ( (choice != '4') ) ;
}

void Shape::DisplayS hape()
{ Circle circ;
Now, here is another local Circle object. Initialised to what?
Zero for the radius and what for the volume?

char choice;

do {
cout<<" Shape Management System "<<endl;
cout<< " =============== =============== =============== = "<<endl;
cout <<endl <<endl;
cout<<" 1. Show Circles created "<<endl;
cout<<" 2. Show Cylinders created "<<endl;
cout<<" 3. Show Triangles created "<<endl;
cout<<" 4. Go back to main menu "<<endl;
cout << " Your choice please: " ;
cin >> choice;

switch (choice)
{
case '1':
circ.ShowCircle () ; //SetTime( &s[0], &count, &totalTime[0] );
And what do you expect to see? The contents of the local (to this
function) Circle object. Two different local Circle objects have
absolutely _nothing_ to do with each other.
break;
case '2':

break;
case '3':

break;
case '4': cout<<"This exit will go back to the previous menu !" <<endl;
break;
default: cout<<"Error: Invalid option, Please try again" <<endl;
break;
}

}while ( (choice != '4') ) ;
}

void menu()
{ char choice;
Circle draw;
Another local Circle object. They are like cockroaches, everywhere,
aren't they?

float *count =0;

do {
cout<<endl <<" Shape Management System "<<endl;
cout<< " =============== =============== =============== = "<<endl;
cout<<" 1. Create Object "<<endl;
cout<<" 2. Display Object Created "<<endl;
cout<<" 3. Quit "<<endl;
cout << " Your choice please: " ;
cin >> choice;

switch (choice)
{
case '1':
draw.CreateShap e(); //SetTime( &s[0], &count, &totalTime[0] );
break;
case '2':
draw.DisplaySha pe();
break;
case '3': cout<<"Thank you for having used this system, Bye Bye!!!";
break;
default: cout<<"Error: Invalid option, Please try again" << endl <<endl;
}

}while ( (choice != '3') ) ;
Well, at least in this function you create and show it in one function,
so it doesn't change between "create" and "show"...
}

float main()
"float" main? You're not allowed to overload 'main', IIRC.
{
menu();
getch();
return 0;
}

void Circle::CreateC ircle() //Draw a circle
{ Circle cir(0);
char color;
float itsRadius;
Well, and here, as I already mentioned to you, the 'itsRadius'
variable has nothing to do with the 'itsRadius' data member except
the same name. Did you intend to use 'itsRadius' member here or
did you intend to use a local variable? It's better to name them
differently, unless _hiding_ (sometimes called "shadowing" ) is the
effect you desire.

cout<<"Enter the radius in cm: " ;
cin>> itsRadius;
You're entering the value that will be stored only in the local
float variable, not in the data member.

cir.SetVolume(i tsRadius);

cout<<" Volume: " << cir.GetVolume() <<endl;

color = SetColor();
}

void Circle::ShowCir cle()
{
cout <<" Volume is : " << GetVolume() <<" cm cube " <<endl;

}

int Shape::SetColor () //Functions for the base class
{ char input;

cout<<" Choose a color from the following menu: " <<endl;
cout<<" 1. Blue "<<endl;
cout<<" 2. Green "<<endl;
cout<<" 3. Red "<<endl;
cout<<" 4. Orange "<<endl;
cout<<" 5. Yellow "<<endl;
cout<<" 6. Purple "<<endl;

cin >> input ;
if (input == '1' || input == '2' || input == '3' || input == '4' || input
== '5' || input == '6') return input ;
else
{ cout<<"You have not choosen a color, Please choose a number between 1 to
'chosen' is misspelled.
6 !" <<endl;
}

return 0;
}


Victor
Jul 22 '05 #5
Dan
> > cout<<"Enter the radius in cm: " ;
cin>> itsRadius;


You're entering the value that will be stored only in the local
float variable, not in the data member.

cir.SetVolume(i tsRadius);

cout<<" Volume: " << cir.GetVolume() <<endl;

color = SetColor();
}

void Circle::ShowCir cle()
{
cout <<" Volume is : " << GetVolume() <<" cm cube " <<endl;

}

int Shape::SetColor () //Functions for the base class
{ char input;


Ok , so How do I make this value volume global to the class then ? is it
possible??.
So that other function can use it, Now like you said it does get removed
everytime I exit

Jul 22 '05 #6
Dan
Here is a anotehr wy to ask my question:
How can I get the value of ''pConstRect->GetWidth() '' outside of the
function where pConstRect was declared:

#include <iostream>
#include <conio.h>
using namespace std;

class Rectangle
{
public:
Rectangle();
~Rectangle();
void SetLength(int length) { itsLength = length; }
int GetLength() const { return itsLength; }

void SetWidth(int width) { itsWidth = width; }
int GetWidth() const { return itsWidth; }

void ShowWidth();

private:
int itsLength;
int itsWidth;
};
Rectangle::Rect angle():
itsWidth(5),
itsLength(10)
{}
Rectangle::~Rec tangle()
{}

int main()
{
Rectangle* pRect = new Rectangle;
const Rectangle * pConstRect = new Rectangle;

cout << "pConstRect width: " << pConstRect->GetWidth() << "
feet\n";

getch();
return 0;
}

void Rectangle::Show Width()
{

cout<<"hello: " << pConstRect->GetWidth() <<endl;
}
Jul 22 '05 #7
"Dan" <le*********@ya h00.c0m> wrote in message
news:5D******** **********@news 20.bellglobal.c om...
cout<<"Enter the radius in cm: " ;
cin>> itsRadius;


You're entering the value that will be stored only in the local
float variable, not in the data member.

cir.SetVolume(i tsRadius);

cout<<" Volume: " << cir.GetVolume() <<endl;

color = SetColor();
}

void Circle::ShowCir cle()
{
cout <<" Volume is : " << GetVolume() <<" cm cube " <<endl;

}

int Shape::SetColor () //Functions for the base class
{ char input;


Ok , so How do I make this value volume global to the class then ? is it
possible??.
So that other function can use it, Now like you said it does get removed
everytime I exit


Just remove its declaration from that function. The member variable
will be used instead of that local one.

V
Jul 22 '05 #8
"Dan" <le*********@ya h00.c0m> wrote...
Here is a anotehr wy to ask my question:
How can I get the value of ''pConstRect->GetWidth() '' outside of the
function where pConstRect was declared:
You either pass the value as an argument or pass the 'pConstRect'
an an argument.

Where do you need it "outside"?

#include <iostream>
#include <conio.h>
using namespace std;

class Rectangle
{
public:
Rectangle();
~Rectangle();
void SetLength(int length) { itsLength = length; }
int GetLength() const { return itsLength; }

void SetWidth(int width) { itsWidth = width; }
int GetWidth() const { return itsWidth; }

void ShowWidth();

private:
int itsLength;
int itsWidth;
};
Rectangle::Rect angle():
itsWidth(5),
itsLength(10)
{}
Rectangle::~Rec tangle()
{}

int main()
{
Rectangle* pRect = new Rectangle;
const Rectangle * pConstRect = new Rectangle;

cout << "pConstRect width: " << pConstRect->GetWidth() << "
feet\n";

getch();
return 0;
}

void Rectangle::Show Width()
{

cout<<"hello: " << pConstRect->GetWidth() <<endl;
}

Jul 22 '05 #9
Dan
> Just remove its declaration from that function. The member variable
will be used instead of that local one.

There are no declaration in ShowCircle()
and If I remove the one in CreateCircle(), then it still gives me the same
eronous results when I choose to display them ( the other function() )
Jul 22 '05 #10

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
2255
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
10524
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
1556
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
3014
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
9578
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10188
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...
1
9973
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,...
0
8845
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7391
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
5429
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3941
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
3543
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
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.