473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Accessing the base class

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

Company.Customers(0).Products(0).ShowCompanyName

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

Cheers, Robby

Jul 21 '05 #1
4 1301
Jim
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::ShowCompanyName( 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" <Ro***@Here.Now>
wrote:
How can access properties of an object lower down in the
hierarchy of my class structure? For example;

Company.Customers(0).Products(0).ShowCompanyNam e

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

Cheers, Robby


Jul 21 '05 #2
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?
-----Original Message-----
I'm not quite sure what you're trying to do here, but let me see if Ihave 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 classcompany, 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 themost-derrived versions of the data members or member functions youcall. You can override this with the scope resolution operator (::).That is what it's for. Consider this:

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

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

If products inherits from company, but you do not define a name memberfor the product class, then the above example would still work,because the name defined for company would still be the most derrivedone in products. If products also has a name member, then you wouldsimply use the proper class and the scope resolution operator to getthe 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 somethinghere with C# and inheritance, but if I'm wrong, please re-post andI'll see if I can help you out. :) Good luck!

JIM
On Thu, 6 Nov 2003 00:13:20 -0800, "Robby White" <Ro***@Here.Now>wrote:
How can access properties of an object lower down in thehierarchy of my class structure? For example;

Company.Customers(0).Products(0).ShowCompanyNa me

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

Cheers, Robby


.

Jul 21 '05 #3
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.

Jul 21 '05 #4
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.ShowCompanyName wouldn't
really apply to company (unless it is a different property with the same
name).

-mike
MVP

"Robby White" <Ro***@Here.Now> wrote in message
news:07****************************@phx.gbl...
How can access properties of an object lower down in the
hierarchy of my class structure? For example;

Company.Customers(0).Products(0).ShowCompanyName

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

Cheers, Robby

Jul 21 '05 #5

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

Similar topics

6
by: Abhijit Deshpande | last post by:
Is there any elegant way to acheive following: class Base { public: Base() {} virtual ~Base() {} virtual void Method() { cout << "Base::Method called"; return; } };
2
by: Simon White | last post by:
Hi, I have code that looks legal to me and has compiled for years and on latest VC, etc compilers. Just recently gcc made a change to their compiler (3.4) that broke it. I don't have access to...
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
4
by: Hadi | last post by:
Hello, Say I have three classes inheritance A <-- B <-- C And in A I have the Create method: class A { public override bool Create() {
5
by: Clive Dixon | last post by:
Is it possible to access an indexer of a base class with identical signature, e.g. class Class1 { public object this { get { // ...
5
by: Siva | last post by:
Hello I have a dropdownlist inside the gridview as a template column defined as follows: <asp:TemplateField HeaderText="Choose Location"> <ItemTemplate> <asp:DropDownList ID="ddlChooseLoc"...
2
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh...
4
by: Angus | last post by:
I have a base class which has a function GetThingID which returns a integer value. The ctor of the base class sets the value. In my derived class I want to use this function GetThingID. I have...
6
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
1
by: nsphelt | last post by:
I am wondering if it is possible to access the underlying property of a base class within a derived that has overridden that property. I am using VB.NET and when I use the MyBase keyword to access...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.