#include <iostream> void modify(int &x) { x += 5; } int main() { int a = 10; modify(a); std::cout << "a: " << a << std::endl; return 0; }
a: 15
class Example { const int value; public: void setValue(int v) { value = v; } };
value
is marked const
but setValue attempts to modify value. Remove const
from the declaration.
#include <iostream> int main() { int *ptr = new int(42); std::cout << *ptr << std::endl; delete ptr; return 0; }
42
#include <iostream> #include <stdexcept> int main() { try { throw 10; } catch (const std::exception &e) { std::cout << "Caught std::exception: " << e.what() << std::endl; } catch (int n) { std::cout << "Caught int: " << n << std::endl; } return 0; }
The exception thrown is of type int
, so the output will be: Caught int: 10
#include <iostream> int main() { int x = 10, y = 20; int * const ptr1 = &x; // constant pointer to int const int * ptr2 = &y; // pointer to constant int // ptr1 = &y; // Error: cannot change the address stored in ptr1 *ptr1 = 15; // Allowed: modifying the value at x // *ptr2 = 25; // Error: cannot modify the value pointed to by ptr2 std::cout << x << " " << y << std::endl; return 0; }
The output is: 15 20
. This demonstrates that for ptr1
(a constant pointer), you cannot change the address it points to, but you can modify the value at that address. In contrast, for ptr2
(a pointer to constant data), you cannot modify the value pointed to, even though the pointer itself can point to a different address.
#include <iostream> class Complex { double real, imag; public: Complex(double r, double i) : real(r), imag(i) {} Complex operator+(const Complex &other) const { return Complex(real + other.real, imag + other.imag); } void print() const { std::cout << real << " + " << imag << "i" << std::endl; } }; int main(){ Complex a(1.0, 2.0), b(3.0, 4.0); Complex c = a + b; c.print(); return 0; }
The output will be: 4 + 6i
main
?
#include <iostream> template<typename T> T max(T a, T b) { return a > b ? a : b; } int main() { std::cout << max(3, 7) << std::endl; std::cout << max(3.14, 2.72) << std::endl; return 0; }
The output will be: 7
3.14
#include <iostream> class Animal { public: virtual void speak() { std::cout << "Some generic animal sound" << std::endl; } virtual ~Animal() = default; }; class Dog : public Animal { public: void speak() override { std::cout << "Woof!" << std::endl; } }; int main() { Animal* pet = new Dog(); pet->speak(); delete pet; return 0; }
Output: Woof!
Shape
be instantiated?
#include <iostream> class Shape { public: virtual double area() const = 0; // Pure virtual function }; class Circle : public Shape { double radius; public: Circle(double r) : radius(r) {} double area() const override { return 3.14159 * radius * radius; } }; int main() { // Shape s; // Uncommenting this line will cause a compile-time error Circle c(5); std::cout << c.area() << std::endl; return 0; }
Shape
is an abstract class due to the pure virtual function area()
. Abstract classes cannot be instantiated.
#include <iostream> #include <memory> class Widget { public: Widget() { std::cout << "Widget created" << std::endl; } ~Widget() { std::cout << "Widget destroyed" << std::endl; } }; int main() { std::unique_ptr<Widget> ptr = std::make_unique<Widget>(); return 0; }
Output:Widget created
Widget destroyed
use_count()
and why?
#include <iostream> #include <memory> int main(){ std::shared_ptr<int> sp = std::make_shared<int>(100); std::weak_ptr<int> wp = sp; std::cout << "Shared count: " << sp.use_count() << std::endl; return 0; }
Output: Shared count: 1
because weak_ptr
does not increase the reference count.
ls -al
Lists all files and directories (including hidden files) in the current directory in long format, showing permissions, owner, group, file size, and timestamp.
grep -r "main" .
Searches recursively (-r
) for the string "main"
in all files in the current directory.
rwxr-xr--
mean for a file?
- Owner: rwx
– read, write, and execute
- Group: r-x
– read and execute (no write)
- Others: r--
– read only
document.txt
?
chmod 644 document.txt