Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the OO concept interface not represented by a keyword in C++?

Tags:

c++

oop

Languages such as Java explicitly use the interface keyword to denote interfaces. Having used Java, the concept seems useful enough to me to justify a keyword to enforce the concept.

Of course one can use a pure virtual class and label it as an interface. However, this keyword seems to be so useful and differentiated from a pure virtual class as to be useful. Perhaps it is being included in C++ 0x?

like image 317
casualcoder Avatar asked Jan 26 '09 02:01

casualcoder


People also ask

Why OOPs concept is not used in C?

It is an object-driven language. C is a Procedural Oriented language. It does not support object-oriented programming (OOP) features such as polymorphism, encapsulation, and inheritance programming.

Is object-oriented programming possible in C?

Object-Oriented Programming in C Although the fundamental OOP concepts have been traditionally associated with object-oriented languages, such as Smalltalk, C++, or Java, you can implement them in almost any programming language including portable, standard-compliant C (ISO-C90 Standard).

What does oo mean in programming?

Object-oriented programming (OOP) is a style of programming characterized by the identification of classes of objects closely linked with the methods (functions) with which they are associated. It also includes ideas of inheritance of attributes and methods.

Where we should not use the concept of OOPs?

These include: design patterns, abstraction, encapsulation, modularity, polymorphism, and inheritance. When not to use OOP: Putting square pegs in round holes: Don't wrap everything in classes when they don't need to be. Sometimes there is no need and the extra overhead just makes your code slower and more complex.


3 Answers

Because C++ allows multiple inheritance, and because an interface is an abstract class which has all of it's members also abstract/virtual, C++ does not need it - a class can simply "extend" multiple other classes, any of which may be purely virtual (abstract).

Java and C#, on the other hand do not permit MI, since the designers of those languages felt that MI creates more problems than it solves. But it is still necessary for an object to "be" many things (the OOP is-a relationship), so interfaces provide a mechanism which allows an object to be many things, without inheriting multiple implementations - keeping the baby, but throwing out the bathwater.

like image 77
Lawrence Dol Avatar answered Oct 16 '22 09:10

Lawrence Dol


It's redundant, since interfaces are represented by having every class member be pure virtual (=0).

like image 17
Crashworks Avatar answered Oct 16 '22 08:10

Crashworks


Adding an "interface" keyword would add complexity to the implementation without adding any truly useful capability; it would duplicate existing functionality. As others have said, it's just a pure virtual class. Java and C# had to have 'interface' to get a piece of what C++ already had. Philosophically, C++ is designed to enable programmers to write good software, not to prevent programmers from writing bad software. In my experience, the hoopla against MI is way overblown. Idiots misused it, like they misuse everything, and instead of blaming the idiots for being idiots, people blamed the tool.

like image 7
Rob K Avatar answered Oct 16 '22 07:10

Rob K