Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following class as

C#
public abstract class BaseClass
   {
       public virtual void VirtualMethod()
       {
           Console.WriteLine("NewBaseClass:VirtualMethod");
       }
   }

   public class DerivedClass : BaseClass
   {
       public override void VirtualMethod()
       {
           Console.WriteLine("NewClassA:VirtualMethod");

       }
   }


Now i want to call base class method by creating object of DerivedClass using base keyword
Please help
Posted
Updated 4-Mar-13 1:31am
v2

In your example (C++):

C++
// Create an instance of DerivedClass
DerivedClass *dc = new DerivedClass();

// BTW this will work as well:
//BaseClass *dc = new DerivedClass();

// Call the method of base class
dc->BaseClass::VirtualMethod();

// Let' clean
delete dc;


This example shall give an output like:

>NewBaseClass:VirtualMethod
 
Share this answer
 
v4
 
Share this answer
 
Comments
Leo Chapiro 4-Mar-13 8:48am    
' Give a man a fish and you feed him for a day, teach a man to fish and you feed him for life' , huh?
;)
Maciej Los 4-Mar-13 9:09am    
That's my motto!
And your is?
Leo Chapiro 4-Mar-13 9:25am    
I'm too old to have a motto - perhaps "Too old to die young" if any ;)
Maciej Los 4-Mar-13 9:44am    
;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900