Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: How do I access another type's method from one type's method

Reply
Thread Tools

Re: How do I access another type's method from one type's method

 
 
Howard
Guest
Posts: n/a
 
      07-03-2003

"ding feng" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I got this program:
>
> class building{
> ...
> };
>
> class schoolublic building{
> ...
>
> int getstudentnumber(int m);
> };
>
> class hospitalublic building{
> ...
>
> int reportpatientnumber();
> };
>
> int hospital:reportpatientnumber()
> {
> //here I need to call school method getstudentnumber(int m);
> }
>
> In hosptial:reportpatientnumber() method, I can't pass anything as a

parameter,
> for example, can't pass a school type pointer. The parameter lists
> MUST be empty.
>
> Is there an alternative way to do that?


If you don't have a school object, then how can your code know which school
to get a student number from? You have to have SOME method of getting an
instance of the school that you want to call that function on.

Unless, of course, those student numbers are the same for ALL school...?, In
that case, you can make the getstudentnumber a static function, and just
call school::getstudentnumber.

Where is m coming from in reportpatientnumber?

And why in the world do the parameter lists have to be empty??? There IS a
parameter in getstudentnumber, you'll notice. (Is this an assignment for a
class? If so, then I guess I just gave you the answer...look up 'static' in
your textbook.)


-Howard




 
Reply With Quote
 
 
 
 
ding feng
Guest
Posts: n/a
 
      07-04-2003
"Howard" <(E-Mail Removed)> wrote in message news:<be2c68$(E-Mail Removed)>...
> "ding feng" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > I got this program:
> >
> > class building{
> > ...
> > };
> >
> > class schoolublic building{
> > ...
> >
> > int getstudentnumber(int m);
> > };
> >
> > class hospitalublic building{
> > ...
> >
> > int reportpatientnumber();
> > };
> >
> > int hospital:reportpatientnumber()
> > {
> > //here I need to call school method getstudentnumber(int m);
> > }
> >
> > In hosptial:reportpatientnumber() method, I can't pass anything as a

> parameter,
> > for example, can't pass a school type pointer. The parameter lists
> > MUST be empty.
> >
> > Is there an alternative way to do that?

>
> If you don't have a school object, then how can your code know which school
> to get a student number from? You have to have SOME method of getting an
> instance of the school that you want to call that function on.
>
> Unless, of course, those student numbers are the same for ALL school...?, In
> that case, you can make the getstudentnumber a static function, and just
> call school::getstudentnumber.
>
> Where is m coming from in reportpatientnumber?
>
> And why in the world do the parameter lists have to be empty??? There IS a
> parameter in getstudentnumber, you'll notice. (Is this an assignment for a
> class? If so, then I guess I just gave you the answer...look up 'static' in
> your textbook.)
>
>
> -Howard



Thanx for your reply.

of course, I can declare that school school1, shool2..., hospital
hospital1, hospital2... Then there will be objects.

I just used this example to represent what I am going to deal with in
my real problem.

In my real problem, I have to have empty parameter lists for one
derived class method and I have to call another UNKNOWN derived class'
method from this method. I say unknown here doesn't really mean
unknown. It actually depends on the domain I am dealing with.

It might be better for you to imagine this senario: in some hospital,
a group of people has got some kind of disease. The task is to check
how many patients in the hospital. According to some conditions or
decisions by a group of expert, this group involved all the students
from some school. So school1.getstudentnumber(m) is called and the
total number of the students is returned, then the patients number for
this kind of disease is obtained.

Well, you may complain my design is not good. But it is not related to
my question. My question however is: is it possible to access another
class method from this class method in C++? How can I get a pointer
for another class so that its method can be accessed? is there an
alternative way to do that?
 
Reply With Quote
 
 
 
 
Michiel Salters
Guest
Posts: n/a
 
      07-04-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (ding feng) wrote in message news:<(E-Mail Removed) om>...
> "Howard" <(E-Mail Removed)> wrote in message news:<be2c68$(E-Mail Removed)>...
> > "ding feng" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) om...
> > > I got this program:
> > >
> > > class building{
> > > ...
> > > };
> > >
> > > class schoolublic building{
> > > ...
> > >
> > > int getstudentnumber(int m);
> > > };
> > >
> > > class hospitalublic building{
> > > ...
> > >
> > > int reportpatientnumber();
> > > };
> > >
> > > int hospital:reportpatientnumber()
> > > {
> > > //here I need to call school method getstudentnumber(int m);
> > > }
> > >



> of course, I can declare that school school1, shool2..., hospital
> hospital1, hospital2... Then there will be objects.


Yes. This is an important distinction to make. You have one
class school, but the number of objects of type school may
vary. It may even vary in time; school* NewSchool = new school;
would create one and delete NewSchool; would decrease the number.

> In my real problem, I have to have empty parameter lists for one
> derived class method and I have to call another UNKNOWN derived class'
> method from this method. I say unknown here doesn't really mean
> unknown. It actually depends on the domain I am dealing with.


Then your design is probably wrong. You're missing information.

> It might be better for you to imagine this senario: in some hospital,
> a group of people has got some kind of disease. The task is to check
> how many patients in the hospital. According to some conditions or
> decisions by a group of expert, this group involved all the students
> from some school. So school1.getstudentnumber(m) is called and the
> total number of the students is returned, then the patients number for
> this kind of disease is obtained.


Why school1? How does hospital1 (the object on which the method
is called) know school1? What's wrong with school2? or *NewSchool?
or citySchools[3]?

Your design should relate schools to hospitals, if any hospital
uses schools

> Well, you may complain my design is not good. But it is not related to
> my question. My question however is: is it possible to access another
> class method from this class method in C++? How can I get a pointer
> for another class so that its method can be accessed? is there an
> alternative way to do that?


There are a lot of methods. There are in fact so many methods that I
can't list all of them here. They are all based on knowing objects,
though. You need a real school object to ask how many students there
are. Once you have such an object S of type school, just use the
syntax S.getstudentnumber( m ).

How you calculate a value for m I don't know. What does it mean?

Regards,
--
Michiel Salters
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to access: a page from a User control, and another User controlfrom another one? qwerty ASP .Net 3 09-30-2004 05:32 PM
Using One XSLT and multiple XML Problem (One is XML and another one is XBRL) loveNUNO XML 2 11-20-2003 06:47 AM
Passing value from one script on one page to another script on another page. Robert Cohen ASP General 3 07-15-2003 01:46 PM
Re: How do I access another type's method from one type's method Rolf Magnus C++ 1 07-04-2003 02:38 AM



Advertisments