C++ Flashcards

1
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is SFINAE (Substitution Failure Is Not An Error) in C++?

A

SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Integral type
";
}

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Non-integral type
";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between std::vector and std::array?

A

std::vector is a dynamic array with size management, while std::array is a fixed-size array.

std::vector vec = {1, 2, 3};
std::array arr = {1, 2, 3};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is SFINAE (Substitution Failure Is Not An Error) in C++?

A

SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Integral type
";
}

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Non-integral type
";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between std::vector and std::array?

A

std::vector is a dynamic array with size management, while std::array is a fixed-size array.

std::vector vec = {1, 2, 3};
std::array arr = {1, 2, 3};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is SFINAE (Substitution Failure Is Not An Error) in C++?

A

SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Integral type
";
}

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Non-integral type
";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the difference between std::vector and std::array?

A

std::vector is a dynamic array with size management, while std::array is a fixed-size array.

std::vector vec = {1, 2, 3};
std::array arr = {1, 2, 3};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is SFINAE (Substitution Failure Is Not An Error) in C++?

A

SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Integral type
";
}

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Non-integral type
";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the difference between std::vector and std::array?

A

std::vector is a dynamic array with size management, while std::array is a fixed-size array.

std::vector vec = {1, 2, 3};
std::array arr = {1, 2, 3};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What is SFINAE (Substitution Failure Is Not An Error) in C++?

A

SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Integral type
";
}

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Non-integral type
";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is the difference between std::vector and std::array?

A

std::vector is a dynamic array with size management, while std::array is a fixed-size array.

std::vector vec = {1, 2, 3};
std::array arr = {1, 2, 3};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What is SFINAE (Substitution Failure Is Not An Error) in C++?

A

SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Integral type
";
}

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Non-integral type
";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

What is the difference between std::vector and std::array?

A

std::vector is a dynamic array with size management, while std::array is a fixed-size array.

std::vector vec = {1, 2, 3};
std::array arr = {1, 2, 3};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

What is SFINAE (Substitution Failure Is Not An Error) in C++?

A

SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Integral type
";
}

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Non-integral type
";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

What is the difference between std::vector and std::array?

A

std::vector is a dynamic array with size management, while std::array is a fixed-size array.

std::vector vec = {1, 2, 3};
std::array arr = {1, 2, 3};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

What is SFINAE (Substitution Failure Is Not An Error) in C++?

A

SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Integral type
";
}

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Non-integral type
";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

What is the difference between std::vector and std::array?

A

std::vector is a dynamic array with size management, while std::array is a fixed-size array.

std::vector vec = {1, 2, 3};
std::array arr = {1, 2, 3};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

What is SFINAE (Substitution Failure Is Not An Error) in C++?

A

SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Integral type
";
}

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Non-integral type
";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

What is the difference between std::vector and std::array?

A

std::vector is a dynamic array with size management, while std::array is a fixed-size array.

std::vector vec = {1, 2, 3};
std::array arr = {1, 2, 3};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

What is SFINAE (Substitution Failure Is Not An Error) in C++?

A

SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Integral type
";
}

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Non-integral type
";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

What is the difference between std::vector and std::array?

A

std::vector is a dynamic array with size management, while std::array is a fixed-size array.

std::vector vec = {1, 2, 3};
std::array arr = {1, 2, 3};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
65
Q

What is SFINAE (Substitution Failure Is Not An Error) in C++?

A

SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Integral type
";
}

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Non-integral type
";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
66
Q

What is the difference between std::vector and std::array?

A

std::vector is a dynamic array with size management, while std::array is a fixed-size array.

std::vector vec = {1, 2, 3};
std::array arr = {1, 2, 3};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
67
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
68
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
69
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
70
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
71
Q

What is SFINAE (Substitution Failure Is Not An Error) in C++?

A

SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Integral type
";
}

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Non-integral type
";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
72
Q

What is the difference between std::vector and std::array?

A

std::vector is a dynamic array with size management, while std::array is a fixed-size array.

std::vector vec = {1, 2, 3};
std::array arr = {1, 2, 3};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
73
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
74
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
75
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
76
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
77
Q

What is SFINAE (Substitution Failure Is Not An Error) in C++?

A

SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Integral type
";
}

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Non-integral type
";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
78
Q

What is the difference between std::vector and std::array?

A

std::vector is a dynamic array with size management, while std::array is a fixed-size array.

std::vector vec = {1, 2, 3};
std::array arr = {1, 2, 3};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
79
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
80
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
81
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
82
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
83
Q

What is SFINAE (Substitution Failure Is Not An Error) in C++?

