1

Is it true that for C++ to work similarly in terms of modern OOP as in Java, Ruby, Python, the function (or methods) must be declared virtual and if not, what "strange" behaviors may occur?

I think it is true that for Java, Ruby, Python, and possibly other OOP languages that are late comers such as PHP and Lua, and even Smalltalk and Objective-C, all methods are just what is known as "virtual functions"?

nonopolarity
  • 146,324
  • 131
  • 460
  • 740

3 Answers3

1

"Method" is an unfortunately overloaded term that can mean many things. There's a reason C++ prefers different terminology, and that's because not only does it do something different from other languages, but it intends to do something different from what other languages do.

In C++ you call a member function. i.e. you externally make a call to a function associated with an object. Whether that function is virtual or not is secondary; what matters is the intended ordering of your actions - you're reaching into the object's scope, and commanding it to take a specific action. It might be that the object can specialize the action, but if so, it warned you in advance that it would do this.

In Smalltalk and the languages that imitate it (Objective-C most closely), you send a message to an object. A message is constructed on your side of the call consisting of a task name (i.e. method selector), arguments, etc., and the packed up and sent to the object, for the object to deal with as it sees fit. Semantically, it's entirely the object's decision what to do upon receipt of the message - it can examine the task name and decide which implementation to apply dynamically, according to a user-implemented choice process, or even do nothing at all. The outside calling code doesn't get to say what the object will do, and certainly doesn't get any say in which procedure actually runs.

Some languages fall in the middle ground, e.g. Java is inspired by the latter, but doesn't give the user any way to specify unusual dynamic responses - for the sake of simplicity every message does result in a call, but which call is still hidden from the external code, because it's entirely the object's business. C++ was never built on this philosophy of messages in the first place, so it has a different default assumption about how its member functions should operate.

Community
  • 1
  • 1
Alex Celeste
  • 12,824
  • 10
  • 46
  • 89
0

It is true that (for example) in Java all methods are virtual by default. In C++ it is possible to overload a non-virtual function (as opposed to overriding a virtual function) in a subclass, leading to possible counter-intutive behaviour, when only the base function is actually executed via a pointer or reference to the base class (i.e. when polymorphic behavior would normally be expected).

Because C++ is a value-based (as opposed to reference-based) language, then even when a function has been declared as virtual, the well known object slicing problem can still arise: the superclass method is invoked when the type of a value object of a subclass is `cut down' to that of the base class (e.g. when the subclass is passed to a function which takes a base class argument by value).

For this reason, it is recommended to make all non-leaf classes abstract, something which is often achieved by providing a virtual destructor, even if such would otherwise be gratuitous.

Community
  • 1
  • 1
NietzscheanAI
  • 966
  • 6
  • 16
0

The thing is that C++ is like the great grand father. It has many features, which often requires huge code definition.

Consider an example:

class A
{
    virtual void fn() = 0;
};

class B: A
{
    void fn();
};


#include "a.hpp"
#include "b.hpp"
int main()
{
    A *a = new B();
    a->fn();
}

This would implement overriding in C++.

Note that virtual void fn()=0 makes the class A abstract, and a pointer to base class (A) is essential.

In Java, the process is even simpler

abstract class A
{
    abstract void fn();
}

class B extends A
{
    void fn() {
        //Some insane function :)
    }
}

public static void main(String[] args) {
    B ob = new B();
    ob.fn();   
}

Well, the effect is same; but the process is largely different. In short, C++ does have many features implemented in languages like Java, Ruby etc. but it is simply implemented using some (often complicated) techniques.

Regarding Php, since it is directly based on C++, there exists some syntax similarities between C++ and Php.

aliasm2k
  • 883
  • 6
  • 12