


Khusboo Tayal
Writing high-quality, efficient, and maintainable C++ code requires more than just knowing the syntax. This tutorial focuses on best practices in C++ programming, which can significantly enhance code quality, performance, and maintainability.
#include <iostream>
using namespace std;
// Descriptive function name
int CalculateSum(int a, int b) {
return a + b;
}
int main() {
int result = CalculateSum(5, 3);
cout << "Sum: " << result << endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {1, 2, 3, 4, 5};
// Range-based for loop
for (int num : numbers) {
cout << num << " ";
}
cout << endl;
// Lambda function
auto square = [](int x) { return x * x; };
cout << "Square of 4: " << square(4) << endl;
return 0;
}
#include <iostream>
#include <memory>
using namespace std;
int main() {
unique_ptr<int> ptr = make_unique<int>(10);
cout << "Unique Pointer Value: " << *ptr << endl;
shared_ptr<int> sp1 = make_shared<int>(20);
shared_ptr<int> sp2 = sp1;
cout << "Shared Pointer Value: " << *sp1 << ", Count: " << sp1.use_count() << endl;
return 0;
}
int *ptr = new int(5); delete ptr;
#include <memory> using namespace std; auto ptr = make_unique<int>(5);
#include <iostream>
using namespace std;
// Constant value
const double PI = 3.14159;
// Function with const parameter
void PrintMessage(const string &message) {
cout << message << endl;
}
// Constant member function
class Circle {
double radius;
public:
Circle(double r) : radius(r) {}
double GetArea() const {
return PI * radius * radius;
}
};
int main() {
Circle c(5);
cout << "Area: " << c.GetArea() << endl;
return 0;
}
int counter = 0; // Global variable (bad practice)
class Counter {
int value;
public:
Counter() : value(0) {}
void Increment() { value++; }
int GetValue() const { return value; }
};#include <iostream>
#include <stdexcept>
using namespace std;
double Divide(double a, double b) {
if (b == 0) {
throw runtime_error("Division by zero");
}
return a / b;
}
int main() {
try {
cout << "Result: " << Divide(10, 0) << endl;
} catch (const exception &e) {
cout << "Error: " << e.what() << endl;
}
return 0;
}
#include <iostream>
using namespace std;
enum class Color { Red, Green, Blue };
int main() {
Color color = Color::Red;
if (color == Color::Red) {
cout << "Color is Red" << endl;
}
return 0;
}
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
mutex mtx;
void PrintMessage() {
lock_guard<mutex> lock(mtx);
cout << "Thread-Safe Message" << endl;
}
int main() {
thread t1(PrintMessage);
thread t2(PrintMessage);
t1.join();
t2.join();
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
cout << num << " ";
}
return 0;
}
#include <iostream>
using namespace std;
// Reusable Function
template <typename T>
T Add(T a, T b) {
return a + b;
}
int main() {
cout << "Int Addition: " << Add(3, 4) << endl;
cout << "Double Addition: " << Add(2.5, 3.5) << endl;
return 0;
}
#include <iostream>
using namespace std;
// Function to calculate factorial of a number
int Factorial(int n) {
if (n <= 1) return 1;
return n * Factorial(n - 1);
}
#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
unordered_map<string, int> ageMap;
ageMap["Alice"] = 25;
ageMap["Bob"] = 30;
cout << "Alice's age: " << ageMap["Alice"] << endl;
return 0;
}
#include <iostream>
#include <cassert>
using namespace std;
int main() {
int value = 10;
assert(value > 0); // Debug assertion
cout << "Value is positive" << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> numbers = {4, 2, 1, 3};
sort(numbers.begin(), numbers.end());
for (int num : numbers) {
cout << num << " ";
}
return 0;
}
In this tutorial, we explored essential best practices in C++ programming that help you write clean, efficient, and maintainable code:
By following these best practices, you can build scalable, efficient, and robust C++ applications suitable for real-world projects.