kids encyclopedia robot

C++ facts for kids

Kids Encyclopedia Facts
Quick facts for kids
C++
ISO C++ Logo.svg
Logo endorsed by the C++ standards committee
Paradigms Multi-paradigm: procedural, imperative, functional, object-oriented, generic, modular
Family C
Designed by Bjarne Stroustrup
Developer ISO/IEC JTC 1 (Joint Technical Committee 1) / SC 22 (Subcommittee 22) / WG 21 (Working Group 21)
First appeared 1985; 40 years ago (1985)
Stable release
C++20 (ISO/IEC 14882:2020) / 15 December 2020; 4 years ago (2020-12-15)
Preview release
C++23 / 19 March 2023; 2 years ago (2023-03-19)
Typing discipline Static, strong, nominative, partially inferred
OS Cross-platform
Filename extensions .C, .cc, .cpp, .cxx, .c++, .h, .H, .hh, .hpp, .hxx, .h++ .cppm, .ixx
Major implementations
GCC, LLVM Clang, Microsoft Visual C++, Embarcadero C++Builder, Intel C++ Compiler, IBM XL C++, EDG
Influenced by
Ada, ALGOL 68, BCPL, C, CLU, F#, ML, Mesa, Modula-2, Simula, Smalltalk
Influenced
Ada 95, C#, C99, Carbon, Chapel, Clojure, D, Java, JS++, Lua, Nim, Objective-C++, Perl, PHP, Python, Rust, Seed7

C++ (pronounced "C plus plus" and sometimes called CPP) is a powerful programming language. It was created by a Danish computer scientist named Bjarne Stroustrup. C++ was first released in 1985.

It started as an improved version of the C programming language. Over time, C++ has grown a lot. It can now handle different ways of programming. This includes object-oriented programming, generic programming, and functional programming.

C++ is great for making many kinds of software. It's used for systems programming, like creating operating systems such as Linux or Windows. It's also used for embedded software in devices with limited resources.

Because it's fast and efficient, C++ is popular for video games. It's also used for desktop applications and servers. Many companies like Microsoft and Intel provide tools to write C++ code.

C++ is kept up-to-date by the International Organization for Standardization (ISO). The latest version, C++20, came out in December 2020. New versions are released about every three years.

History of C++

BjarneStroustrup
Bjarne Stroustrup, the creator of C++, in his AT&T New Jersey office, around 2000.

In 1979, Bjarne Stroustrup started working on what would become C++. He called it "C with Classes." He wanted a language that was fast like BCPL but also easy to organize like Simula.

Stroustrup chose to build on the C language. C was already popular because it was fast and could be used on many different computers. He added new features to C, like "classes." Classes help organize code into reusable parts.

In 1982, Stroustrup decided to rename "C with Classes" to "C++." The "++" comes from a common symbol in C. It means to "add one" or "increase." So, C++ means "C, but better" or "the next version of C."

20160121 CppFRUG Joel Falcou CppQuiz 3
People taking a quiz on C++ features in Paris in 2015.

New features were added to C++ over time. These included virtual functions and the ability to use the same name for different functions (called operator overloading). In 1985, the first book about C++ was released. It became the main guide for the language.

In 1989, C++ 2.0 came out with more improvements. In 1998, C++ became an official international standard. This meant everyone agreed on how the language should work. After that, new versions like C++11, C++14, C++17, and C++20 added even more features.

In 2018, Bjarne Stroustrup won a special award for creating C++. Today, C++ is one of the most popular programming languages in the world.

What Does "C++" Mean?

The name "C++" shows how the language grew from C. Rick Mascitti came up with the name in 1983.

The "++" symbol is an operator in C. It means to increase a variable's value by one. So, C++ suggests it's an improved version of C. Before its final name, people called it "new C" or "C with Classes."

How C++ Was Designed

