473,776 Members | 2,201 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 3213
Dan

Where do you need it "outside"?

In ShowWidth() I want to get pConstRect->GetWidth() , but it do not
work, only in the function where pConstRect has been declared. I need it to
get this information in another function., This is the same problem as the
other problem with the radius..


#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 #11
"Dan" <le*********@ya h00.c0m> wrote...

Where do you need it "outside"? In ShowWidth() I want to get pConstRect->GetWidth() , but it do not
work, only in the function where pConstRect has been declared. I need it

to get this information in another function., This is the same problem as the other problem with the radius..
Perhaps you need to give a systematic C++ study a try. You
apparently have no understanding of objects, members, and how
they fit together.

OK, I don't know if this is going to help your efforts or hurt
them in the long run, but here is the solution for you: just
remove the "pConstRect->" from the 'ShowWidth' function.


#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 #12
"Dan" <le*********@ya h00.c0m> wrote...
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() )


Dan, you need to sit down either with a good book that would teach
you object-oriented features of C++ step by step, or with a teacher
who will hold your hand while answering all your questions and all
your concerns about that program of yours. I am not a book and the
Usenet is not a good place for hand-holding of pupils. I strongly
recommend you to start over. Without thorough understanding of the
basics you won't be able to move ahead even if we write the program
for you. Start with simpler things. Work your way up. Visit the
alt.comp.lang.l earn.c-c++ newsgroup, it's more for newcomers like
you.

Good luck!
Jul 22 '05 #13
Dan

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() )


Dan, you need to sit down either with a good book that would teach
you object-oriented features of C++ step by step, or with a teacher
who will hold your hand while answering all your questions and all
your concerns about that program of yours. I am not a book and the
Usenet is not a good place for hand-holding of pupils. I strongly
recommend you to start over. Without thorough understanding of the
basics you won't be able to move ahead even if we write the program
for you. Start with simpler things. Work your way up. Visit the
alt.comp.lang.l earn.c-c++ newsgroup, it's more for newcomers like
you.

Good luck!


Well I know what you mean, But I am starting over reading my stuff. BUt
like many books I have seen so far, They teach about basic functions and
another chapter about class. But they don't cover all the possibilities
mixing all of those types together and the diferent syntax these involves.
One of the book that I bought, and recently fount it on the net at:
http://newdata.box.sk/bx/c/ seems to be good, But like they tell you what
and how to call a function from the main ( or only one function) tell what
a class is and bl;a bla bla, but but the syntax changes when calling a
function through another function from the same class. Maybe O have not
picked up the right book yet for me or I just can't understand straight.
Jul 22 '05 #14

"Dan" <le*********@ya h00.c0m> wrote in message
news:M4******** **********@news 20.bellglobal.c om...

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() )
Dan, you need to sit down either with a good book that would teach
you object-oriented features of C++ step by step, or with a teacher
who will hold your hand while answering all your questions and all
your concerns about that program of yours. I am not a book and the
Usenet is not a good place for hand-holding of pupils. I strongly
recommend you to start over. Without thorough understanding of the
basics you won't be able to move ahead even if we write the program
for you. Start with simpler things. Work your way up. Visit the
alt.comp.lang.l earn.c-c++ newsgroup, it's more for newcomers like
you.

Good luck!
Well I know what you mean, But I am starting over reading my stuff. BUt
like many books I have seen so far, They teach about basic functions and
another chapter about class. But they don't cover all the possibilities
mixing all of those types together and the diferent syntax these involves.
One of the book that I bought, and recently fount it on the net at:
http://newdata.box.sk/bx/c/ seems to be good, But like they tell you what
and how to call a function from the main ( or only one function) tell

what a class is and bl;a bla bla, but but the syntax changes when calling a
function through another function from the same class. Maybe O have not
picked up the right book yet for me or I just can't understand straight.


Often recommended in this group is Thinking in C++ by Bruce Eckel, free from
www.mindview.net.

Your book looks reasonable (I doubt its legal though) but it is of course
completely impossible to learn C++ in 21 days. Two years is a more likely
figure.

