123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|689|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> Concept/Design -> Virtual functions and multiple inheritamce - OOP question(s) and general rant

Tue, 23 Oct 2007, 05:26
Afr0
Alright, so I've been playing a bit With C++.Net/C++ CLI. I just learned how to properly override virtual/abstract functions in derived classes, and then found out that C++.NET doesn't actually support multiple inheritance!

What the heck is the point of supporting virtual functions at all in a language that doesn't support multiple inheritance?!

From what I gather, it isn't possible to instantiate abstract classes anyhow (or classes that have 1 or more virtual functions, which is the same thing), so then why not just write the function in the derived class without putting an unneccessary abstract definition in the base class that you can't use anyway?

Does C++ support multiple inheritance, and if so, why doesn't C++.NET support it?

Also, is there something I have misunderstood here? Is there any way you can get an option of maybe calling the function in a base class from an instantiated derived class if you actually have a function implementation (as opposed to just an abstract function definition) in the base class?

Arg, I hate OOP sometimes!

-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Tue, 23 Oct 2007, 08:46
mindstorm8191
Arg, I hate OOP sometimes!


Yeah I agree with you there, actually. Sometimes I think the whole OOP thing is made to be much more complicated than need-be. All this polymorphism, multiple inheritance, virtual functions... what does it all mean??? People always try to explain it and it never makes any sense. Can someone show us some quality examples??

I don't usually try to bother with OOP features. If I can find a way to do it an easier way, why not do that?

-=-=-
Vesuvius web game
Tue, 23 Oct 2007, 12:47
Afr0
Hehe, actually it's not that complex mindstorm! You see, a virtual function is like a function definition that exists for the sole purpose of being overridden in a derived class.

So let's say that you have 2 classes that are based on the same base class. This base class contains the following virtual function:

void virtual Eat();

If you have a language that actually supports multiple inheritance, that means that you can have as many versions of this function as you want! Say you have a class to represent a dog, and a class to represent a cow. A cow has four stomachs, so it digests it's food differently than a dog, which means it'd make sense to write a different version of it for the cow, and another one for the dog.

The end result would be something like



I'm just wondering what is the point of virtual functions in a language that doesn't support multiple inheritance, and also if there's something I've misunderstood...


-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Tue, 23 Oct 2007, 14:42
Nolan
Your code doesn't represent multiple inheritance, though. Both animals are extending one type, not the other way around. Multiple inheritance would be if you had two classes (say Shinythings and Timepieces), and one class, ( a wristwatch, in this example), extended from both.

Of course, perhaps I'm just not understanding what you're saying.

-=-=-
nolandc.com
Tue, 23 Oct 2007, 15:28
JL235
I've not encountered virtual methods before, but it sounds similar to Java's abstract methods and interfaces. Java does not allow multiple inheritance, however you can have multiple interfaces. The methods of the interface are abstract and implemented in the sub-class. This allows the appearance of multiple inheritance, but is actually only inheriting fields and defined methods from one super class.

I use polymorphism and inheritance in pretty much everything I make. Currently I'm working on extra stuff for Llama where you can create a World class that stores Entities and then processes and draws them in turn. In reality what the Entity actually is, is defined by the sub-class of Entity. It could be a Bullet, Tank, Player, Ball or Baton in Pong. However the World class will treat them exactly the same.
Tue, 23 Oct 2007, 15:49
Scherererer
Multiple inheritance was taken out due to massive code logic problems which often times occured because of it. C++ was a pretty broad language, C++.Net is more strict, and C# is even more so. Now, classes can only inherit one other class, but as many interfaces as you want. Code is written the same way though; so it would end up looking like this:


It should be noted, that all methods defined by an interface MUST be implemented in the class, unless it is abstract. Also, you can typecast items as their interfaces, so you could have a collection of Dog, Animal, IRun, IPiss, or whatever else, and all of them could contain something of type Dog.

-=-=-
YouTube Twitter
Computer Science Series: Logic (pt1) (part 2) (part 3) 2's Complement Mathematics: Basic Differential Calculus
Tue, 23 Oct 2007, 16:57
oscar
There are ways around multiple inheritance.. the chief being a slight change in the way you code... in essence whatinstinct said ^_^
Thu, 25 Oct 2007, 10:41
Afr0
Thanks guys! That made me realize that I've misunderstood the concept of multiple inheritance.
C++.NET doesn't actually need multiple inheritance because I can define virtual functions in one class and have two classes inherit from it, and write a separate version of the virtual function for each class. =)
If I ever end up needing multiple inheritance, I'll look more closely into what Instinct said though. Nolan's example of how to 'build' a wristwatch is actually very interesting... hm...

Edit: What are you on about in regards to 'massive code logic problems' exactly, Instinct?

-=-=-
Afr0 Games

Project Dollhouse on Github - Please fork!
Fri, 26 Oct 2007, 17:19
Scherererer
well, take this scenario, you have two classes you wish to inherit from, ClassA and ClassB. Both of these classes have methods with the same name and arguments, "public void method(int arg1)". Additionally, both of these methods have separate implementations. Once your class inherits these methods, there will be an obvious conflict.

However, if we make a more strict implementation such as with the case in the managed languages (C++.NET, C#, Java), there is no chance of this kind of conflict occurring, because interfaces are not permitted to have any implementation of its methods. However, one would still get the benefit of being able to typecast the classes which implement said interfaces.

-=-=-
YouTube Twitter
Computer Science Series: Logic (pt1) (part 2) (part 3) 2's Complement Mathematics: Basic Differential Calculus