Your favourite thing about C++

Pages: 123
I found something I'm not fond of in Java, that I enjoy about C++. How inheritance is handled (in some cases)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class baz {
public:
    void func1() { /*Do something */ }
    void func2() { /*Do something else */ }
}

class bar {
public:
    void func3() { /* Derp */ }
    void func4() { /* Herp */ }
}

class foo : bar , baz {
public:
    void callFunc(); //calls all the functions inherited     
}

class foo2 : baz {
public:
    void callFunc(); // same as above
}


Basically here's what it breaks down to: You have a total of 4 classes, two parent, and two children. One child (foo) needs the methods defined by both parents, while the other only needs the one parent.
In java this would be a good time for an interface, BUT say you have more child classes than this. An interface (or abstract class) wouldn't be efficient cause it produces repetitive code. So the solution in java would be something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class baz {
public:
    void func1() { /*Do something */ }
    void func2() { /*Do something else */ }
}

class bar : baz {
public:
    void func3() { /* Derp */ }
    void func4() { /* Herp */ }
}

class foo : bar {
public:
    void callFunc(); //calls all the functions inherited     
}

class foo2 : bar {
public:
    void callFunc(); // same as above
}


But now foo2 has more functions than in needs to have, and it's scope is polluted. While this doesnt produce anything measurably detrimental (AFAIK), it isn't clean or concise and kind of goes against OOP principles (maybe?) I could be wrong on that last bit.

This a time when C++'s inheritance is better than Java's (IMHO). And don't even bring up the whole diamond problem, because in this example that doesnt matter. You are essantially making a non abstract interface in c++. there are no collisions.


edit: Yes I know my second code block is still c++
Last edited on
Well, this is nice, however why compare always to Java? Java is a simple language for average Joe programmer. They didn't know how to fix some problematic features of C++, so they left them out (and I admit sometimes it is a bad thing). C++ multiple inheritance is a nice thing in simple cases, but can get very nasty for more complex hierarchies.

You can code your multiple inheritance example in Jython, JRuby or Scala, and still have it run on JVM together with the rest of the Java code. So the problem of lack of multiple inheritance in Java has been completely solved. Your example translates directly into Scala. But if you still want to stick with pure Java, you can use composition and interfaces instead - still without any code redundancy.
Last edited on
I personally don't always compare to java, but I know as a whole this forum does.

Personally I used java as an example because I realized this when my Java professor wrote a class that inherited from one class, then inherited 6 other classes from that one. The amount of bloat in each child class was unreal.

As an aside: I would only use pure Java if i was restricted to it by something. C++ is my primary weapon of choice for most minor applications , Java I use for school work (as I'm required to) Scala I use when I want to use the JVM and have no restrictions, scripting for quick development, and something like haskell or scheme or lisp for experimenting with ideas and algorithms.

edit: You know, for the past year I've been telling people I've been a software junkie for about 10 years, since I was 8-9. While I did write my first program in BASIC then, I hadn't touched a compiler/interpreter until my 2nd year of highschool. Since then I've learned more then anyone I know at a subject being self taught in such a short amount of time. I found in programming my passion, obsession, and my talent.

edit edit: I personally love Google's experimental language, it introduced a lot of things that most OOP or Procedural languages lacked. Especially ones with it's routes in C. For instance, multiple returns from one function.
Last edited on
Google has a lot of exciting projects going on and they do not neglect their sales strategy direction. For every Open Source initiatives they started, somewhere along the work-flow they will find a way where they stand to earn revenue.

They understand passionate developers like choices and in most cases *free* development tools and platforms. So they provide and host them. In return they think of a way to get some monies from the finished products those developers develop. It is like borrow "resources" to earn monies but first they invest on "infrastructure" to attract developers.

Sun Microsystems used to have a lot of exciting projects going on too but I feel they tend to neglect on their sales strategy leading to dismal sales year after year until they are taken over. A sad story I would say.

Topic archived. No new replies allowed.
Pages: 123