C++ is viewed as a superset of C, and thus offers backward compatibility with this language. This reliance on C provides important benefits:
• Reuse of legacy C code in new C++ programs
• Efficiency
• Platform neutrality
• Relatively quick migration from C to C++
Yet it also incurs certain complexities and ailments such as manual memory management, pointers, unchecked array bounds, and a cryptic declarator syntax, as described in the following sections.
As opposed to many other programming languages, C++ doesn't have versions. Rather, it has an International ANSI/ISO Standard, ratified in 1998, that defines the core language, its standard libraries, and implementation requirements. The C++ Standard is treated as a skeleton on which vendors might add their own platform-specific extensions, mostly by means of code libraries. However, it's possible to develop large-scale applications using pure standard C++, thereby ensuring code portability and easier maintenance.
The primary reason for selecting C++ is its support of object-oriented programming. Yet even as a procedural programming language, C++ is considered an improvement over ANSI C in several aspects. C programmers who prefer for various reasons not to switch to object-oriented programming can still benefit from the migration to C++ because of its tighter type-safety, strongly typed pointers, improved memory management, and many other features that make a programmer's life easier. Let's look at some of these improvements more closely:
• Improved memory management. In C, you have to call library functions to allocate storage dynamically and release it afterwards. But C++ treats dynamically allocated objects as first-class citizens: it uses the keywords new and delete to allocate and deallocate objects dynamically.
• User-defined types are treated as built-in types. For example, a struct or a union's name can be used directly in declarations and definitions just as a built-in type:
• struct Date
• {
• int day;
• int month;
• int year;
• };
•
• Date d; //In C, 'struct' is required before Date
void func(Date *pdate); //ditto
• Pass-by-reference. C has two types of argument passing: by address and by value. C++ defines a third argument-passing mechanism: passing by reference. When you pass an argument by reference, the callee gets an alias of the original object and can modify it. In fact, references are rather similar to pointers in their semantics; they're efficient because the callee doesn't get a copy of the original variable, but rather a handle that's bound to the original object. Syntactically, however, references look like variables that are passed by value. Here's an example:
• Date date;
• void func(Date &date_ref); //func takes Date by reference
func(date); // date is passed by reference, not by value
• Default argument values. C++ allows you to declare functions that take default argument values. When the function call doesn't provide such an argument, the compiler automatically inserts its respective default value into the function call. For example:
• void authorize(const string & username, bool log=true);
authorize(user); // equivalent to: authorize(user, true);
In the function call above, the programmer didn't provide the second argument. Because this argument has a default value, the compiler silently inserted true as a second argument.
• Mandatory function prototypes. In classic C, functions could be called without being previously declared. In C++, you must either declare or define a function before calling it. This way, the compiler can check the type and number of each argument. Without mandatory prototypes, passing arguments by reference or using default argument values wouldn't be possible because the compiler must replace the arguments with their references or add the default values as specified in the prototype.
A well-formed C++ program must contain a main() function and a pair of matching braces:
int main()
{}
Though perfectly valid, this program doesn't really do anything. To get a taste of C++, let's look at a more famous example:
#include
int main()
{
std::cout<<"hello world!"<
If you're a C programmer with little or no prior experience in C++, the code snippet above might shock you. It's entirely different from the equivalent C program:
#include
int main()
{
printf("hello world!\n");
}
Let's take a closer look at the C++ example. The first line is a preprocessor directive that #includes the standard
#include
std::cout<<"hello world!"<
• std::cout is the qualified name of the standard output stream object, cout. This object is automatically created whenever you #include
• The overloaded insertion operator << comes next. It takes an argument and passes it on to cout. In this case, the argument is a literal string that we want to print on the screen.
• The second argument, std::endl, is a manipulator that appends the newline character after the string and forces buffer flushing.
As trivial as this program seems, it exposes some of the most important features of C++. For starters, C++ is an object-oriented language. Therefore, it uses objects rather than functions to perform I/O. Secondly, the standard libraries of C++, including
seja o primeiro a comentar!