File size: 3,281 Bytes
93d826e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <memory>
#include <vector>
#include <string>
#include <functional>

// Global variables at file scope
int globalInteger = 42;
const double PI = 3.14159;
static std::string globalString = "Global static string";

// Global enum and constant
enum Color { RED, GREEN, BLUE };
constexpr int MAX_SIZE = 100;

// Template class
template<typename T>
class Container {
public:
    static T defaultValue;  // Static class member
    const T maxCapacity = MAX_SIZE;  // Class constant
    void add(T item) { items.push_back(item); }
    const std::vector<T>& getItems() const { return items; }
private:
    std::vector<T> items;
};

// Static member definition
template<typename T>
T Container<T>::defaultValue = T();

// Abstract base class
class Animal {
public:
    virtual ~Animal() = default;
    virtual void makeSound() const = 0;
protected:
    std::string name;
};

// Derived class with virtual inheritance
class Dog : virtual public Animal {
public:
    Dog(const std::string& dogName) { name = dogName; }
    void makeSound() const override {
        std::cout << name << " says: Woof!" << std::endl;
    }
};

// Namespace example
namespace Utils {
    // Namespace-level variables
    inline int counter = 0;
    const std::string VERSION = "1.0.0";
    
    // Function template
    template<typename T>
    T max(T a, T b) {
        return (a > b) ? a : b;
    }

    // Lambda function stored in variable
    const auto printer = [](const std::string& msg) {
        std::cout << "Message: " << msg << std::endl;
    };
}

// Smart pointer and move semantics example
class Resource {
public:
    Resource(const std::string& data) : data_(data) {
        std::cout << "Resource constructed" << std::endl;
    }
    ~Resource() {
        std::cout << "Resource destroyed" << std::endl;
    }
    Resource(Resource&& other) noexcept : data_(std::move(other.data_)) {}
    std::string getData() const { return data_; }
private:
    std::string data_;
};

// Static member variable and function
class Counter {
public:
    static int getCount() { return count; }
    Counter() { ++count; }
    ~Counter() { --count; }
private:
    static int count;
};
int Counter::count = 0;

// Friend function example
class Box {
    friend std::ostream& operator<<(std::ostream& os, const Box& box);
public:
    Box(int w, int h) : width(w), height(h) {}
private:
    int width;
    int height;
};

std::ostream& operator<<(std::ostream& os, const Box& box) {
    return os << "Box(" << box.width << "x" << box.height << ")";
}

// Main function
int main() {
    // Smart pointer usage
    auto resource = std::make_unique<Resource>("Hello");
    std::cout << resource->getData() << std::endl;

    // Template class usage
    Container<int> numbers;
    numbers.add(1);
    numbers.add(2);

    // Polymorphism
    std::unique_ptr<Animal> dog = std::make_unique<Dog>("Rex");
    dog->makeSound();

    // Template function
    std::cout << "Max: " << Utils::max(10, 20) << std::endl;

    // Lambda function
    Utils::printer("Hello from lambda!");

    // Counter static example
    Counter c1, c2;
    std::cout << "Count: " << Counter::getCount() << std::endl;

    // Friend function
    Box box(10, 20);
    std::cout << box << std::endl;

    return 0;
}