HighTechTalks DotNet Forums  

Accessing base class method using derived class object

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss Accessing base class method using derived class object in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
srini4vasan@gmail.com
 
Posts: n/a

Default Accessing base class method using derived class object - 07-26-2007 , 07:54 AM






#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.


Reply With Quote
  #2  
Old   
Phill W.
 
Posts: n/a

Default Re: Accessing base class method using derived class object - 07-27-2007 , 07:53 AM






srini4vasan (AT) gmail (DOT) com wrote:
Quote:
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.
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.


Reply With Quote
  #3  
Old   
Ben Voigt [C++ MVP]
 
Posts: n/a

Default Re: Accessing base class method using derived class object - 08-02-2007 , 01:36 PM




"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-k> wrote

Quote:
srini4vasan (AT) gmail (DOT) com wrote:
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.

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.
There's no "virtual", so no dynamic dispatch. Calling through a reference
to A will call the base implementation.

Quote:
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.



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.