answersLogoWhite

0

If the base class method is non-private, the derived class can call the base class method implicitly. However, if the derived class overrides or overloads the method, the base class method must be called explicitly. The following demonstrates explicit calls to base class methods:

#include <iostream>

using namespace std;

class Base

{

public:

Base(){}

void Foo(){ cout << "Base::Foo" << endl; }

};

class Derived : public Base

{

public:

Derived(){}

void Foo(){ cout << "Derived::Foo" << endl; Base::Foo(); }

};

int main()

{

Derived derived;

derived.Foo();

derived.Base::Foo(); // Explicit call to base class method.

return(0);

}

Output:

Derived::Foo

Base::Foo

Base::Foo

User Avatar

Wiki User

12y ago

What else can I help you with?

Continue Learning about Engineering

How do you call a method from another class in Java?

If the method is static you can do this way: Classname.method() If the method is not static then you would have to instantiate the class that contains this method and then call it using that object. Classname obj = new Classname(); obj.method()


What is the main purpose in using a destructor?

Destructors are called automatically whenever an object falls from scope. Even if you don't actually declare a destructor, one is implied. The primary purpose of a destructor is to give the programmer one final chance to clean up an object's memory allocations before the object is destroyed forever. If an object has allocated memory associated with it (via member pointer variables), then that memory must be released before the object is destroyed, otherwise a memory leak is inevitable. Destructors are also important in class inheritance. When a derived object falls from scope, its immediate base class destructors are called, followed by their base class destructors. This is effectively the reverse of an object's construction, which always begins with the least-derived base classes, working up to the most-derived class, which is the object itself. Just as a derived class cannot be instantiated until all its base classes are constructed, a base class object cannot be destroyed until all its derived class objects are destroyed. All the destructors in a class hierarchy must be declared virtual to ensure the correct most-derived destructor is called whenever a base class reference falls from scope.


Why can't method override be used without inheritance?

Overriding a method means that you are replacing an existing or virtual method that has already been defined in the parent object class, so without using inheritance, there can be no existing method to override.


Which keyword is used to declare a class?

There is no keyword for it. You can use a variable of 1 class in another only id the other class is derived from the 1st class.Although you can use the variable of an object of a class in another class using object.variable


Why a static member method can access only static members in java?

Because, the keyword static signifies the fact that the method or variable that is qualified using the static keyword is not attached to any object of the class. Therefore we cannot instantiate the class and use the object to reference to access it. The only option we have is to use the class name to directly access them

Related Questions

How do you invoke method by using object of the class?

If you have and object with method described within its class you can use dot access operator, for instance:myObject.DoSomething();


What is method call in java?

Calling a method in Java is when you run code associated with a specific class, using the name of an instance object of a class, followed by the dot operator, followed by the name of the method, followed by the arguments of the method, enclosed in parentheses.


How do you call a method from another class in Java?

If the method is static you can do this way: Classname.method() If the method is not static then you would have to instantiate the class that contains this method and then call it using that object. Classname obj = new Classname(); obj.method()


What is need of object in object oriented programming?

Object is an instant of the class, by using an object we can members of the class.


What is the main purpose in using a destructor?

Destructors are called automatically whenever an object falls from scope. Even if you don't actually declare a destructor, one is implied. The primary purpose of a destructor is to give the programmer one final chance to clean up an object's memory allocations before the object is destroyed forever. If an object has allocated memory associated with it (via member pointer variables), then that memory must be released before the object is destroyed, otherwise a memory leak is inevitable. Destructors are also important in class inheritance. When a derived object falls from scope, its immediate base class destructors are called, followed by their base class destructors. This is effectively the reverse of an object's construction, which always begins with the least-derived base classes, working up to the most-derived class, which is the object itself. Just as a derived class cannot be instantiated until all its base classes are constructed, a base class object cannot be destroyed until all its derived class objects are destroyed. All the destructors in a class hierarchy must be declared virtual to ensure the correct most-derived destructor is called whenever a base class reference falls from scope.


Why can't method override be used without inheritance?

Overriding a method means that you are replacing an existing or virtual method that has already been defined in the parent object class, so without using inheritance, there can be no existing method to override.


