This content originally appeared on DEV Community and was authored by Madhav Ganesan
C++ (or “C-plus-plus”)
History:
C++ was developed by Danish computer scientist Bjarne Stroustrup at AT&T Bell Labs in 1979 as an extension of the C programming language. It emerged from an analysis of the UNIX kernel to explore how it could be distributed across a network. The initial version of C++ was publicly released in 1983.
Paradigms:
Procedural Programming
#include <iostream>
void greet() {
std::cout << "Hello, World!" << std::endl;
}
int main() {
greet();
return 0;
}
Object Oriented Programming
#include <iostream>
#include <string>
using namespace std;
class Animal {
public:
Animal(const string& name) : name(name) {}
void speak() const {
cout << "My name is " << name << endl;
}
private:
string name;
};
int main() {
Animal dog("Doggy");
dog.speak();
return 0;
}
Functional Programming
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
for_each(vec.begin(), vec.end(), [](int &n) { n *= 2; });
for (const auto &n : vec) {
cout << n << " ";
}
return 0;
}
Low-level Programming
#include <iostream>
int main() {
int a = 5;
int *p = &a;
std::cout << "Value of a: " << a << std::endl;
std::cout << "Address of a: " << p << std::endl;
std::cout << "Value at address p: " << *p << std::endl;
return 0;
}
Libraries and Frameworks:
External Libraries:
Third-party libraries (e.g., Boost for C++).
Standard Library:
Built-in libraries provided by the language (e.g., STL in C++).
The C++ Standard Library is a collection of classes and functions, which are part of the ISO C++ standard and come bundled with every C++ compiler. It provides a wide range of functionalities, including containers, algorithms, iterators, input/output facilities, and more.
std namespace is used differentiate functions present in different libraries such as , etc.
Components of the C++ Standard Library
Containers: vector, list, deque, set, map, array, etc.
Algorithms: sort, find, accumulate, copy, etc.
Iterators: iterator, reverse_iterator, ostream_iterator, etc.
Function Objects: std::less, std::greater, custom functors.
Streams: std::cin, std::cout, std::cerr, std::ifstream, std::ofstream.
Stream Manipulators: std::endl, std::setw, std::setprecision.
String Class: std::string, std::wstring.
Smart Pointers: std::unique_ptr, std::shared_ptr, std::weak_ptr.
Pairs and Tuples: std::pair, std::tuple.
Functional Utilities: std::function, std::bind, std::mem_fn.
Memory Management
Allocators: std::allocator.
Memory Functions: std::allocate_shared, std::make_unique.
Threads: std::thread.
Mutexes and Locks: std::mutex, std::lock_guard.
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <memory>
int main() {
// Containers and algorithms
std::vector<int> vec = {1, 2, 3, 4, 5};
std::sort(vec.begin(), vec.end(), std::greater<int>());
int sum = std::accumulate(vec.begin(), vec.end(), 0);
// Output the results
std::cout << "Sorted vector in descending order: ";
for (const auto& val : vec) {
std::cout << val << " ";
}
std::cout << std::endl;
std::cout << "Sum of elements: " << sum << std::endl;
// Smart pointers
std::unique_ptr<int> ptr = std::make_unique<int>(10);
std::cout << "Value of unique_ptr: " << *ptr << std::endl;
return 0;
}
Performance
C++ is a compiled language where source is translated into machine code by a compiler. Compiled languages generally have better performance while interpreted languages may be slower.
Compiler:
Turbo C++
Type System:
Static Typing: Types are checked at compile-time.
Strong Typing: Strict type rules are enforced.
Static Type Checking: Ensures type correctness before execution.
Abstraction:
Both high-level and low-level abstraction
Important facts:
All member functions and variables of a class are private by default.
Usage:
Both C and C++ are widely used in system programming, including operating systems, embedded systems, and hardware-level programming.
Feel free to reach out if you have any questions or need further assistance. 😊📁✨
This content originally appeared on DEV Community and was authored by Madhav Ganesan
Madhav Ganesan | Sciencx (2024-07-19T01:50:56+00:00) Introduction to C++. Retrieved from https://www.scien.cx/2024/07/19/introduction-to-c/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.