#include <iostream>
#include <vector>
#include <memory>
#include <string>
#include <algorithm>
#include <functional>
#include <exception>
namespace geometry {
class Shape {
public:
virtual ~Shape() = default;
virtual double area() const = 0;
virtual double perimeter() const { return 0.0; }
void display() const {
std::cout << "Shape with area: " << area() << std::endl;
}
};
class Circle : public Shape {
private:
double radius_;
public:
explicit Circle(double r) : radius_(r) {}
double area() const override {
return 3.14159 * radius_ * radius_;
}
double perimeter() const override {
return 2 * 3.14159 * radius_;
}
double radius() const noexcept { return radius_; }
};
class Rectangle : public Shape {
private:
double width_, height_;
public:
Rectangle(double w, double h) : width_(w), height_(h) {}
double area() const override {
return width_ * height_;
}
double perimeter() const override {
return 2 * (width_ + height_);
}
};
}
namespace utils {
template<typename T>
constexpr T max(const T& a, const T& b) {
return (a > b) ? a : b;
}
template<class Container, class Predicate>
auto filter(const Container& container, Predicate pred) -> std::vector<typename Container::value_type> {
std::vector<typename Container::value_type> result;
std::copy_if(container.begin(), container.end(),
std::back_inserter(result), pred);
return result;
}
template<>
constexpr int max<int>(const int& a, const int& b) {
return (a > b) ? a : b;
}
}
class ResourceManager {
private:
std::unique_ptr<int[]> data_;
size_t size_;
public:
explicit ResourceManager(size_t size)
: data_(std::make_unique<int[]>(size)), size_(size) {
std::cout << "ResourceManager allocated " << size << " integers\n";
}
ResourceManager(ResourceManager&& other) noexcept
: data_(std::move(other.data_)), size_(other.size_) {
other.size_ = 0;
}
ResourceManager& operator=(ResourceManager&& other) noexcept {
if (this != &other) {
data_ = std::move(other.data_);
size_ = other.size_;
other.size_ = 0;
}
return *this;
}
ResourceManager(const ResourceManager&) = delete;
ResourceManager& operator=(const ResourceManager&) = delete;
~ResourceManager() {
if (size_ > 0) {
std::cout << "ResourceManager deallocating " << size_ << " integers\n";
}
}
int& operator[](size_t index) {
return data_[index];
}
const int& operator[](size_t index) const {
return data_[index];
}
size_t size() const noexcept { return size_; }
};
class CustomException : public std::exception {
private:
std::string message_;
public:
explicit CustomException(const std::string& msg) : message_(msg) {}
const char* what() const noexcept override {
return message_.c_str();
}
};
void demonstrate_exceptions(bool risky_operation) {
try {
ResourceManager manager(10);
if (risky_operation) {
throw CustomException("Simulated error in operation");
}
for (size_t i = 0; i < manager.size(); ++i) {
manager[i] = static_cast<int>(i * i);
}
} catch (const CustomException& e) {
std::cerr << "Custom exception caught: " << e.what() << std::endl;
} catch (const std::exception& e) {
std::cerr << "Standard exception caught: " << e.what() << std::endl;
}
}
void demonstrate_lambdas() {
std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int multiplier = 2;
auto multiply = [multiplier](int x) { return x * multiplier; };
int sum = 0;
auto accumulate = [&sum](int x) { sum += x; };
auto generic_print = [](const auto& value) {
std::cout << value << " ";
};
std::for_each(numbers.begin(), numbers.end(), accumulate);
std::cout << "Sum: " << sum << std::endl;
std::vector<int> doubled;
std::transform(numbers.begin(), numbers.end(),
std::back_inserter(doubled), multiply);
std::cout << "Doubled: ";
std::for_each(doubled.begin(), doubled.end(), generic_print);
std::cout << std::endl;
}
template<typename T>
void perfect_forward(T&& value) {
auto process = [](auto&& arg) {
std::cout << "Processing: " << std::forward<decltype(arg)>(arg) << std::endl;
};
process(std::forward<T>(value));
}
int main() {
std::cout << "=== C++ Comprehensive Example ===" << std::endl;
std::vector<std::unique_ptr<geometry::Shape>> shapes;
shapes.push_back(std::make_unique<geometry::Circle>(5.0));
shapes.push_back(std::make_unique<geometry::Rectangle>(4.0, 6.0));
for (const auto& shape : shapes) {
shape->display();
}
std::cout << "Max of 10 and 20: " << utils::max(10, 20) << std::endl;
std::cout << "Max of 3.14 and 2.71: " << utils::max(3.14, 2.71) << std::endl;
std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto even_numbers = utils::filter(numbers, [](int x) { return x % 2 == 0; });
std::cout << "Even numbers: ";
for (int n : even_numbers) {
std::cout << n << " ";
}
std::cout << std::endl;
demonstrate_exceptions(false); demonstrate_exceptions(true);
demonstrate_lambdas();
std::string text = "Hello, World!";
perfect_forward(text); perfect_forward(std::string("Temp"));
auto automatic_int = 42;
auto automatic_double = 3.14159;
auto automatic_string = std::string("auto deduction");
std::cout << "Auto deduced types: "
<< automatic_int << ", "
<< automatic_double << ", "
<< automatic_string << std::endl;
std::cout << "Numbers: ";
for (const auto& num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
enum Color {
RED,
GREEN,
BLUE
};
struct Point {
int x;
int y;
};
enum class Status {
SUCCESS,
FAILURE,
PENDING
};
union Data {
int integer;
float floating;
char character;
};
typedef unsigned long ulong;
typedef Point* PointPtr;
using std::cout;
using std::endl;
using std::vector;
using StringVector = std::vector<std::string>;
using IntPtr = std::unique_ptr<int>;
class OperatorExample {
private:
int value_;
public:
explicit OperatorExample(int val) : value_(val) {}
OperatorExample(const OperatorExample& other) : value_(other.value_) {}
~OperatorExample() {
}
OperatorExample operator+(const OperatorExample& other) const {
return OperatorExample(value_ + other.value_);
}
OperatorExample& operator=(const OperatorExample& other) {
if (this != &other) {
value_ = other.value_;
}
return *this;
}
bool operator==(const OperatorExample& other) const {
return value_ == other.value_;
}
friend std::ostream& operator<<(std::ostream& os, const OperatorExample& obj) {
os << obj.value_;
return os;
}
int getValue() const { return value_; }
};
void template_instantiation_examples() {
std::vector<int> int_vector;
std::vector<std::string> string_vector;
std::unique_ptr<geometry::Circle> circle_ptr = std::make_unique<geometry::Circle>(10.0);
std::unique_ptr<geometry::Rectangle> rect_ptr = std::make_unique<geometry::Rectangle>(5.0, 10.0);
auto max_int = utils::max<int>(10, 20);
auto max_double = utils::max<double>(3.14, 2.71);
}
void call_expression_examples() {
demonstrate_lambdas();
demonstrate_exceptions(true);
geometry::Circle circle(5.0);
double area = circle.area();
double perimeter = circle.perimeter();
circle.display();
geometry::Circle* circle_ptr = new geometry::Circle(3.0);
double ptr_area = circle_ptr->area();
circle_ptr->display();
delete circle_ptr;
int max_val = utils::max(10, 20);
int specialized = utils::max<int>(30, 40);
std::string str = "test";
std::vector<int> vec;
std::cout << "Using scoped_identifier" << std::endl;
std::make_unique<int>(42);
geometry::Circle::radius();
std::unique_ptr<geometry::Shape> shape = std::make_unique<geometry::Circle>(7.0);
shape->display();
std::cout << "Test output" << std::endl;
}
class TestClass {
public:
void methodA();
void methodB() const;
static void staticMethod();
};
void TestClass::methodA() {
std::cout << "TestClass::methodA implementation" << std::endl;
}
void TestClass::methodB() const {
std::cout << "TestClass::methodB const implementation" << std::endl;
}
void TestClass::staticMethod() {
std::cout << "TestClass::staticMethod implementation" << std::endl;
}