| Download the amazing global Makindo app: Android | Apple | |
|---|---|
| MEDICAL DISCLAIMER: Educational use only. Not for diagnosis or management. See below for full disclaimer. |
Related Subjects: |Python |C |Java |C++ |C sharp |VB |How does a CPU work |Computer Networking |Computer Security |Concurrent Programming |Cryptography |Data Structures |Database Management
C++ is a powerful, high-performance programming language that supports procedural, object-oriented, and generic programming. It is widely used in software development for applications requiring real-time performance, such as game development, system/software development, and large-scale simulations.
// This is a single-line comment
/* This is a multi-line comment spanning multiple lines */
#include#define PI 3.14159
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
int age = 25; float height = 5.9f; double pi = 3.14159; char initial = 'A'; bool isActive = true;
if (age > 18) {
std::cout << "Adult" << std::endl;
} else {
std::cout << "Minor" << std::endl;
}
switch (initial) {
case 'A':
std::cout << "Grade A" << std::endl;
break;
case 'B':
std::cout << "Grade B" << std::endl;
break;
default:
std::cout << "Other Grade" << std::endl;
}
for (int i = 0; i < 5; i++) {
std::cout << i << std::endl;
}
int j = 0;
while (j < 5) {
std::cout << j << std::endl;
j++;
}
int k = 0;
do {
std::cout << k << std::endl;
k++;
} while (k < 5);
int add(int a, int b) {
return a + b;
}
int main() {
int sum = add(5, 3);
std::cout << "Sum: " << sum << std::endl;
return 0;
}
void greet() {
std::cout << "Hello, World!" << std::endl;
}
int main() {
greet();
return 0;
}
inline int square(int x) {
return x * x;
}
int main() {
std::cout << "Square of 5: " << square(5) << std::endl;
return 0;
}
class Dog {
public:
std::string name;
int age;
Dog(std::string name, int age) {
this->name = name;
this->age = age;
}
void bark() {
std::cout << name << " says woof!" << std::endl;
}
};
int main() {
Dog myDog("Buddy", 3);
myDog.bark();
return 0;
}
class Animal {
public:
std::string name;
void speak() {
std::cout << "Animal speaks" << std::endl;
}
};
class Dog : public Animal {
public:
void speak() {
std::cout << name << " says woof!" << std::endl;
}
};
int main() {
Dog myDog;
myDog.name = "Buddy";
myDog.speak();
return 0;
}
class Animal {
public:
virtual void speak() {
std::cout << "Animal speaks" << std::endl;
}
};
class Dog : public Animal {
public:
void speak() override {
std::cout << "Dog barks" << std::endl;
}
};
class Cat : public Animal {
public:
void speak() override {
std::cout << "Cat meows" << std::endl;
}
};
int main() {
Animal* myDog = new Dog();
Animal* myCat = new Cat();
myDog->speak(); // Output: Dog barks
myCat->speak(); // Output: Cat meows
delete myDog;
delete myCat;
return 0;
}
templateT add(T a, T b) { return a + b; } int main() { std::cout << "Sum of ints: " << add (3, 4) << std::endl; std::cout << "Sum of doubles: " << add (3.5, 4.5) << std::endl; return 0; }
templateclass Box { public: T value; Box(T value) : value(value) {} T getValue() { return value; } }; int main() { Box intBox Box doubleBox(456.78); std::cout << "Integer Box: " << intBox.getValue() << std::endl; std::cout << "Double Box: " << doubleBox.getValue() << std::endl; return 0; }
try {
int result = 10 / 0;
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
class DivisionByZeroException : public std::exception {
public:
const char* what() const noexcept override {
return "Division by zero is not allowed!";
}
};
int divide(int a, int b) {
if (b == 0) {
throw DivisionByZeroException();
}
return a / b;
}
int main() {
try {
int result = divide(10, 0);
} catch (const DivisionByZeroException& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}
#include#include int main() { std::vector numbers = {1, 2, 3, 4, 5}; numbers.push_back(6); for (int num : numbers) { std::cout << num << " "; } return 0; }
#include
#include#include int main() { std::vector numbers = {1, 2, 3, 4, 5}; for (std::vector ::iterator it = numbers.begin(); it != numbers.end(); ++it) { std::cout << *it << " "; } return 0; }
#include#include #include int main() { std::ifstream file("example.txt"); std::string line; if (file.is_open()) { while (getline(file, line)) { std::cout << line << std::endl; } file.close(); } else { std::cerr << "Unable to open file" << std::endl; } return 0; }
#include#include int main() { std::ofstream file("example.txt"); if (file.is_open()) { file << "Hello, World!" << std::endl; file.close(); } else { std::cerr << "Unable to open file" << std::endl; } return 0; }
int* ptr = new int(10);
std::cout << "Value: " << *ptr << std::endl;
delete ptr;
int* arr = new int[5];
for (int i = 0; i < 5; i++) {
arr[i] = i + 1;
}
delete[] arr;
#define PI 3.14159
#define SQUARE(x) ((x) * (x))
int main() {
std::cout << "PI: " << PI << std::endl;
std::cout << "Square of 5: " << SQUARE(5) << std::endl;
return 0;
}
#define DEBUG
int main() {
#ifdef DEBUG
std::cout << "Debug mode" << std::endl;
#else
std::cout << "Release mode" << std::endl;
#endif
return 0;
}
C++ is a powerful and versatile programming language that provides low-level access to memory and system resources. Understanding its basic syntax, control structures, functions, object-oriented principles, templates, exception handling, the Standard Template Library (STL), file I/O, and memory management is essential for developing efficient and effective programs. C++ is widely used in system programming, game development, and performance-critical applications.