C++ was built with some important ideas in mind:

  • It should solve real-world problems and be useful right away.
  • Every feature should be possible to create.
  • Programmers should be able to choose their own coding style.
  • It's better to allow a useful feature than to stop every possible mistake.
  • Programs should be easy to organize into different parts.
  • It should work well with other programming languages.
  • If the computer doesn't know what the programmer wants, the programmer should be able to give exact instructions.

C++ Standards

C++ standards
Year Standard Name Common Name
1998 14882:1998 C++98
2003 14882:2003 C++03
2011 14882:2011 C++11
2014 14882:2014 C++14
2017 14882:2017 C++17
2020 14882:2020 C++20
2023 C++23

C++ is officially standardized by a group called JTC1/SC22/WG21. This group makes sure that C++ works the same way everywhere. They have released several versions of the C++ standard.

C++ Standards Committee meeting - July 1996 Stockholm - Wednesday general session
A meeting of the C++ standards committee in Stockholm in 1996.

The first official standard, C++98, came out in 1998. A small update, C++03, followed in 2003.

A big update, C++11, was released in 2011. It added many new features to the language. After that, smaller updates like C++14 and C++17 were released. The latest major standard is C++20, published in December 2020. The next one, C++23, is planned.

How C++ Works

C++ combines features from the C language with new tools. It lets you control computer hardware directly. It also provides "abstractions" to make programming easier. Abstractions are like simplified models that hide complex details.

C++ uses a lot of the same rules as C. For example, a simple "Hello, world!" program written in C can often work in C++ too. Here's how you might write "Hello, world!" in C++:

#include <iostream>

int main()
{
    std::cout << "Hello, world!\n";
}

This code tells the computer to print "Hello, world!" on the screen.

Storing Information (Objects)

C++ has different ways to store information, called "objects." Think of objects as containers for data.

  • Static Objects: These objects are created when your program starts. They stay in memory until the program finishes.
  • Thread Objects: If your program uses "threads" (parts of a program that run at the same time), these objects are created for each thread. They disappear when the thread finishes.
  • Automatic Objects: These are common. They are created when you enter a specific part of your code (like inside a function). They are automatically removed when you leave that part of the code. This helps manage memory.
  • Dynamic Objects: You create these objects whenever you need them. You also have to tell the computer when to remove them. This gives you more control over memory.

Templates

C++ templates are like blueprints for creating code. They let you write code that works with different types of data. For example, you could write one template for a list that can hold numbers, words, or any other type of data.

Templates save you from writing the same code over and over for different data types. The computer figures out the exact code to use when your program is built.

Objects and Classes

C++ adds object-oriented programming (OOP) features to C. OOP helps you organize your code using "classes" and "objects."

A class is like a blueprint for creating objects. It defines what kind of data an object will hold and what actions it can perform. An object is a specific instance of a class. For example, "Car" could be a class, and "my red car" could be an object of that class.

OOP has four main ideas:

  • Abstraction: Hiding complex details and showing only what's important.
  • Encapsulation: Keeping data and the code that works with it together. This protects the data from being changed accidentally.
  • Inheritance: Allowing new classes to reuse code from existing classes. A "Sports Car" class could inherit features from a "Car" class.
  • Polymorphism: Allowing objects to behave differently depending on their type.

Encapsulation

Encapsulation means keeping the inner workings of a class private. You decide what parts of your class are public (anyone can use) and what parts are private (only the class itself can use). This helps keep your code organized and prevents mistakes.

Inheritance

Inheritance lets a new class (called a "derived class") get features from an existing class (called a "base class"). This is like how you might inherit traits from your parents. It helps you reuse code and build new classes based on existing ones.

For example, a "Dog" class could inherit from an "Animal" class. The "Dog" class would automatically have all the features of an "Animal" (like eating and sleeping) and then you could add dog-specific features (like barking).

Operators

Operators that cannot be overloaded
Operator Symbol
Scope resolution ::
Conditional ?:
dot .
Member selection .*
"sizeof" sizeof
"typeid" typeid

