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

Discussion in 'C++' started by Howard, Jul 3, 2003.

  1. Howard

    Howard Guest

    "ding feng" <> wrote in message
    news:...
    > I got this program:
    >
    > class building{
    > ...
    > };
    >
    > class school:public building{
    > ...
    >
    > int getstudentnumber(int m);
    > };
    >
    > class hospital:public 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
     
    Howard, Jul 3, 2003
    #1
    1. Advertising

  2. Howard

    ding feng Guest

    "Howard" <> wrote in message news:<be2c68$>...
    > "ding feng" <> wrote in message
    > news:...
    > > I got this program:
    > >
    > > class building{
    > > ...
    > > };
    > >
    > > class school:public building{
    > > ...
    > >
    > > int getstudentnumber(int m);
    > > };
    > >
    > > class hospital:public 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?
     
    ding feng, Jul 4, 2003
    #2
    1. Advertising

  3. (ding feng) wrote in message news:<>...
    > "Howard" <> wrote in message news:<be2c68$>...
    > > "ding feng" <> wrote in message
    > > news:...
    > > > I got this program:
    > > >
    > > > class building{
    > > > ...
    > > > };
    > > >
    > > > class school:public building{
    > > > ...
    > > >
    > > > int getstudentnumber(int m);
    > > > };
    > > >
    > > > class hospital:public 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
     
    Michiel Salters, Jul 4, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qwerty
    Replies:
    3
    Views:
    9,325
    Scott Allen
    Sep 30, 2004
  2. Magne Bergfjord
    Replies:
    1
    Views:
    471
  3. loveNUNO
    Replies:
    2
    Views:
    964
    loveNUNO
    Nov 20, 2003
  4. Rolf Magnus
    Replies:
    1
    Views:
    341
    ding feng
    Jul 4, 2003
  5. Robert Cohen
    Replies:
    3
    Views:
    301
    Andrew Durstewitz
    Jul 15, 2003
Loading...

Share This Page