Tuesday, July 14, 2009

C++ Inheritance?

Hi guys,


Can you please elaborate the statement below.. it's about C++ Inheritance...





"Including a base-class member function with a different signature in the derived class hides the base-class version of the function. Attempts to call the base-class version through the public interface of a derived-class object result in compilation errors."





Thanks guys.

C++ Inheritance?
Okie i am into JAVA so i would call it





*Method overriding*





Method(Function) overriding, in object oriented programming, is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses. The implementation in the subclass overrides (replaces) the implementation in the superclass.





The new method definition must have the same method signature (i.e., method name and parameters) and return type. Only parameter types and return type are chosen as criteria for matching method signature. So if a subclass has its method parameters as final it doesn’t really matter for method overriding scenarios as it still holds true. The new method definition cannot narrow the accessibility of the method, but it can widen it. The new method definition can only specify all or none, or a subset of the exception classes (including their subclasses) specified in the throws clause of the overridden method in the super class





A static method cannot be overridden to be non-static instance method as shown in the overridden method declaration of method9. A static method is class-specific and not part of any object, while overriding methods are invoked on behalf of objects of the subclass.





A subclass cannot override fields of the superclass, but it can hide them. Code in the subclass can use the keyword super to access members, including hidden fields.





The following distinction between invoking instance methods on an object and accessing fields of an object must be noted. When an instance method is invoked on an object using a reference, it is the class of the current object denoted by the reference, not the type of the reference, that determines which method implementation will be executed. When a field of an object is accessed using a reference, it is the type of the reference, not the class of the current object denoted by the reference, that determines which field will actually be accessed. This is demonstrated in the above program.











Sorry i dint sticked to C++....yet i am sure Concept is almost the same.





hope this helps


Cheers:)
Reply:Not too sure about C++ but this is the situation with C#.





The compiler gives a "'AnswersDerivative.SampleMethod()' hides inherited member 'Answers.SampleMethod()'. Use the new keyword if hiding was intended." warning with the following code. But the derived class will implicitly hide the base method, however there is no problem with the method with the alternate signature because it doesn't match anything in the base class





public class Answers


{


public Answers()


{


}





public void SampleMethod()


{


}





}





public class AnswersDerivative : Answers


{


public /* new */ void SampleMethod()


{


}





public void SampleMethod( int AlternateSignature )


{


}





}
Reply:hey this is called function overriding


u have to use VIRTUAL keyword before the base class's function, otherwise if u make an object of derived class %26amp; call its' function, then even base class's function will be called.





class base


{


public:


virtual void show()


{cout%26lt;%26lt;"Base Class";}


}





class derived:base


{


public:


void show()


{cout%26lt;%26lt;"Derived Class";}


}





void main()


{


base obj1;


obj1.show() // Base Class function will be called


derived obj2;


obj2.show() // Base class function will be called if u don't put VIRTUAL keyword before the function defined in base class


}


No comments:

Post a Comment