The main difference I can think of is that C ++ is a lot more multi-paradigm language than C and C #. In C #, OOP is still a paradigm. This is the OOP language first of all, and if you are not doing OOP, the C # community will tell you that you are doing it wrong. (although C # has added good support for several bits of functional programming, but over the past few years).
In C ++ OOP, well, it is supported, and you can use it when you like it, but the whole problem is related to general programming. C ++ templates allow you to use a wide range of smart, reusable, and universal libraries and achieve many goals, such as old-fashioned OOP, but without large inheritance hierarchies and virtually no communication between classes. The standard library contains many examples of this.
In C ++, many C constructs, although legitimate, mostly avoid:
- Raw pointers (usually replaced by smart pointers such as
boost::shared_ptr or std::auto_ptr , or with links - Memory allocation in user code (usually should be included in a smart pointer or in a custom RAII object)
- Function pointers (usually replaced by functors, to increase security and performance)
- goto (often used in C to switch to cleansing code. Again, made unnecessary with RAII)
- preprocessor (almost never needed. Prefer templates instead)
Of course, there are exceptions to each of these points, but, as a rule, C ++ code, unlike C code, largely eliminates all of their use.
And more than in C #, classes are really workhorses doing a lot of hard work. In C #, a class is a little more than a few forests, a container in which all your methods are stored. Of course, he has a constructor, and he can implement Dispose (); but C ++ takes this a lot more, and you have:
- Constructor (as in C #, which initializes the class from scratch)
- Copy constructor (initializes the class as a copy of another object)
- Assignment operator (since classes are what C # will consider value types. And therefore, an assignment is not just a change to a link, but copying over the entire contents of an object defined by the user)
- Destructor
The destructor is probably the most important concept in C ++. This is vital for RAII, which is controlled by memory or other resources, because it is automatically called when an object is out of scope. This allows your classes to fulfill many guarantees that cannot be achieved in C or C #. For example, boost :: thread provides locked locks that are guaranteed to be released when you exit the scope, whether the function returns normally, an exception is thrown, or something else. Therefore, when using this library, the user does not need to worry about releasing locks or other resources. It just happens automatically as soon as you are done with them.
In a way, this gives you a lot more hooks to customize the behavior of your class. Unlike C #, you precisely control what happens when a simple assignment is executed. You control what happens when a class goes out of scope, when it is initialized from scratch, or as a copy of another object. This allows you to write the class correctly, which will be practically impossible to use incorrectly. (Nearly)
In addition, templates and metaprogramming templates are concepts you are likely to come across. They are very powerful tools, so make sure you are friends with them. :)