C++ Origin and philosophy - History of C++
The history of C++ is quite interesting. C++ is a general-purpose, high-level programming language developed in 1979 by Bjarne Stroustrup at AT & T Bell Laboratories. C++ was created as an advanced version of the C programming language. It extended the features of C and added new ones including classes and objects, type checking, exception handling, inheritance, polymorphism, etc., to the C language. Over the years, the language has received several significant updates to stay in touch with modern programming languages. Even though C++ was created decades ago, it is widely used to develop many software programs even today.
Programming Languages Developed Before C++
Before reading the history of C++, let us take a look at the programming languages developed before C++. Before the origin of the C++ programming language, many different programming languages were developed. Here is a table depicting the history of programming languages developed before C++.
|Year of Origin
|Basic Combined Programming Language
|Brian Kernighan & Dennis Ritchie
Origin of the Name “C++”
Let us take a look at the history of the name C++. During the development of the language, C++ (C plus plus) was initially referred to as "new C". Then it was renamed to "C with Classes", which implied that it was a superset of the well-known C language. The final renaming of the language was done in 1983 by Rick Mascitti when it was renamed to "C++". The ++ in C++ comes from the C language. In C language, ++ means incrementing a variable by 1. So, C++ roughly means "one more than C".
Bjarne Stroustrup addressed the origin of the name "C++" in the preface of his book, The C++ Programming Language, saying that the name "C++" was inspired by George Orwell's book Nineteen Eighty-Four. When Rick Mascitti was asked about the naming in 1992, he indicated that the name was given humorously. He never thought that "C++" would become the formal name of the language.
The philosophy of C++ programming language was laid in The Design and Evolution of C++ (1994) by Bjarne Stroustrup. Understanding the philosophy or rules of C++ helps us to understand why certain things in C++ are the way they are.
Here is the summary of the philosophy of C++:
- The programmer should be free to choose his or her own programming paradigm (procedural programming, object-oriented programming, generic programming, and data abstraction)
- The language should be compatible with C. The transition from C to C++ should not be difficult.
- Every feature in the language should be built for the general purpose. The features should not be platform-specific.
- The language should be designed to work without a sophisticated programming environment (C++ code can even be written on a simple notepad).
- The language should be statically typed and for general purposes. It should be as portable as C, i.e., the code written on one computer should be usable on another computer with little to no change required in the code.
- The language should give the option to programmers to make their own choice (choosing among different variable types, allocating and deallocating memory as per the program's needs, etc.), even if it increases the possibility that a programmer will choose incorrectly.
- The language should not slow down a program or consume space (overhead) for the features not used in the code.
- There should be no language underneath C++, except the assembly language.
An interface is a programming structure that describes the behavior of a class in C++ without defining a specific implementation of that class. For example, if we have a class Car and a class Scooter, then each of these classes should have a LightsOn() action (function). Because both the classes share the same action, we can create an interface to reduce code and improve the program's efficiency. But how the lights will be turned on will depend on the implementation of the LightsOn() function in each class. In other words, both classes will have a parent class that contains the LightsOn() function in it. But the specific details of how the lights will turn on will be explained in the respective classes.
Some of the most important aspects of an interface are:
- It should be easy to understand
- It should not be prone to errors
- It should encourage efficient use of the program
Interfaces and abstract classes more or less convey the same idea. This is why C++ interfaces are implemented using the abstract classes. A class with a pure virtual function is called an abstract class in c++. We can create a pure virtual function by writing "= 0" in the function declaration.
NOTE: It is important to note that abstract classes and data abstraction in C++ are two separate concepts. Data abstraction is used to separate the essential data from its implementation techniques.
Let us take an example to understand the implementation of an abstract class:
We have declared a pure virtual function calculateVolume() inside the class Cuboid. Because of this, the Cuboid class became an abstract class. The purpose of this abstract class is to serve as a suitable base class for other classes. The classes which inherit the Cuboid class can now define (and implement) the calculateVolume() as per their need.
Importance of Interfaces
An interface or abstract class is used as the base class for other classes that inherit the abstract class. It provides generic functionality to a class so that other classes can easily use the class's pure virtual functions.
Let us take an example to understand the importance of interfaces.
In the above example, we created an abstract class called Shape. Because the formulas to calculate the volume of a cube and a sphere are different, we created a pure virtual function called findVolume in the Shape class. Then we used this pure virtual function in the derived classes (Sphere and Cube) to calculate the volumes of the respective shapes. The interface class we created helped us avoid code repetition and saved our time as well.
Rules Associated With Interfaces
We should keep certain rules in our minds while we work with interfaces in C++. These are:
- A pure virtual function can not be defined, it can only be declared.
- We can only assign the value 0 to a pure virtual function.
- A derived class will act as an abstract class if it can not implement the pure virtual function of the base class.
- We can not create an instance of an abstract class.
- We can create a pointer to a derived class with a reference of the base (abstract) class.
C++ Standard Library
The Standard Library in C++ is based on the conventions introduced in the Standard Template Library (STL) and the Standard Library of C with some modifications in it. The Standard Template Library provides various useful features like containers (vectors, for example), iterators (generalized pointers) for accessing the containers like arrays, and algorithms to perform different operations (like sorting and searching). We can use these templates to write generic algorithms that can work with any container. As STL consists of a collection of template classes (vectors, arrays, queue, etc.), it is a generalized library that is independent of data types. It is a kind of blueprint that contains all the parametrized components. To work with the standard template library, we should know how to work with different components and features of template classes.
We can access the different features of the standard library in C++ using the sixty-nine standard headers provided in C++ (nineteen of which are not used anymore). These features can be declared using the namespace std in the code. Using the standard library helps us avoid writing code from scratch. It helps us save time as many things needed in a code are already present in the C++ standard library.
We should always use some standard rules defined for the standard library. These are:
- Use libraries wherever possible: The standard library is created to save the time and effort of the programmer. Utilize the benefits of the work done by other programmers.
- Use the standard library over other libraries: The standard library is being developed and maintained by several experienced programmers. It is likely to be more stable and well maintained compared to other libraries and even your code.
- Never add non-standard entities to the namespace std: Adding non-standard entities to std might change the meaning of the template. It can even clash with the future versions of the standard library.
- Prefer the type-safe manner with the standard library: Avoiding the type-safe manner while using the standard library can result in unexpected program behavior, corruption of memory, and errors that are difficult to recognize.
Characteristics/Features of C++
Compared to C, C++ introduced many new features in the language. Let us take a look at some of the significant features of C++:
- Simple: One of the reasons why C++ is the first programming language of many programmers is that C++ is simple and easy to learn. Even though it is beginner-friendly, C++ is widely used to create numerous advanced programs.
- Object Oriented: C++ is an object-oriented programming language. Objects make the development and maintenance of software easy. With the help of these objects, we can perform data abstraction, data encapsulation, inheritance, polymorphism, and data hiding.
- Dynamic Memory Allocation: C++ supports dynamic memory allocation. With the help of pointers in C++, we can allocate the memory dynamically.
- Pointers: A pointer is a variable that stores the address of another variable. C++ supports the use of pointers. They are used to interact with the memory location of any variable.
- Broad Library: C++ offers a vast library full of in-built functions that make it easy for the programmer to code. The library functions can be accessed using different header files in C++.
- Compiler Based: Unlike other languages like Python and Java which are interpreter-based, C++ is a compiler-based language. Hence, C++ is much faster than Python or Java.
- Operator Overloading: C++ supports operator overloading. This means C++ can provide the operators with a special meaning for any given data type. It provides the programmers an option for creating new definitions of the C++ operators.
- Case Sensitive: Just like C, C++ is case-sensitive. It means that C++ treats lowercase and uppercase letters differently.
- High-Level Programming Language: Unlike C, which is a mid-level programming language, C++ is a high-level language. It makes it easier for the programmer to work in C++ as we can closely associate it with the English language.
- Exception Handling: C++ supports exception handling. It helps the programmer to catch the error(s) if an error arises in a program.
- Portable: C++ programs can be executed in different machines with little or no change in the code. But C++ is not platform-independent. Suppose we have compiled a program in a Windows operating system. In that case, that compiled file (.exe file) will not work in a Mac operating system. But, a .cpp file created in Windows will work perfectly on Mac operating system.
- C++ was developed in 1979 by Bjarne Stroustrup.
- It was built as an enhancement to the C programming language.
- The standard library in C++ provides various useful features that make it easy for programmers to code.
- As C++ is a compiler-based language, it is much faster compared to languages like Java and Python.