john
Jul 22 '05 #15
Dan
>
Well I know what you mean, But I am starting over reading my stuff. BUt
like many books I have seen so far, They teach about basic functions and
another chapter about class. But they don't cover all the possibilities
mixing all of those types together and the diferent syntax these involves.
One of the book that I bought, and recently fount it on the net at:
http://newdata.box.sk/bx/c/ seems to be good, But like they tell you what
and how to call a function from the main ( or only one function) tell what a class is and bl;a bla bla, but but the syntax changes when calling a
function through another function from the same class. Maybe O have not
picked up the right book yet for me or I just can't understand straight.


And again , I am using the same technique as above but with a different
program , this time I not using main but two other functions and it still
wont give me the right answer.This is what I mean , two funtions, two
programs and two different rules this is why I am confused. This below
compiles but showCircle don't give me the right answer.
void Circle::CreateC ircle()
{ Circle * cir = new Circle;

float itsRadius;

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

cir->SetVolume(itsR adius);
cout<<" Volume: " << cir->GetVolume() <<endl;

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

Jul 22 '05 #16
"Dan" <le*********@ya h00.c0m> wrote...

Well I know what you mean, But I am starting over reading my stuff. BUt
like many books I have seen so far, They teach about basic functions and another chapter about class. But they don't cover all the possibilities
mixing all of those types together and the diferent syntax these involves. One of the book that I bought, and recently fount it on the net at:
http://newdata.box.sk/bx/c/ seems to be good, But like they tell you what and how to call a function from the main ( or only one function) tell what
a class is and bl;a bla bla, but but the syntax changes when calling a
function through another function from the same class. Maybe O have not
picked up the right book yet for me or I just can't understand

straight.
And again , I am using the same technique as above but with a different
program , this time I not using main but two other functions and it still
wont give me the right answer.This is what I mean , two funtions, two
programs and two different rules this is why I am confused. This below
compiles but showCircle don't give me the right answer.
void Circle::CreateC ircle()
{ Circle * cir = new Circle; ^^^^^^^^^^^^^^^ ^^^^^^^^^^^
Throw this declaration/definition/initialisation out. You don't need it.
float itsRadius;
Throw this declaration/definition out. It screws things up for you.

cout<<"Enter the radius in cm: " ;
cin>> itsRadius;
Now it will read into the _member_ variable. That's what you need.

cir->SetVolume(itsR adius);
Don't use "cir->". Throw "cir->" away. Use "this->":

this->SetVolume(itsR adius);
cout<<" Volume: " << cir->GetVolume() <<endl;
Again, replace "cir->" with "this->".

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


Learning C++ like this is detrimental to the cause. You should use
_systematic_ _study_. Do you understand the word "systematic "?
Jul 22 '05 #17
Dan wrote:
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;
};


BTW, circles don't have volumes, they have areas.
Cylinders, spheres and cones have volumes.
Three dimensional objects have volumes, two
dimensional have areas.

The same with rectangles and boxes. Rectangles
have areas, boxes have volume.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #18
Dan
> > void Circle::CreateC ircle()
{ Circle * cir = new Circle;

^^^^^^^^^^^^^^^ ^^^^^^^^^^^
Throw this declaration/definition/initialisation out. You don't need it.
float itsRadius;


Throw this declaration/definition out. It screws things up for you.

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


Now it will read into the _member_ variable. That's what you need.

cir->SetVolume(itsR adius);


Don't use "cir->". Throw "cir->" away. Use "this->":

this->SetVolume(itsR adius);
cout<<" Volume: " << cir->GetVolume() <<endl;


Again, replace "cir->" with "this->".

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


Learning C++ like this is detrimental to the cause. You should use
_systematic_ _study_. Do you understand the word "systematic "?


Thanks for the help, Ya I know what systematic means. BUt I think learning
a new language is confusing, or this could only be me. I did the
modification but , I still don't get a value for Getvolume in ShowCircle.
Would passing those values by reference work ? right now I have only one
value , but later I would like to create many circles and store them into an
array. Would your method work ? I tried using pointer and passing by
reference but with no luck, but if you tell me thats the way I will do
more work on it.
thanks
Jul 22 '05 #19
Dan
BTW, circles don't have volumes, they have areas.
Cylinders, spheres and cones have volumes.
Three dimensional objects have volumes, two
dimensional have areas.

The same with rectangles and boxes. Rectangles
have areas, boxes have volume.


I know that , I just want to get the program, or part of it working, I dont
need to be mathematically correct for now.
Jul 22 '05 #20

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
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
9625
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
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
10282
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
9920
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8948
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
7467
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
4027
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
3619
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.