C++ Codes - Basic to Advanced 💻
الذهاب إلى القناة على Telegram
💡 Daily C++ Codes with Output & Logic Any queries, message 👉 @sai7981 🤖 Get instant code explanations: @cpp_codes_bot 🔗 Join now: @cpp_code_snippets #cpp #dsa #coding #programming
إظهار المزيد3 188
المشتركون
لا توجد بيانات24 ساعات
+357 أيام
+12030 أيام
أرشيف المشاركات
Demonstrate bitwise AND, OR, XOR of two integers
#include <iostream>
int main() {
unsigned int a = 5;
unsigned int b = 3;
unsigned int andResult = a & b;
unsigned int orResult = a | b;
unsigned int xorResult = a ^ b;
std::cout << "a = " << a << std::endl;
std::cout << "b = " << b << std::endl;
std::cout << "a & b = " << andResult << std::endl;
std::cout << "a | b = " << orResult << std::endl;
std::cout << "a ^ b = " << xorResult << std::endl;
return 0;
}Check if a character is a vowel or consonant
#include <iostream>
#include <vector>
#include <stdexcept>
template <typename T>
class CircularBuffer {
private:
std::vector<T> buffer;
size_t head;
size_t tail;
size_t capacity;
size_t size;
public:
CircularBuffer(size_t capacity) : capacity(capacity), size(0), head(0), tail(0), buffer(capacity) {}
void enqueue(T value) {
if (isFull()) {
throw std::runtime_error("Buffer is full");
}
buffer[tail] = value;
tail = (tail + 1) % capacity;
size++;
}
T dequeue() {
if (isEmpty()) {
throw std::runtime_error("Buffer is empty");
}
T value = buffer[head];
head = (head + 1) % capacity;
size--;
return value;
}
bool isEmpty() const {
return size == 0;
}
bool isFull() const {
return size == capacity;
}
size_t getSize() const {
return size;
}
size_t getCapacity() const {
return capacity;
}
};
int main() {
CircularBuffer<int> cb(3);
cb.enqueue(1);
cb.enqueue(2);
cb.enqueue(3);
std::cout << cb.dequeue() << std::endl;
std::cout << cb.dequeue() << std::endl;
cb.enqueue(4);
cb.enqueue(5);
std::cout << cb.dequeue() << std::endl;
std::cout << cb.dequeue() << std::endl;
std::cout << cb.dequeue() << std::endl;
return 0;
}Demonstrate bitwise AND, OR, XOR of two integers
#include <iostream>
class Singleton {
private:
static Singleton* instance;
Singleton() {}
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
public:
static Singleton* getInstance() {
if (!instance) {
instance = new Singleton();
}
return instance;
}
void someFunction() {
std::cout << "Singleton instance is doing something!" << std::endl;
}
};
Singleton* Singleton::instance = nullptr;
int main() {
Singleton* s1 = Singleton::getInstance();
Singleton* s2 = Singleton::getInstance();
s1->someFunction();
if (s1 == s2) {
std::cout << "Both instances are the same!" << std::endl;
}
return 0;
}Check if a character is a vowel or consonant
#include <iostream>
#include <memory>
class MyClass {
public:
MyClass(int value) : value_(value) {
std::cout << "MyClass constructor called with value: " << value_ << std::endl;
}
~MyClass() {
std::cout << "MyClass destructor called with value: " << value_ << std::endl;
}
void printValue() const {
std::cout << "Value: " << value_ << std::endl;
}
private:
int value_;
};
int main() {
// Creating a unique pointer
std::unique_ptr<MyClass> ptr1 = std::make_unique<MyClass>(10);
// Accessing the object through the unique pointer
ptr1->printValue();
// Transferring ownership
std::unique_ptr<MyClass> ptr2 = std::move(ptr1);
// ptr1 is now null
if (ptr1 == nullptr) {
std::cout << "ptr1 is now null" << std::endl;
}
// ptr2 now owns the object
ptr2->printValue();
// Object will be deleted when ptr2 goes out of scope
return 0;
}Demonstrate bitwise AND, OR, XOR of two integers
#include <iostream>
#include <fstream>
#include <memory>
class FileHandler {
private:
std::ofstream fileStream;
std::string filename;
public:
FileHandler(const std::string& filename) : filename(filename), fileStream(filename) {
if (!fileStream.is_open()) {
throw std::runtime_error("Could not open file: " + filename);
}
std::cout << "File opened: " << filename << std::endl;
}
~FileHandler() {
if (fileStream.is_open()) {
fileStream.close();
std::cout << "File closed: " << filename << std::endl;
}
}
void writeToFile(const std::string& data) {
fileStream << data << std::endl;
}
// Disable copy constructor and assignment operator
FileHandler(const FileHandler&) = delete;
FileHandler& operator=(const FileHandler&) = delete;
};
int main() {
try {
FileHandler myFile("example.txt");
myFile.writeToFile("This is the first line.");
myFile.writeToFile("This is the second line.");
// Simulate an exception to test RAII
// throw std::runtime_error("Simulated error");
} catch (const std::exception& e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
return 1;
}
std::cout << "Program completed successfully." << std::endl;
return 0;
}Check if a character is a vowel or consonant
#include <iostream>
#include <memory>
class MyClass {
public:
MyClass(int value) : value_(value) {
std::cout << "MyClass constructor called with value: " << value_ << std::endl;
}
~MyClass() {
std::cout << "MyClass destructor called with value: " << value_ << std::endl;
}
void printValue() const {
std::cout << "Value: " << value_ << std::endl;
}
private:
int value_;
};
int main() {
// Creating a unique pointer
std::unique_ptr<MyClass> ptr1 = std::make_unique<MyClass>(10);
// Accessing the object through the unique pointer
ptr1->printValue();
// Transferring ownership
std::unique_ptr<MyClass> ptr2 = std::move(ptr1);
// ptr1 is now null
if (ptr1 == nullptr) {
std::cout << "ptr1 is now null" << std::endl;
}
// ptr2 now owns the object
ptr2->printValue();
// Object will be deleted when ptr2 goes out of scope
return 0;
}Check if a year is a leap year
#include <iostream>
#include <fstream>
#include <memory>
class FileWrapper {
private:
std::ofstream file;
std::string filename;
public:
FileWrapper(const std::string& filename) : filename(filename), file(filename) {
if (!file.is_open()) {
throw std::runtime_error("Could not open file: " + filename);
}
std::cout << "File opened: " << filename << std::endl;
}
~FileWrapper() {
if (file.is_open()) {
file.close();
std::cout << "File closed: " << filename << std::endl;
}
}
void write(const std::string& data) {
file << data << std::endl;
}
// Disallow copy and assignment
FileWrapper(const FileWrapper&) = delete;
FileWrapper& operator=(const FileWrapper&) = delete;
FileWrapper(FileWrapper&&) = default;
FileWrapper& operator=(FileWrapper&&) = default;
};
int main() {
try {
FileWrapper myFile("example.txt");
myFile.write("This is the first line.");
myFile.write("This is the second line.");
// Simulate an exception
// throw std::runtime_error("Something went wrong!");
} catch (const std::exception& e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
// File will be automatically closed when myFile goes out of scope, even if an exception occurs.
std::cout << "Program finished." << std::endl;
return 0;
}Check if a number is positive, negative or zero
#include <iostream>
int main() {
int number;
std::cin >> number;
if (number > 0) {
std::cout << "Positive" << std::endl;
} else if (number < 0) {
std::cout << "Negative" << std::endl;
} else {
std::cout << "Zero" << std::endl;
}
return 0;
}Compute quotient and remainder using / and %
#include <iostream>
int main() {
int dividend = 25;
int divisor = 4;
int quotient = dividend / divisor;
int remainder = dividend % divisor;
std::cout << "Quotient: " << quotient << std::endl;
std::cout << "Remainder: " << remainder << std::endl;
return 0;
}Calculate area of circle using constant PI
#include <iostream>
#include <cmath>
const double PI = 3.14159265358979323846;
double calculateCircleArea(double radius) {
return PI * std::pow(radius, 2);
}
int main() {
double radius;
std::cout << "Enter the radius of the circle: ";
std::cin >> radius;
if (radius < 0) {
std::cerr << "Error: Radius cannot be negative." << std::endl;
return 1;
}
double area = calculateCircleArea(radius);
std::cout << "The area of the circle is: " << area << std::endl;
return 0;
}