Which keyword is used to declare a class?

There is no keyword for it. You can use a variable of 1 class in another only id the other class is derived from the 1st class.Although you can use the variable of an object of a class in another class using object.variable


Why a static member method can access only static members in java?

Because, the keyword static signifies the fact that the method or variable that is qualified using the static keyword is not attached to any object of the class. Therefore we cannot instantiate the class and use the object to reference to access it. The only option we have is to use the class name to directly access them


How much a base class members can access of derived class?

Ideally, none. Base classes should have no knowledge whatsoever of their derived classes and therefore should have no access to any members declared in the derived classes. However, derived class methods are accessible via virtual functions declared in the base class. The correct methods (overrides) are called via the virtual table, but the base class itself requires no specific knowledge of the derived class in order to call these methods. This makes sense since a base class cannot know in advance all the classes that may or may not derive from it in the future. If a method must be guaranteed to be implemented by a derived class, a pure-virtual method can be declared in the base class instead. This renders the base class abstract, meaning you cannot instantiate an object from the base class, only from a derivative that fully implements (or inherits) all the pure-virtual methods. Conversely, a derived class can access all the public and protected members of its base class.


What is the primary value in using virtual functions within C plus plus?

Virtual methods are member methods of a base class that can be overridden by derived classes. Classes that can act as base classes should contain all the functionality that is common to all their derived classes. However, the derived classes may need to alter that functionality in some way by overriding the methods in the base class. While this is fine when we actually hold pointers or references to a derived object, what happens when we hold pointers or references to the base class of a derived object? We cannot call the overridden methods explicitly unless we expose the runtime class of the derived object, which would incur an unacceptable processing overhead. Virtual methods allow us to get around this problem in a more elegant fashion. By declaring a method as virtual, calling the method implicitly on a base class will automatically and explicitly call the most-derived implementation in the class hierarchy. If no override exists, the base class method is called explicitly. While this mechanism incurs a memory penalty in creating the v-table (virtual table), the bulk of that cost is paid with the first virtual method declared. Subsequent methods add very little overhead, so it is not uncommon for programmers to declare all methods virtual. However, if there is at least one virtual method declared, the class destructor must also be declared virtual. Thereafter, you should only declare other methods to be virtual if there is an actual need to do so. After all, there's no point in consuming more memory than is actually required. Sometimes it is not possible for a base class to provide the implementation for a virtual method, In this case the method should be declared pure-virtual. The same rules apply as for virtual functions, however you can no longer instantiate objects from the base class itself -- it becomes an abstract class -- so you must derive from it and you must provide an implementation for all the pure-virtual methods (otherwise it becomes abstract itself). Abstract classes will generally have few, if any, member variables, and all member methods will generally be declared as pure-virtual. Abstract classes are primarily intended to provide a common interface to their derivatives. The base class can still provide default implementations for pure-virtual methods, however they must be explicitly called. In some cases, a derived class will simply augment the default implementation, rather than override it completely. Be aware that base classes that are common to derived classes that can be used by multiple inheritance classes will introduce ambiguities. The multiple inheritance class will inherit two or more instances of the common base class, and therefore must be called explicitly from the multiple inheritance class. However, it is possible to remove the ambiguity altogether, by declaring the common base class to be virtual in its immediate derivatives, in which case all derivatives share the same common base class when multiply inherited. Ideally, the common base class should have very little in the way of implementation, and few, if any, member variables.


What is abstraction method in java?

Abstraction in Java or Object oriented programming is a way to segregate implementation from interface and one of the five fundamentals along with Encapsulation, Inheritance, Polymorphism, Class and Object. Abstraction in Java is achieved by using interface and abstract class in Java.


Static keyword in java?

A Static method in Java is one that belongs to a class rather than an object of a class. Normal methods of a class can be invoked only by using an object of the class but a Static method can be invoked Directly. Example: public class A { ..... public static int getAge(){ .... } } public class B { ..... int age = A.getAge(); } In class B when we wanted the age value we directly called the method using the instance of the class instead of instantiating an object of the class. Tip: A static method can access only static variables. The reason is obvious. Something that is common to a class cannot refer to things that are specific to an object...