C++ (programming language):
What are some features, functions, or techniques that programmers should avoid in C++?
General Features of C++
C++ is syntactically and semantically complex (eg compared with Eiffel).
This is not necessarily a good thing
For example, there is explicit pointer arithmetic (as in C)
Making the many features work together effectively is a challenge.
Some specific OO features of C++
Classes
Classes can contain both data and operations
Encapsulation
Data and functions in classes can be hidden
Composition
A class can be composed of other classes
Inheritance (class derivation)
New classes can be obtained from other classes
Polymorphism
C++ allows all forms of polymorphism:
coercion
overloading
pure polymorphism
parametric polymorphism: templates, (genericity in Eiffel)
Specific language features
Comments
Both C style comments /* ..... */ are allowed as well as comments starting with //
Can use /* ...*/ for multiple line comments
// can be used as in line comments
long numTribbles; // will increase!
They can also be used as single line comments
// This is a comment
// Every comment line must start
// with a //
/*
// C++ comments can be contained
// inside C style comments
*/
Programming recommendation
Always use // comments
This allows sections of code to be commented out with /* ... */
Introduction to iostream
Input and output are not part of the C++ language.
They are supported by the iostream library.
This predefines three streams for I/O:
cin - standard input (terminal)
cout - standard output (screen)
cerr - standard error
The << (put to) operator directs output to a stream
The >> (get from) operator retrieves data from a stream
A stream is a sequence of characters.-------------------------------------------------------------------------------------------------------------------------------------
C++ is a powerful, highly flexible, and adaptable programming language that allows software engineers to organize and process information quickly and effectively
Readability Advantages of C over C++According to : Carlos Oliveira
C++ is a language that was created to improve some of the features and use cases of the C language, in order to make it
easier to use for application development (instead of system programming, the main area dominated by C). One of the
perceived problems in C is the lack of support for object oriented features that are so common on the development of
desktop applications. For example, C lacks the concept of a type that encapsulates code and data. While it is possible to
program in an object oriented style in C, it is somewhat cumbersome to use such a style.
In order to make C easier to use, C++ designers added a number of features that would be more appealing to programmers.
Inheritance was one of them, used to support object oriented features. Overridden methods were also added to improve
object oriented programs.
C++ also added some features that can be considered syntactic sugar for already supported operations. For example,
operators can be overloaded, so that many math operations, and even punctuation such as “,” can be overloaded by
programmers. Normal functions can also be overloaded, by providing versions that differ on the number and type of
arguments.
Later on, C++ added templates to fix a problem with the language’s type system. With templates, it is possible to
instantiate a class based on one or more types. This allows for the creation of containers, such as the ones found in the STL.
Vectors, maps, and multimaps can all be created by adding a type as a parameter for template instantiation
Conclusion
While C++ provides a lot of helpful features for application programming, these features have a cost. It becomes
increasingly difficult to understand what is going on in each line of code, unless the developers are very disciplined.
This also shows a little of the myth surrounding the idea that C++ is better than C for large code bases. While C++ has
some features that might be helpful, in general the bigger the C++ code base the harder it is to understand due to these
issues.
This is true even for automated tools. It is very difficult to create automatic tools to parse and change C++ (for example, to
perform refactoring). In comparison, C is much easier to understand, and one can do a lot with tools such as ctags. It is not a
surprise that large scale projects such as the Linux kernel use C without any problem.
Recommended