A

SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Integral type
";
}

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Non-integral type
";
}
84
Q

What is the difference between std::vector and std::array?

A

std::vector is a dynamic array with size management, while std::array is a fixed-size array.

std::vector vec = {1, 2, 3};
std::array arr = {1, 2, 3};
85
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
86
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
87
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
88
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
89
Q

What is SFINAE (Substitution Failure Is Not An Error) in C++?

A

SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Integral type
";
}

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Non-integral type
";
}
90
Q

What is the difference between std::vector and std::array?

A

std::vector is a dynamic array with size management, while std::array is a fixed-size array.

std::vector vec = {1, 2, 3};
std::array arr = {1, 2, 3};
91
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
92
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
93
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
94
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
95
Q

What is SFINAE (Substitution Failure Is Not An Error) in C++?

A

SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Integral type
";
}

template
typename std::enable_if::value, void>::type
process(T val) {
    std::cout << "Non-integral type
";
}
96
Q

What is the difference between std::vector and std::array?

A

std::vector is a dynamic array with size management, while std::array is a fixed-size array.

std::vector vec = {1, 2, 3};
std::array arr = {1, 2, 3};
97
Q

What is the Rule of Three in C++?

A

The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.

class Example {
    int* data;
public:
    Example(int val) : data(new int(val)) {}
    ~Example() { delete data; }
    Example(const Example& other) : data(new int(*other.data)) {}
    Example& operator=(const Example& other) {
        if (this == &other) return *this;
        *data = *other.data;
        return *this;
    }
};
98
Q

Explain the difference between new and malloc in C++.

A

new initializes objects, calls constructors, and returns a typed pointer. malloc only allocates memory and returns a void pointer.

int* p1 = new int(5); // calls constructor
int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
99
Q

What is RAII (Resource Acquisition Is Initialization)?

A

RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.

class File {
    std::ofstream file;
public:
    File(const std::string& name) { file.open(name); }
    ~File() { file.close(); }
};
100
Q

What is the difference between shallow copy and deep copy in C++?

A

A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.

class Shallow {
    int* data;
public:
    Shallow(int val) : data(new int(val)) {}
    ~Shallow() { delete data; }
    Shallow(const Shallow& other) : data(other.data) {} // Shallow copy
};

class Deep {
    int* data;
public:
    Deep(int val) : data(new int(val)) {}
    ~Deep() { delete data; }
    Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy
};
101
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
102
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

103
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
104
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
105
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
106
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

107
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
108
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
109
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
110
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

111
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
112
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
113
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
114
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

115
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
116
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
117
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
118
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

119
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
120
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
121
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
122
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

123
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
124
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
125
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
126
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

127
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
128
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
129
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
130
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

131
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
132
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
133
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
134
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

135
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
136
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
137
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
138
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

139
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
140
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
141
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
142
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

143
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
144
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
145
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
146
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

147
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
148
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
149
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
150
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

151
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
152
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
153
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
154
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

155
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
156
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
157
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
158
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

159
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
160
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
161
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
162
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

163
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
164
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
165
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
166
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

167
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
168
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
169
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
170
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

171
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
172
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
173
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
174
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

175
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
176
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
177
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
178
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

179
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
180
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
181
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
182
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

183
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
184
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
185
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
186
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

187
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
188
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
189
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
190
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

191
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
192
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
193
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
194
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

195
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
196
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid
197
Q

What is the difference between a deep copy and a shallow copy in C++?

A

A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.

class MyClass {
    int* ptr;
public:
    MyClass(int val) {
        ptr = new int(val);
    }
    // Shallow Copy Constructor
    MyClass(const MyClass& obj) {
        ptr = obj.ptr;
    }
    // Deep Copy Constructor
    MyClass deepCopy(const MyClass& obj) {
        ptr = new int(*obj.ptr);
    }
};
198
Q

What are virtual tables (vtable) and virtual table pointers (vptr) in C++?

A

A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.

class Base {
public:
    virtual void show() { std::cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { std::cout << "Derived\n"; }
};

The vptr is set at runtime to point to the vtable of the Base or Derived class depending on the type of the object.

199
Q

Explain the memory layout of a C++ object.

A

A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.

class A {
    int x;   // 4 bytes
    char y;  // 1 byte
    // Padding of 3 bytes for alignment
};
200
Q

How does dynamic_cast work internally?

A

A:
dynamic_cast performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr for pointers or throws std::bad_cast for references.

class Base { virtual void func() {} };
class Derived : public Base {};
Base* base = new Derived();
Derived* derived = dynamic_cast(base); // Valid