C++
Official website
The C++ programming language doesn’t have an “official” website maintained by a single central authority like some other languages. However, the C++ Standards Committee and the ISO C++ Foundation support a central resource at:
This site provides a wealth of information about the C++ language, its standards, upcoming developments, community resources, and more. It’s widely regarded as the go-to resource for anything related to modern C++.
Additionally, while some websites (for example, cplusplus.com) exist and are popular, they are community-run and not officially maintained by the organizations responsible for the C++ standard.
Static typing
Yes, C++ is a statically typed language. In C++, the type of every variable and expression is known at compile time, and the compiler enforces type constraints during compilation. This means that type-related errors, such as trying to assign a value of one type to a variable of another incompatible type, are caught before the program runs.
That said, C++ does have some dynamic features:
- Runtime type identification (RTTI): This mechanism allows programs to identify the type of an object at runtime using features like
dynamic_castandtypeid. - Polymorphism: Through virtual functions, C++ supports runtime polymorphism, which allows the program to decide at runtime which function to invoke.
- Type Casting: C++ provides both compile-time casts (e.g.,
static_cast) and runtime casts (e.g.,dynamic_cast) to allow conversions between different types.
However, these dynamic features operate within the framework of a predominantly statically typed system, where most type checks happen at compile time.
Example code
Below is a complete C++ example that defines a simple binary search tree (BST) and implements a function to search for a value within the tree. In this example, the tree nodes contain integer values, but you can adjust the data type as needed.
#include <iostream>
// Structure representing a node in the BST
struct TreeNode {
int value;
TreeNode* left;
TreeNode* right;
TreeNode(int val) : value(val), left(nullptr), right(nullptr) {}
};
// Function to insert a new value into the BST
TreeNode* insert(TreeNode* root, int val) {
if (root == nullptr) {
return new TreeNode(val);
}
if (val < root->value) {
root->left = insert(root->left, val);
} else if (val > root->value) {
root->right = insert(root->right, val);
}
// If the value is already in the BST, we do not insert duplicates.
return root;
}
// Function to search for a given value in the BST
// Returns a pointer to the node containing the value, or nullptr if not found.
TreeNode* search(TreeNode* root, int target) {
if (root == nullptr) {
return nullptr; // Value not found; reached a leaf node's child.
}
if (target == root->value) {
return root; // Value found.
}
// Recurse down the tree. If target is less than current node's value, search the left subtree, else the right.
if (target < root->value) {
return search(root->left, target);
} else {
return search(root->right, target);
}
}
int main() {
// Create a simple BST by inserting values.
TreeNode* root = nullptr;
int valuesToInsert[] = { 50, 30, 70, 20, 40, 60, 80 };
for (int val : valuesToInsert) {
root = insert(root, val);
}
// The value we want to search for
int target = 60;
TreeNode* result = search(root, target);
if (result != nullptr)
std::cout << "Value " << target << " found in the BST." << std::endl;
else
std::cout << "Value " << target << " not found in the BST." << std::endl;
// Cleanup: Ideally, you should free the tree memory here.
// For a quick example, we won't implement the deletion logic.
return 0;
}
Explanation
- Structure Definition (
TreeNode):- Each node holds an integer
valueand two pointers:leftandright.
- Each node holds an integer
- Insert Function:
- The
insertfunction adds a new value to the BST. It recursively finds the correct position based on the BST property (left subtree contains values less than the node, right subtree contains values greater).
- The
- Search Function:
- The
searchfunction recursively traverses the tree. If the node isnullptr, it means the value was not found. If the current node’s value equals the target, the node is returned. Otherwise, the function continues searching in the left or right subtree based on the comparison.
- The
- Main Function:
- A BST is built by inserting a set of values. The target value is searched in the BST, and the result is printed accordingly.
This code is complete, correct, and runnable. You can compile it with a C++ compiler (such as g++) and run it to see how the BST search works.