C++ Codes - Basic to Advanced 💻
Ir al canal en 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
Mostrar más3 188
Suscriptores
Sin datos24 horas
+357 días
+12030 días
Archivo de publicaciones
Read two integers and print their sum
#include <iostream>
int main() {
int a, b;
std::cin >> a >> b;
std::cout << a + b << std::endl;
return 0;
}Print Hello World
#include <iostream>
#include <memory>
class MyClass {
public:
MyClass(int value) : data(value) { std::cout << "MyClass created with value: " << data << std::endl; }
~MyClass() { std::cout << "MyClass destroyed with value: " << data << std::endl; }
void printData() { std::cout << "Data: " << data << std::endl; }
private:
int data;
};
int main() {
std::unique_ptr<MyClass> ptr = std::make_unique<MyClass>(42);
ptr->printData();
if (ptr) {
std::cout << "Pointer is not null" << std::endl;
}
std::unique_ptr<MyClass> ptr2 = std::move(ptr);
if (!ptr) {
std::cout << "Pointer is now null after move" << std::endl;
}
if (ptr2) {
ptr2->printData();
}
return 0;
}Read a character and print its ASCII value
#include <iostream>
int main() {
char ch;
std::cin >> ch;
int asciiValue = static_cast<int>(ch);
std::cout << asciiValue << std::endl;
return 0;
}Read two integers and print their sum
#include <iostream>
int main() {
int a, b;
std::cin >> a >> b;
std::cout << a + b << std::endl;
return 0;
}Print Hello World
#include <iostream>
#include <memory>
class Resource {
public:
Resource(int value) : data(value) { std::cout << "Resource acquired: " << data << std::endl; }
~Resource() { std::cout << "Resource released: " << data << std::endl; }
int data;
};
int main() {
std::unique_ptr<Resource> ptr1(new Resource(10));
std::unique_ptr<Resource> ptr2 = std::make_unique<Resource>(20);
if (ptr1) {
std::cout << "ptr1's resource value: " << ptr1->data << std::endl;
}
std::unique_ptr<Resource> ptr3 = std::move(ptr1);
if (!ptr1) {
std::cout << "ptr1 is now null." << std::endl;
}
if (ptr3) {
std::cout << "ptr3's resource value: " << ptr3->data << std::endl;
}
return 0;
}Find the size of different data types
#include <iostream>
int main() {
std::cout << "Size of int: " << sizeof(int) << " bytes" << std::endl;
std::cout << "Size of char: " << sizeof(char) << " byte" << std::endl;
std::cout << "Size of float: " << sizeof(float) << " bytes" << std::endl;
std::cout << "Size of double: " << sizeof(double) << " bytes" << std::endl;
std::cout << "Size of bool: " << sizeof(bool) << " byte" << std::endl;
std::cout << "Size of short: " << sizeof(short) << " bytes" << std::endl;
std::cout << "Size of long: " << sizeof(long) << " bytes" << std::endl;
std::cout << "Size of long long: " << sizeof(long long) << " bytes" << std::endl;
std::cout << "Size of int*: " << sizeof(int*) << " bytes" << std::endl;
return 0;
}Swap two numbers without using a third variable
#include <iostream>
int main() {
int a = 10;
int b = 5;
a = a + b;
b = a - b;
a = a - b;
std::cout << "a = " << a << std::endl;
std::cout << "b = " << b << std::endl;
return 0;
}Read a character and print its ASCII value
#include <iostream>
int main() {
char ch;
std::cin >> ch;
std::cout << static_cast<int>(ch) << std::endl;
return 0;
}Sum of Two Integers
#include <iostream>
int main() {
int a, b;
std::cin >> a >> b;
std::cout << a + b << std::endl;
return 0;
}Smart Pointers (std::unique_ptr)
#include <iostream>
#include <memory>
class MyClass {
public:
MyClass(int value) : data(value) { std::cout << "MyClass constructed with " << data << std::endl; }
~MyClass() { std::cout << "MyClass destructed with " << data << std::endl; }
int getData() const { return data; }
private:
int data;
};
int main() {
std::unique_ptr<MyClass> ptr1 = std::make_unique<MyClass>(10);
std::cout << "Value inside ptr1: " << ptr1->getData() << std::endl;
std::unique_ptr<MyClass> ptr2 = std::move(ptr1);
if (ptr1 == nullptr) {
std::cout << "ptr1 is now null" << std::endl;
}
std::cout << "Value inside ptr2: " << ptr2->getData() << std::endl;
return 0;
}Sum of Two Integers in C++
#include <iostream>
int main() {
int a, b;
std::cin >> a >> b;
std::cout << a + b << std::endl;
return 0;
}