Sei sulla pagina 1di 9

History of C++

The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was doing
work for his Ph.D. thesis. One of the languages Stroustrup had the opportunity to work with was a
language called Simula, which as the name implies is a language primarily designed for
simulations. The Simula 67 language - which was the variant that Stroustrup worked with - is
regarded as the first language to support the object-oriented programming paradigm. Stroustrup
found that this paradigm was very useful for software development, however the Simula language was
far too slow for practical use.

Shortly thereafter, he began work on "C with Classes", which as the name implies was meant to be a
superset of the C language. His goal was to add object-oriented programming into the C language,
which was and still is a language well-respected for its portability without sacrificing speed or low-level
functionality. His language included classes, basic inheritance, inlining, default function arguments,
and strong type checking in addition to all the features of the C language.

The first C with Classes compiler was called Cfront, which was derived from a C compiler called CPre.
It was a program designed to translate C with Classes code to ordinary C. A rather interesting point
worth noting is that Cfront was written mostly in C with Classes, making it a self-hosting compiler (a
compiler that can compile itself). Cfront would later be abandoned in 1993 after it became difficult to
integrate new features into it, namely C++ exceptions. Nonetheless, Cfront made a huge impact on
the implementations of future compilers and on the Unix operating system.

In 1983, the name of the language was changed from C with Classes to C++. The ++ operator in the
C language is an operator for incrementing a variable, which gives some insight into how Stroustrup
regarded the language. Many new features were added around this time, the most notable of which
are virtual functions, function overloading, references with the & symbol, the const keyword, and
single-line comments using two forward slashes (which is a feature taken from the language BCPL).

In 1985, Stroustrup's reference to the language entitled The C++ Programming Language was
published. That same year, C++ was implemented as a commercial product. The language was not
officially standardized yet, making the book a very important reference. The language was updated
again in 1989 to include protected and static members, as well as inheritance from several classes.

In 1990, The Annotated C++ Reference Manual was released. The same year, Borland's Turbo C++
compiler would be released as a commercial product. Turbo C++ added a plethora of additional
libraries which would have a considerable impact on C++'s development. Although Turbo C++'s last
stable release was in 2006, the compiler is still widely used.

In 1998, the C++ standards committee published the first international standard for C++ ISO/IEC
14882:1998, which would be informally known as C++98. The Annotated C++ Reference Manual was
said to be a large influence in the development of the standard. The Standard Template Library, which
began its conceptual development in 1979, was also included. In 2003, the committee responded to
multiple problems that were reported with their 1998 standard, and revised it accordingly. The
changed language was dubbed C++03.

In 2005, the C++ standards committee released a technical report (dubbed TR1) detailing various
features they were planning to add to the latest C++ standard. The new standard was informally
dubbed C++0x as it was expected to be released sometime before the end of the first decade.
Ironically, however, the new standard would not be released until mid-2011. Several technical reports
were released up until then, and some compilers began adding experimental support for the new
features.

In mid-2011, the new C++ standard (dubbed C++11) was finished. The Boost library project made a
considerable impact on the new standard, and some of the new modules were derived directly from
the corresponding Boost libraries. Some of the new features included regular expression support
(details on regular expressions may be found here), a comprehensive randomization library, a new C+
+ time library, atomics support, a standard threading library (which up until 2011 both C and C++
were lacking), a new for loop syntax providing functionality similar to foreach loops in certain other
languages, the auto keyword, new container classes, better support for unions and array-initialization
lists, and variadic templates.
Difference between C and C++

C C++

C was developed by Dennis Ritchie

between the year 1969 and 1973 at C++ was developed by Bjarne Stroustrup in

AT&T Bell Labs. 1979.

C does no support polymorphism,

encapsulation, and inheritance which C++ supports polymorphism, encapsulation,

means that C does not support object and inheritance because it is an object

oriented programming. oriented programming language.

C is a subset of C++. C++ is a superset of C.

C contains 32 keywords. C++ contains 52 keywords.

C++ is known as hybrid language because

For the development of code, C C++ supports both procedural and object

supports procedural programming. oriented programming paradigms.

Data and functions are separated in C

because it is a procedural programming Data and functions are encapsulated

language. together in form of an object in C++.

Data is hidden by the Encapsulation to

ensure that data structures and operators

C does not support information hiding. are used as intended.

Built-in & user-defined data types is

Built-in data types is supported in C. supported in C++.


C C++

C is a function driven language because

C is a procedural programming C++ is an object driven language because it

language. is an object oriented programming.

Function and operator overloading is not Function and operator overloading is

supported in C. supported by C++.

C is a function-driven language. C++ is an object-driven language

Functions in C are not defined inside Functions can be used inside a structure in

structures. C++.

Namespace features are not present Namespace is used by C++, which avoid

inside the C. name collisions.

Header file used by C is stdio.h. Header file used by C++ is iostream.h.

Reference variables are not supported

by C. Reference variables are supported by C++.

Virtual and friend functions are not Virtual and friend functions are supported by

