473,789 Members | 2,284 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 3215

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

void Circle::Display () const
{
cout<<"CenterPo int: " << GetCenterPoint( ) << endl <<"Radius: " <<
GetRadius() <<endl <<"Area: " << GetArea() <<endl;
}

You showed me this function. I would say that is good for one value, cause
Get Radius will get one value from that function in the class. But I need to save those values into an array. Up to ten times because my array is only that long.


Right. There is nothing in the above function to stop you calling it ten
times for ten different objects

Circle circle[10];

for (int i = 0; i < 10; i++)
{
cout << "circle #" << i + 1 << " " << circle[i].Display() << endl;
}

This is how you write class methods, the class method works for *one* object
(usually), you then call it as many times as you need for as many different
objects as you have. This is more flexible and simpler.

john
Jul 22 '05 #31

"Dan" <le*********@ya h00.c0m> wrote in message
news:_b******** *****@news20.be llglobal.com...
class Circle : public Shape
{
public:
Circle(Point center, float radius) : Shape(center), _radius(radius)
{
}
I don't get this _radius(radius


Its more or less the same as this

Circle(Point center, float radius) : Shape(center)
{
_radius = radius;
}

But there are situations when the first form is better, look up initialiser
lists in your favourite C++ book. But its a minor point, no big deal.
float Circle::GetArea () const
{
return PI * pow(radius,2);
Ya thats smart, one function instead of one , neat.

The Display method doesn't take all those incorrect parameters, the

Display
method displays a circle using the class member variables and methods, its does not pick a Circle from an array and display that.

You could write a function to pick a circle from and array if you wanted

to,
but that function should not be a member of the Circle class.


Did you tell me the other day that whenever I leave a function I also

loose my information unless it is in an array ?? so This is why I was doing that
You lose information from variables (and arrays) which are local to that
function (unless you return them from that function). But class member
variables are not local to any one function, they are only destroyed when
the object itself is destroyed.

I think this is the most important thing you aren't getting. The lifetime
and scope of objects and variables. I guess the only answer is more practice
and study. I'd still advise you work with a simpler program. For instance
its not a good idea to be trying to learn inheritance as well, while you are
still struggling with the basics of objects and classes.

Really you should start again, I know it difficult to do this, but you are really flogging a dead horse with your current code. Make the Circle class look like the one I have above, same for the other classes. Then declare
your arrays like this

Circle circle[10];
Cylinder cylinder[10];
Triangle triangle[10];
int count_circle;
int count_cylinder;
int count_triangle;
Ok creating an array there is fine ( along my menu)
But I need to pass the array to my function no ?


Maybe see below
The user is going have to
put in his information, and that information will be put in Circle array. If I just call a void function in my menu how can I insert that information
into my array. ? This is my problem in c++ is how to point object put
and take values and being to play as much as.. well as need anyway

I think you were doing that more or less OK, there were two problems, first
you had two different arrays, secondly your CreateCircle function was
wrongly part of the Circle class.

Something like this should do the trick, this is using a return value.

void menu()
{
Circle circle[10];
Cylinder cylinder[10];
Triangle triangle[10];
int count_circle = 0;
int count_cylinder = 0;
int count_triangle = 0;

switch (...)
{
case ...:
circle[count_circle] = CreateCircle();
count_circle++;
break;
...
}
}

Circle CreateCircle()
{
cout << "enter circle radius ";
float radius;
cin >> radius;
cout << "enter circle center point";
Point center;
cin >> center;
return Circle(center, radius);
}

but you could do it with parameters like you were doing, the important thing
is that CreateCircle is not, and doesn't need to be, part of the Circle
class.
I tried adding the const atthe end of the function but I get this error:
DisplayCircle' : overloaded member function 'void (void)' not found in
'Circle'

Probably you didn't add it in both places.

class Circle
{
void DisplayCircle() const; // const here
};

void Circle::Display () const // and here
{
...
}

but it not a big deal for you at the moment (its an important topic for
later) so leave it out if it is causing problems.
And if you are not passing anything with you void function, how can you keep track of you count if it was created in the menu function.?
BTW , where do you live ?
and thanks a million for you help John

Jul 22 '05 #32
> Right. There is nothing in the above function to stop you calling it ten
times for ten different objects

Circle circle[10];

for (int i = 0; i < 10; i++)
{
cout << "circle #" << i + 1 << " " << circle[i].Display() << endl;
}

This is how you write class methods, the class method works for *one* object (usually), you then call it as many times as you need for as many different objects as you have. This is more flexible and simpler.


Sorry the above code is garbage, something like this is what I meant

Circle circle[10];

for (int i = 0; i < 10; i++)
{
cout << "circle #" << i + 1 << endl;
circle[i].Display();
}

john
Jul 22 '05 #33
Dan
> Circle circle[10];

for (int i = 0; i < 10; i++)
{
cout << "circle #" << i + 1 << " " << circle[i].Display() << endl;
}

This is how you write class methods, the class method works for *one* object (usually), you then call it as many times as you need for as many different objects as you have. This is more flexible and simpler.


Ok I tried this , but it still not showing me the right radius value,
I am passing it by reference, and my count by value.
class Circle: public Shape
{
public:
Circle(){};
Circle(Point p, float r) : Shape(p) {radius = r;
cout<<"rad:" << radius; }

void CreateCircle() ;
void DisplayCircle(C ircle* circle ,int);

float GetArea(){ return PI * ( pow(radius,2) ); }

private:
float radius;

};

void Circle::Display Circle(Circle* circle, int count_circle)
{
if ( (count_circle) <= 0)
{ cout<< "There are no Circles to display! Please choose another option "
<<endl; }
else
{
for (int i=0; i< (count_circle); i++)
{
cout<<" Here is the area for circle #" << i+1 <<endl;
cout <<"Area: " << circle[i].GetArea() <<endl;
cout <<endl;
}
}
}

and in my switch :
( and this is where I initialize the Circle array and counter.

case '1':
circle[count_circle].CreateCircle() ;
count_circle++;
break;
Jul 22 '05 #34
Dan
void menu()
{
Circle circle[10];
Cylinder cylinder[10];
Triangle triangle[10];
int count_circle = 0;
int count_cylinder = 0;
int count_triangle = 0;

switch (...)
{
case ...:
circle[count_circle] = CreateCircle();
count_circle++;
break;


This is giving me an error,: undeclared identifier
so I used this:
circle[count_circle]=circle.CreateC ircle();
but then error: '.CreateCircle' must have class/struct/union type

But It is, circle is the object , and that object is calling the function.
This is what I am seeing in my documentation.
The problem is , that books gives you a very basic example of classes,
functions, but very rarely a mixed of all that. This is why I am having
problem manipulating the information, but not so much with the basic coding
and syntax.


Jul 22 '05 #35

"Dan" <le*********@ya h00.c0m> wrote in message
news:HF******** ******@news20.b ellglobal.com.. .
Circle circle[10];

for (int i = 0; i < 10; i++)
{
cout << "circle #" << i + 1 << " " << circle[i].Display() << endl;
}

This is how you write class methods, the class method works for *one* object
(usually), you then call it as many times as you need for as many

different
objects as you have. This is more flexible and simpler.


Ok I tried this , but it still not showing me the right radius value,
I am passing it by reference, and my count by value.
class Circle: public Shape
{
public:
Circle(){};
Circle(Point p, float r) : Shape(p) {radius = r;
cout<<"rad:" << radius; }

void CreateCircle() ;
void DisplayCircle(C ircle* circle ,int);

float GetArea(){ return PI * ( pow(radius,2) ); }

private:
float radius;

};

void Circle::Display Circle(Circle* circle, int count_circle)
{
if ( (count_circle) <= 0)
{ cout<< "There are no Circles to display! Please choose another option

" <<endl; }
else
{
for (int i=0; i< (count_circle); i++)
{
cout<<" Here is the area for circle #" << i+1 <<endl;
cout <<"Area: " << circle[i].GetArea() <<endl;
cout <<endl;
}
}
}

and in my switch :
( and this is where I initialize the Circle array and counter.

case '1':
circle[count_circle].CreateCircle() ;
count_circle++;
break;


You're making the same mistake again!!! You've got everything I said
backwards, Sorry but I give up.

john
Jul 22 '05 #36

"Dan" <le*********@ya h00.c0m> wrote in message
news:0g******** ******@news20.b ellglobal.com.. .
void menu()
{
Circle circle[10];
Cylinder cylinder[10];
Triangle triangle[10];
int count_circle = 0;
int count_cylinder = 0;
int count_triangle = 0;

switch (...)
{
case ...:
circle[count_circle] = CreateCircle();
count_circle++;
break;
This is giving me an error,: undeclared identifier
so I used this:
circle[count_circle]=circle.CreateC ircle();
but then error: '.CreateCircle' must have class/struct/union type


Jesus, look at the code I wrote

circle[count_circle] = CreateCircle();

look at the code you wrote

circle[count_circle]=circle.CreateC ircle();

See the difference? In my version CreateCircle is *not* a member of the
Circle class, I really don't know why you think it should be. I said it
should not in black and white.
The problem is , that books gives you a very basic example of classes,
functions, but very rarely a mixed of all that. This is why I am having
problem manipulating the information, but not so much with the basic coding and syntax.


Yes I can see that. Work with simpler examples until you get it

john
Jul 22 '05 #37
>
You're making the same mistake again!!! You've got everything I said
backwards, Sorry but I give up.


Sorry about that but I think newsgroups are not a good place to learn the
kind of things you need to learn. As you realise you're struggling with
concepts not with syntax (your syntax is pretty good). What you really need
is a teacher who can explain this kind of stuff to you. I don't think you
are going to get it from a newsgroup, it just not the right medium.

John


Jul 22 '05 #38
Dan
>
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 ?
Jul 22 '05 #39
Dan

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2j******** ****@uni-berlin.de...

You're making the same mistake again!!! You've got everything I said
backwards, Sorry but I give up.

Sorry about that but I think newsgroups are not a good place to learn the
kind of things you need to learn. As you realise you're struggling with
concepts not with syntax (your syntax is pretty good). What you really

need is a teacher who can explain this kind of stuff to you. I don't think you
are going to get it from a newsgroup, it just not the right medium.

John


I know I am trying to learn this, and I know I am not the fastest person on
earth to learn this crap. BUt this is where I am at know. I know how
function arryas and classes work individually, but when comes time to mixe
them all up together I'm mixed up, I have been working on this since 8:00
this morning and I have not advanced very much. Thanks for you help and all
those posts. I will be reading all those posts again now see if I missed
anything.
D
Jul 22 '05 #40

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

Similar topics

0
1437
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
2257
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
10531
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
2436
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
3021
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
9657
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
9502
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
10400
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
6754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5412
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...
0
5544
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4084
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
3688
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2902
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.