Lexellence Games Logo

Design and Creation of C++

           

            Initially, bits, memory, and instructions had to be manipulated or executed directly. Manual computer operation was like quicksand compared to software today. The invention of the program-storing computer was indispensable to the progression of the computer industry, but programming was still slow and difficult to manage anything beyond the simplest programs. Assembly language replaced the binary in machine code with more readable identifiers, but a great deal of mental effort was still required to use it. Software creation really boomed after higher level programming languages such as C blessed programmers with something our brains can understand, alongside compilers that did the tedious work of translating the human-readable code into machine-readable code. Bjarne Stroustrup created C++ in the early 1980s to make systems programming easier compared to C or assembly. His specific problem involved distribution of UNIX kernel services aI Love C++ Coffee Mugcross multiple processors and computers. To solve it, he needed the abstraction mechanisms of Simula, and C’s efficiency and low-level facilities for systems programming tasks and concurrency. Simula was too slow, in application performance and in compilation time, and C didn’t have the modularity needed for his solution. At first he just added classes to C and named it appropriately: “C with Classes”. Then with C++ he implemented operator overloading, references, virtual functions, and more. Exceptions and templates came later, from different sources of inspiration. The name implies that it evolved from C because “++” is C’s increment operator, and D was not chosen because features generally were not removed. The decisions for C++ were made in an evolutionary process of solving problems encountered by users; there was no design committee in the early days, just Stroustrup and his discussions with friends and colleagues responding to problems as they came. These days, we have a standard framework of commonly used algorithms and containers (STL) so we won’t have to reinvent the wheel, and a committee which releases updates to C++ every few years, adding new features and tidying up existing ones.


            C++ is a programming language for developing and using elegant and efficient abstractions. C brought with it “close to the machine” efficiency, with its direct hardware mappings of built-in types and operations. C++ introduced “close to the problem to be solved” elegance by providing affordable and flexible user-defined abstract data types to directly and concisely express the concepts involved in the solution. They have the same performance, notation, and range of uses as built-in types. In fact, by design, there aren’t any features of C++ which need to be avoided in code that is under severe time and space constraints. The elegance offered by C++ allows us to get around the difficulty of thinking about all the parts of a program at once. Our minds are not designed to manage large, complex systems all at once. The expression of concepts in code allowed by C++ means the program’s structure becomes clearer as relationships are reflected more in the code itself instead of just in the comments. Implementation details getting hidden also reduces what could go wrong and what you have to think about at any given time. By managing complexity and making code more readable, C++ developers are able to reason about much larger programs than they could in assembly language.


            C++ is general-purpose, aiming to support the widest possible range of applications that it can support well. There is no deliberate area of specialization built into C++. Developers of all kinds of software, such as banks, hospitals, games, and military applications have come to rely on C++. That being said, its uncompromising low-level efficiency and interoperability with code written in other languages make C++ a popular systems programming language for creating device drivers, operating systems, games, or any software for which reliability, extensibility, or performance are critical concerns. Compilers of many other programming languages, Java Virtual Machines, and browsers use C++. Most technologies that we interact with daily utilize C++ in some aspect of it.


            C++ supports a synthesis of multiple programming styles. Each individual language feature is one of many different kinds of building blocks which can be combined to form a solution. For most non-trivial programs, the best solution will combine procedural, data-abstracted/object-oriented, generic, or other programming styles. The object-oriented way of programming is very useful when the concepts in your problem have clear “is-a” or “has-a” relationships, but C++ does not force you to use OOP, nor does it penalize you if you don’t use it. In the game engine I am working on, I use all three styles. For entities in the game, I ditched object-oriented programming in favor of plain C because the object-oriented approach quickly takes you into deep-multiple-inheritance-hierarchy-hell. Switching to a procedural style with components and systems simplified the experimental process of combining components to make new entities, and increased the cache-friendliness of the data structures. On the other hand, the object-oriented approach was useful when designing a system to separate the code for each game state (intro, menu, game-play, etc.). The use of polymorphism allowed me to get rid of the game state manager altogether and cleaned up the code considerably. For generic programming, templates are C++’s tool. I wanted my resource manager to allow multiple game states to use the same resources (textures, sounds, shapes, etc.) without having to load them twice, and without the game states having to know about each other's simultaneous resource use. Templating simplified the task of reusing the resource manager skeleton code for each type of resource so that it can be used with any resource stored in a file. C++ lets you combine styles in whatever way makes sense for your solution without punishing you for ignoring the others.


            Two principles guided the design of C++:

  • Lowest Level: No room was allowed to be left for more efficient code to be written in a lower-level language, except very rare assembly.
  • Zero Overhead: No feature could be included if an even slightly better version could be reasonably hand-written.

  •             High level libraries can be created with C++ that allow you to make future programming tasks pleasant. There are thousands of libraries already created that do a lot of hard work for you, and you can build your own libraries on top of that. I am creating a game development library along with my game so that my next game can reuse a lot of the same code.

                C++ adds safety to C’s type system. The compiler will help make sure you don’t send the wrong type of data to a function. That being said, C++ gives you complete freedom because you can basically cast types into other types if you want. This freedom comes with responsibility. Use it carefully because C++ generally won’t stop you from doing unsafe things that will cause unpredictable behavior. Fortunately, there are STL containers such as vectors and smart pointers to help avoid common errors when managing arrays and memory.

                C++ was never meant to be perfect, and it has been criticized on a number of grounds. One is that the efficiency you get costs you the burden of having to deal with low level details. For this reason, many have chosen for their projects other high-level languages developed since C++ that are relieved of these details. Many would also say that C++ is not the recommended starting point for a beginning programmer because of its relative complexity and the many things that one must deal with that are unrelated to the problem domain. Some projects might require less effort if a different language is chosen. That being said, I personally found C++ easy to learn, and I find it's syntax intuitive.
               
                I love C++.

                Further reading:
  • The C++ Programming Language, 4th Ed. by Bjarne Stroustrup
  • The C++ Super FAQ @ isocpp.org