supported by C. C++.

C does not support inheritance. C++ supports inheritance.

Instead of focusing on data, C focuses C++ focuses on data instead of focusing on

on method or process. method or procedure.

provides malloc() and calloc() functions C++ provides new operator for memory

for dynamic memory allocation, allocation and delete operator for memory

and free() for memory de-allocation. de-allocation.

Direct support for exception handling is Exception handling is supported by C++.


C C++

not supported by C.

scanf() and printf() functions are used for cin and cout are used for input/output in C+

input/output in C. +.

Part of the Hello World Program


Your first C++ program in Lesson 1, “Getting Started,” did nothing more than write a simple “Hello World”
statement to the screen. Yet this program contains some of the most important and basic building blocks of a
C++ program. You use Listing 2.1 as a starting point to analyze components all C++ programs contain.

Listing 2.1. HelloWorldAnalysis.cpp: Analyze a C++ Program

1: // Preprocessor directive that includes header iostream

2: #include <iostream>

3:

4: // Start of your program: function block main()

5: int main()

6: {

7: /* Write to the screen */

8: std::cout << "Hello World" << std::endl;


9:

10: // Return a value to the OS

11: return 0;

12: }

This C++ program can be broadly classified into two parts: the preprocessor directives that start with a # and
the main body of the program that starts with int main().

NOTE

Lines 1, 4, 7, and 10, which start with a // or with a /*, are comments and are ignored by the compiler. These

comments are for humans to read.


Comments are discussed in greater detail in the next section.

Preprocessor Directive #include


As the name suggests, a preprocessor is a tool that runs before the actual compilation starts. Preprocessor
directives are commands to the preprocessor and always start with a pound sign #. In Line 2 of Listing
2.1, #include <filename> tells the preprocessor to take the contents of the file ( iostream, in this case)
and include them at the line where the directive is made. iostream is a standard header file that is included
because it contains the definition of std::cout used in Line 8 that prints “Hello World” on the screen. In other
words, the compiler was able to compile Line 8 that contains std::cout because we instructed the
preprocessor to include the definition of std::cout in Line 2.

NOTE

In professionally programmed C++ applications, not all includes are only standard headers. Complex
applications are typically programmed in multiple files wherein some need to include others. So, if an artifact
declared in FileA needs to be used in FileB, you need to include the former in the latter. You usually do that by
putting the following include statement in FileA:

#include "...relative path to FileB\FileB"a


We use quotes in this case and not angle brackets in including a self-created header. <> brackets are typically

used when including standard headers.


The Body of Your Program main()
Following the preprocessor directive(s) is the body of the program characterized by the function main(). The
execution of a C++ program always starts here. It is a standardized convention that function main() is
declared with an int preceding it. int is the return value type of the function main().

NOTE

In many C++ applications, you find a variant of the main() function that looks like this:

int main (int argc, char* argv[])

This is also standard compliant and acceptable as main returns int. The contents of the parenthesis are

“arguments” supplied to the program. This program possibly allows the user to start it with command-line
arguments, such as

program.exe /DoSomethingSpecific

/DoSomethingSpecific is the argument for that program passed by the OS as a parameter to it, to be
handled within main.

Let’s discuss Line 8 that fulfills the actual purpose of this program!

std::cout << "Hello World" << std::endl;

cout (“console-out”, also pronounced see-out) is the statement that writes “Hello World” to the
screen. cout is a stream defined in the standard namespace (hence, std::cout), and what you are doing in
this line is putting the text “Hello World” into this stream by using the stream insertion
operator <<. std::endl is used to end a line, and inserting it into a stream is akin to inserting a carriage

return. Note that the stream insertion operator is used every time a new entity needs to be inserted into the
stream.
The good thing about streams in C++ is that similar stream semantics used with another stream type result in a
different operation being performed with the same text—for example, insertion into a file instead of a console.
Thus, working with streams gets intuitive, and when you are used to one stream (such as cout that writes text
to the console), you find it easy to work with others (such as fstream that helps write text files to the disk).

Streams are discussed in greater detail in Lesson 27, “Using Streams for Input and Output.”

NOTE

The actual text, including the quotes “Hello World”, is called a string literal.
Returning a Value
Functions in C++ need to return a value unless explicitly specified otherwise. main() is a function, too, and

always returns an integer. This value is returned to the operating system (OS) and, depending on the nature of
your application, can be very useful as most OSes provide for an ability to query on the return value of an
application that has terminated naturally. In many cases, one application is launched by another and the parent
application (that launches) wants to know if the child application (that was launched) has completed its task
successfully. The programmer can use the return value of main() to convey a success or error state to the

parent application.
NOTE

Conventionally programmers return 0 in the event of success or -1 in the event of error. However, the return
value is an integer, and the programmer has the flexibility to convey many different states of success or failure
using the available range of integer return values.

CAUTION

C++ is case-sensitive. So, expect compilation to fail if you write Int instead of int, Void instead of void,
and Std::Cout instead of std::cout.

Potrebbero piacerti anche