Accessing the base class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robby White

    #1

    Accessing the base class

    How can access properties of an object lower down in the
    hierarchy of my class structure? For example;

    Company.Custome rs(0).Products( 0).ShowCompanyN ame

    In the above Show method I want to access the Name
    property with the Company class.

    Cheers, Robby

  • Jim

    #2
    Re: Accessing the base class

    I'm not quite sure what you're trying to do here, but let me see if I
    have the basic idea first...

    class company
    {
    public:
    char name[20];
    ...
    };

    class customer : public company
    {
    public:
    char name[20]; // Overrides company::name
    ...
    void ShowCompanyName ( void); // Should display the name from class
    company, not customer
    };

    When you call the ShowCompany function for an object of type company
    (or any class that derrives from it), you will by default use the
    most-derrived versions of the data members or member functions you
    call. You can override this with the scope resolution operator (::).
    That is what it's for. Consider this:

    void company::ShowCo mpanyName( void)
    {
    cout << "Customer Name:\t" << customer::name << endl; // Use :: to
    override scope, showing the customer information instead.

    cout << "Company Name:\t" << name << endl; // By default, we use the
    most-derrived version of the member, in this case the company name.

    return;
    }

    If products inherits from company, but you do not define a name member
    for the product class, then the above example would still work,
    because the name defined for company would still be the most derrived
    one in products. If products also has a name member, then you would
    simply use the proper class and the scope resolution operator to get
    the company and customer names, and then omit it for the product name.

    I'm a C++ programmer, and I'm guessing that you're doing something
    here with C# and inheritance, but if I'm wrong, please re-post and
    I'll see if I can help you out. :) Good luck!

    JIM


    On Thu, 6 Nov 2003 00:13:20 -0800, "Robby White" <Robby@Here.Now >
    wrote:
    [color=blue]
    >How can access properties of an object lower down in the
    >hierarchy of my class structure? For example;
    >
    >Company.Custom ers(0).Products (0).ShowCompany Name
    >
    >In the above Show method I want to access the Name
    >property with the Company class.
    >
    >Cheers, Robby[/color]

    Comment

    • Robby White

      #3
      Re: Accessing the base class

      Sorry I should have explained better! Im not using
      inheritance as these are distinct classes in there own
      right. I think I have a workaround which basically
      declares a Parent property for each level?
      [color=blue]
      >-----Original Message-----
      >I'm not quite sure what you're trying to do here, but[/color]
      let me see if I[color=blue]
      >have the basic idea first...
      >
      >class company
      >{
      > public:
      > char name[20];
      > ...
      >};
      >
      >class customer : public company
      >{
      > public:
      > char name[20]; // Overrides company::name
      > ...
      > void ShowCompanyName ( void); // Should display the[/color]
      name from class[color=blue]
      >company, not customer
      >};
      >
      >When you call the ShowCompany function for an object of[/color]
      type company[color=blue]
      >(or any class that derrives from it), you will by[/color]
      default use the[color=blue]
      >most-derrived versions of the data members or member[/color]
      functions you[color=blue]
      >call. You can override this with the scope resolution[/color]
      operator (::).[color=blue]
      >That is what it's for. Consider this:
      >
      >void company::ShowCo mpanyName( void)
      >{
      > cout << "Customer Name:\t" << customer::name <<[/color]
      endl; // Use :: to[color=blue]
      >override scope, showing the customer information instead.
      >
      >cout << "Company Name:\t" << name << endl; // By[/color]
      default, we use the[color=blue]
      >most-derrived version of the member, in this case the[/color]
      company name.[color=blue]
      >
      >return;
      >}
      >
      >If products inherits from company, but you do not define[/color]
      a name member[color=blue]
      >for the product class, then the above example would[/color]
      still work,[color=blue]
      >because the name defined for company would still be the[/color]
      most derrived[color=blue]
      >one in products. If products also has a name member,[/color]
      then you would[color=blue]
      >simply use the proper class and the scope resolution[/color]
      operator to get[color=blue]
      >the company and customer names, and then omit it for the[/color]
      product name.[color=blue]
      >
      >I'm a C++ programmer, and I'm guessing that you're doing[/color]
      something[color=blue]
      >here with C# and inheritance, but if I'm wrong, please[/color]
      re-post and[color=blue]
      >I'll see if I can help you out. :) Good luck!
      >
      >JIM
      >
      >
      >On Thu, 6 Nov 2003 00:13:20 -0800, "Robby White"[/color]
      <Robby@Here.Now >[color=blue]
      >wrote:
      >[color=green]
      >>How can access properties of an object lower down in[/color][/color]
      the[color=blue][color=green]
      >>hierarchy of my class structure? For example;
      >>
      >>Company.Custo mers(0).Product s(0).ShowCompan yName
      >>
      >>In the above Show method I want to access the Name
      >>property with the Company class.
      >>
      >>Cheers, Robby[/color]
      >
      >.
      >[/color]

      Comment

      • Tian Min Huang

        #4
        Re: Accessing the base class

        Hi Robby,

        Thanks for your post. I believe it's a good idea to have a property
        pointing to parent object.

        Please feel free to let me know if there is any further I can help
        regarding this issue.

        Have a nice day!

        Regards,

        HuangTM
        Microsoft Online Partner Support
        MCSE/MCSD

        Get Secure! -- www.microsoft.com/security
        This posting is provided "as is" with no warranties and confers no rights.

        Comment

        • Michael Giagnocavo [MVP]

          #5
          Re: Accessing the base class

          If you expose a parent property or a specific "child" property, make very
          sure that it's going to have the intended use. If customers is a
          collection, and products is a collection, then ShowCompanyName might be
          different for each object, right? So Company.ShowCom panyName wouldn't
          really apply to company (unless it is a different property with the same
          name).

          -mike
          MVP

          "Robby White" <Robby@Here.Now > wrote in message
          news:076e01c3a4 3d$d6cc4f20$a60 1280a@phx.gbl.. .[color=blue]
          > How can access properties of an object lower down in the
          > hierarchy of my class structure? For example;
          >
          > Company.Custome rs(0).Products( 0).ShowCompanyN ame
          >
          > In the above Show method I want to access the Name
          > property with the Company class.
          >
          > Cheers, Robby
          >[/color]


          Comment

          Working...