DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Thu, 26 Jul 2007 04:54:38 -0700,    posted on: microsoft.public.dotnet.framework.clr        back       

Thread Index
  1    unknown
          2    Phill W. p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-k
                 3    Ben Voigt [C++ MVP] am


Accessing base class method using derived class object   
#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.
Date:Thu, 26 Jul 2007 04:54:38 -0700   Author:  

Re: Accessing base class method using derived class object   
srini4vasan@gmail.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.
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.
Date:Fri, 27 Jul 2007 12:53:34 +0100   Author:  

Re: Accessing base class method using derived class object   
"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-k> wrote in message 
news:f8cmbu$lui$1@south.jnrs.ja.net...

> srini4vasan@gmail.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.


> 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. 
Date:Thu, 2 Aug 2007 12:36:04 -0500   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us