C++ has many operators, which are symbols that perform actions. Examples include `+` for addition or `*` for multiplication.

Most operators can be "overloaded." This means you can make them do special things for your own custom data types. For instance, you could make the `+` operator add two "Vector" objects in your game.

Polymorphism

Polymorphism means "many forms." In programming, it means that different objects can respond to the same command in their own way.

  • Static Polymorphism: This happens when your program is being built (compiled). For example, you can have two functions with the same name but different inputs. The compiler knows which one to use based on the inputs.
  • Dynamic Polymorphism: This happens when your program is running. It's often used with inheritance. If you have a "Vehicle" class and "Car" and "Boat" classes that inherit from it, you can tell a "Vehicle" to "move." The "Car" will drive, and the "Boat" will float, even though you gave the same command. This is often done using "virtual functions."

Virtual Functions

Virtual functions are a key part of dynamic polymorphism. They allow a function in a base class to be overridden by a function in a derived class. When you call a virtual function, the computer decides which version to run based on the actual type of the object, not just its declared type.

Lambda Expressions

C++ lets you create small, unnamed functions called "lambda expressions." They are useful for short tasks. You can write them directly where you need them in your code.

Here's a simple example:

[](int x, int y) { return x + y; } // This lambda adds two numbers

Handling Errors (Exceptions)

Exception handling is how C++ deals with problems that happen while your program is running. If something unexpected occurs, like trying to open a file that doesn't exist, the program can "throw an exception."

You can then "catch" this exception in another part of your code. This lets you handle the error gracefully, instead of the program crashing.

Here's an example of catching an error:

#include <iostream>
#include <vector>
#include <stdexcept>

int main() {
    try {
        std::vector<int> vec{3, 4, 3, 1};
        int i{vec.at(4)}; // This tries to get the 5th item, but there are only 4 (0-3)
    }
    // If the above code tries to access something out of range, this part runs:
    catch (const std::out_of_range &e) {
        std::cerr << "Oops! You tried to get an item that isn't there: " << e.what() << '\n';
    }
    // This catches any other standard errors:
    catch (const std::exception &e) {
        std::cerr << "An error happened: " << e.what() << '\n';
    }
    // This catches any other unexpected errors:
    catch (...) {
        std::cerr << "Something went very wrong!\n";
    }
}

C++ Standard Library

ANSI ISO C++ WP
The draft standard for C++98. Half of it was about the C++ Standard Library.

The C++ standard includes not just the language itself, but also a huge collection of ready-to-use tools called the "standard library." This library helps programmers do common tasks without writing everything from scratch.

It includes:

  • Containers: Ways to store collections of data, like vectors (like dynamic arrays) and lists.
  • Algorithms: Functions to do common operations, like finding items, sorting lists, or searching.
  • Input/Output: Tools for reading and writing data, like showing text on the screen or saving to files.
  • Smart Pointers: Tools that help manage memory automatically.
  • Multithreading: Features for making programs do several things at once.

A big part of the C++ library is based on the Standard Template Library (STL). The STL was designed by Alexander Stepanov. It provides many useful tools that work with templates, making them very flexible.

Most C++ compilers come with a full version of the C++ standard library.

C++ Core Guidelines

The C++ Core Guidelines are a set of rules and best practices. They were created by Bjarne Stroustrup and Herb Sutter. Their goal is to help programmers write modern, safe, and efficient C++ code. These guidelines are especially for C++11 and newer versions of the language.

They also help people who create tools that check code for mistakes. The main idea is to write code that is safe with types and memory.

Compatibility

C++ is often thought of as a bigger version of C. While most C code can work in C++, there are some small differences. For example, C++ is stricter about how you use certain types of data.

To make C and C++ code work together, you sometimes need to use a special command: `extern "C"`. This tells the C++ compiler to treat that part of the code like C code.

Images for kids

kids search engine
C++ Facts for Kids. Kiddle Encyclopedia.