4

I hope this question is not too silly, but what is the most basic class in standard C++? object? Object?

class MyObject : public object{  ...

and I get "Expected class-name before token{"

Is there any map, diagram or image that shows standard c++ classes inheritance? Something like this but for C++ ?

nacho4d
  • 43,720
  • 45
  • 157
  • 240
  • 20
    Get a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) before you hurt yourself. – GManNickG Jul 26 '10 at 06:57
  • 11
    "I am quite fluent in C++". I'm sorry to be fairly blunt but the content of your question indicates otherwise. GMan's advice is sound; you need to cover some of the basics of C++ with a good book and/or mentor. – CB Bailey Jul 26 '10 at 07:14
  • Indeed. Fluency is unlikely, if you're under the impression that C++ has a base-class. – Jonathan Sterling Apr 15 '11 at 22:21

5 Answers5

14

There is no most basic class in C++ i.e. there is no common base class for all the classes.

Naveen
  • 74,600
  • 47
  • 176
  • 233
10

There is no fundamental object type in C++, unlike in e.g. Java.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
8

In Cocoa, the NSObject class is fundamental to the framework but not to the Objective-C language itself. In Objective-C, it is possible to create a root class by not deriving from anything (but in order to make it work you'll probably have to hack your way through runtime calls).

Similarly, some C++-based frameworks may define a root class that all other classes in that framework derive from, but it is specific to the framework, not the language.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • Though, I will add that `NSObject` actually is fundamental to Apple's *implementation* of the Objective-C language (but not in the GNUstep runtime). This means that you can't create your own base class without reimplementing several `NSObject` methods which the runtime will be expecting. – Jonathan Sterling Apr 15 '11 at 22:22
3

This is the simplest most basic class you can compile:

class Null
{
};
sashang
  • 11,704
  • 6
  • 44
  • 58
3

Inheritance diagram for IOstream library is here. STL is a template library and doesn't use OOP.

user401947
  • 850
  • 10
  • 18
  • 9
    Sure it does, it has classes and polymorphism. – GManNickG Jul 26 '10 at 07:09
  • Actually STL _does_ use OOP - heavily. It uses templates _on top of_ OOP, but it does use OOP. – utnapistim Jul 26 '10 at 09:13
  • No, STL doesn't use OOP. Don't confuse Object Based Programmings with Object Oriented Programming. Yes, STL containers are objects. But you won't find any virtual functions to overwrite. Containers don't have virtual destructors, so that you can't use them as polymorphic base classes. – user401947 Jul 26 '10 at 10:59
  • 2
    @rn141: I think this may be a dichotomy that you've invented! The inability to safely inherit doesn't mean that you don't have OOP. At any rate, the innards of STL uses plenty of inheritance (`reverse_iterator` inherits from `iterator`, and so on). – Oliver Charlesworth Jul 26 '10 at 11:20
  • Everyone defines OOP differently. I think the person who started this question was looking for Java style inheritance/OOP hierarchy for C++ standard library classes. In that sense, STL isn't OOP, no matter what the innards of STL uses. Unlike typical Java library, STL provides us with models which implement certain concepts. We don't care what inherites what. Unlike typical Java library, STL isn't built around IS A relationship. – user401947 Jul 26 '10 at 11:46
  • @rn141: I just gave you an example of an "is a" that is exposed in the interface to STL! There are plenty of others. I'm not sure by what measure this "isn't OOP"! – Oliver Charlesworth Jul 26 '10 at 11:56
  • It is very different from Java, where everything is virtual and you can overwrite pretty much every function. I have never seen any code which uses reverse_iterator as ISA iterator. If you implement STL without inheriting reverse_iterator from iterator, 99.9% of existing C++ code will work fine. STL classes aren't designed to be used polymorphically. Polymorphism is a central figure in OOP and it is absent from STL. – user401947 Jul 26 '10 at 12:13
  • Except that it's defined as such in the C++ standard. Any code can be rewritten to not use polymorphism; that doesn't make it non-OO. Java isn't the definition of OOP! – Oliver Charlesworth Jul 26 '10 at 12:18
  • You are refuting something I didn't state. Open any OOP book and you will see polymorphism, virtual functions, interfaces, abstract base classes, etc everywhere. Non of it really matters to the STL users. Even IOstream library is not used in polymorphic fashion. Look into Gang of Four book (the modern day OPP Bible for all practical purposes) and tell me how it is related to STL? The original question is asking about granddaddy of all C++ classes. Typical OOP approach to building libraries. There is no such thing in STL. – user401947 Jul 26 '10 at 12:40
  • 2
    We'll have to agree to disgaree, because you are arguing a very strange point; that STL isn't OO because it doesn't use certain features. – Oliver Charlesworth Jul 26 '10 at 12:52
  • 1
    I look from the user's point of view. STL is called "template library" for a reason. STL is an example of generic programming, not OOP programming. How STL is implemented isn't that important to the users. In fact, no STL implementation uses OOP design patterns for its internals. When using STL we care about concepts such as default constructable, copy constructable, assignable, comparable, etc. OOP is about interfaces and virtual functions. Alex Stepanov, the creator of STL, doesn't consider it as OOP. Read Stepanov's interview, if you care. – user401947 Jul 26 '10 at 13:25
  • 1
    @m141: "Even IOstream library is not used in polymorphic fashion." -- Yes it is. Any time you implement `operator<<` or `operator>>` and take `iostream` as the first parameter, you're invoking static polymorhism. Just because the interface doesn't expose `virtual` functions doesn't mean it's not polymorphic. – greyfade Jul 26 '10 at 16:18
  • 1
    @rn141: `streambuf` completely depends on virtual functions and inheritance. Don't commit the No True Scotsman fallacy. – GManNickG Jul 26 '10 at 19:48
  • There is not much OOP form the user's perspective. We are not talking about the inner workings of IOstream, right? How operator<< is implemented is not user's concern. Typical OOP library/framework provides users with interfaces and base classes. One needs to inherited/overwrite virtual functions to plug his own classes into the framework. That is OOP and it is not how 99.9% of users use IOstream. To them IOstream is a library which takes care of low level stuff. Every IOstream object is pretty much unique. Compare it to ACE or typical Java library. See the difference? – user401947 Jul 26 '10 at 21:16
  • @rn141 your understanding of what an interface is seems to be limited to java. "An interface is a common boundary or interconnection between systems" (taken from dictionary.reference.com). Template classes require you to implement a number of methods to use them, the interface is implicit. Which is good since primitive types can't "inherit" and could not be used with those templates - a big problem of java generics, they can't deal with primitive types. Back to the point the interface is there, it just isn't a java interface. – josefx Jul 27 '10 at 08:55
  • Pounding a straw man? The original question asked if there is a common base class for all C++ classes. Clearly, the person comes from Java and he wants to know if there is something similar in C++. From the context it is perfectly clear what I mean by interface and why it is pertinent to the original question. Please don't pretend that your definition of interface is carved in stone and everyone goes by it. Basic notions such as interface, object, etc are hard to define outside the context they are used. – user401947 Jul 27 '10 at 10:07
  • 1
    @rn141 I have been responding to your claim that the stl does not use Object oriented programming. If your definition of OOP is limited virtual inheritance then you may be right, but in my opinion there is more to OOP. – josefx Jul 27 '10 at 10:52
  • I understand 1st and 2 comments. After that I made it clear that I was looking from the user's perspective. Alex Stepanov, the creator of STL, doesn't consider it OOP. To the user even IOstream isn't OOP, unless he wants to do some low level stuff. Widely sited OOP book is "Design patterns" by Gamma et. al. It is often referred as Gang of Four and considered a bible by many OOP programmers. But non of Gang of Four really matters to STL. Most of C++ programmers learn how use IOstream long before they start bothering with Gang of Four. To them IOstream isn't about OOP as well. – user401947 Jul 27 '10 at 21:37