Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / August 2007

Tip: Looking for answers? Try searching our database.

Accessing base class method using derived class object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
srini4vasan@gmail.com - 26 Jul 2007 12:54 GMT
#include<iostream>
using namespace std;

class A
{
public:
    void display()
    {
        cout<<"A display"<<endl;
    }

};

class B : public A
{
public:
    void display()
    {
        cout<<" B display "<<endl;
    }
};

int main()
{
    B b1;
    b1.display();

    return 0;
}

In this program can you please tell me how can i access base class
method ie.,display() using derived class object.ie.,b1.

A single line has to be included in this main funtion.
Phill W. - 27 Jul 2007 12:53 GMT
> class A
> {
[quoted text clipped - 27 lines]
>
> A single line has to be included in this main funtion.

Sounds decidedly home-work-ish to me, but what the hey...

/If/ B.display() shadows A.display(), i.e. there's no overriding
involved - my C++ is a /little/ rusty  ;-) - then you can do this
directly.
Simply tell ("up-cast") 'B' to behave as if it were an 'A'.

   ((A)b1).display();

However, if B.display() /overrides/ A.display() - don't think this is
the case, but just for completeness - then you're stuck.
Unless you expose an alternative method on 'B' that calls the base
implementation in 'A', then you can't access the base implementation in
A; and that's as it should be.

HTH,
   Phill  W.
Ben Voigt [C++ MVP] - 02 Aug 2007 18:36 GMT
>> class A
>> {
[quoted text clipped - 39 lines]
> However, if B.display() /overrides/ A.display() - don't think this is the
> case, but just for completeness - then you're stuck.

There's no "virtual", so no dynamic dispatch.  Calling through a reference
to A will call the base implementation.

> Unless you expose an alternative method on 'B' that calls the base
> implementation in 'A', then you can't access the base implementation in A;
> and that's as it should be.
>
> HTH,
>    Phill  W.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.