C++ Flashcards

(200 cards)

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
What is the Rule of Three in C++?
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; } }; ```
26
Explain the difference between `new` and `malloc` in C++.
`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 ```
27
What is RAII (Resource Acquisition Is Initialization)?
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(); } }; ```
28
What is the difference between shallow copy and deep copy in C++?
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 }; ```
29
What is SFINAE (Substitution Failure Is Not An Error) in C++?
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 "; } ```
30
What is the difference between `std::vector` and `std::array`?
`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}; ```
31
What is the Rule of Three in C++?
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; } }; ```
32
Explain the difference between `new` and `malloc` in C++.
`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 ```
33
What is RAII (Resource Acquisition Is Initialization)?
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(); } }; ```
34
What is the difference between shallow copy and deep copy in C++?
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 }; ```
35
What is SFINAE (Substitution Failure Is Not An Error) in C++?
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 "; } ```
36
What is the difference between `std::vector` and `std::array`?
`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}; ```
37
What is the Rule of Three in C++?
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; } }; ```
38
Explain the difference between `new` and `malloc` in C++.
`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 ```
39
What is RAII (Resource Acquisition Is Initialization)?
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(); } }; ```
40
What is the difference between shallow copy and deep copy in C++?
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 }; ```
41
What is SFINAE (Substitution Failure Is Not An Error) in C++?
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 "; } ```
42
What is the difference between `std::vector` and `std::array`?
`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}; ```
43
What is the Rule of Three in C++?
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; } }; ```
44
Explain the difference between `new` and `malloc` in C++.
`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 ```
45
What is RAII (Resource Acquisition Is Initialization)?
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(); } }; ```
46
What is the difference between shallow copy and deep copy in C++?
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 }; ```
47
What is SFINAE (Substitution Failure Is Not An Error) in C++?
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 "; } ```
48
What is the difference between `std::vector` and `std::array`?
`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}; ```
49
What is the Rule of Three in C++?
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; } }; ```
50
Explain the difference between `new` and `malloc` in C++.
`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 ```
51
What is RAII (Resource Acquisition Is Initialization)?
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(); } }; ```
52
What is the difference between shallow copy and deep copy in C++?
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 }; ```
53
What is SFINAE (Substitution Failure Is Not An Error) in C++?
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 "; } ```
54
What is the difference between `std::vector` and `std::array`?
`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}; ```
55
What is the Rule of Three in C++?
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; } }; ```
56
Explain the difference between `new` and `malloc` in C++.
`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 ```
57
What is RAII (Resource Acquisition Is Initialization)?
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(); } }; ```
58
What is the difference between shallow copy and deep copy in C++?
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 }; ```
59
What is SFINAE (Substitution Failure Is Not An Error) in C++?
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 "; } ```
60
What is the difference between `std::vector` and `std::array`?
`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}; ```
61
What is the Rule of Three in C++?
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; } }; ```
62
Explain the difference between `new` and `malloc` in C++.
`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 ```
63
What is RAII (Resource Acquisition Is Initialization)?
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(); } }; ```
64
What is the difference between shallow copy and deep copy in C++?
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 }; ```
65
What is SFINAE (Substitution Failure Is Not An Error) in C++?
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 "; } ```
66
What is the difference between `std::vector` and `std::array`?
`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}; ```
67
What is the Rule of Three in C++?
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; } }; ```
68
Explain the difference between `new` and `malloc` in C++.
`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 ```
69
What is RAII (Resource Acquisition Is Initialization)?
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(); } }; ```
70
What is the difference between shallow copy and deep copy in C++?
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 }; ```
71
What is SFINAE (Substitution Failure Is Not An Error) in C++?
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 "; } ```
72
What is the difference between `std::vector` and `std::array`?
`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}; ```
73
What is the Rule of Three in C++?
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; } }; ```
74
Explain the difference between `new` and `malloc` in C++.
`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 ```
75
What is RAII (Resource Acquisition Is Initialization)?
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(); } }; ```
76
What is the difference between shallow copy and deep copy in C++?
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 }; ```
77
What is SFINAE (Substitution Failure Is Not An Error) in C++?
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 "; } ```
78
What is the difference between `std::vector` and `std::array`?
`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}; ```
79
What is the Rule of Three in C++?
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; } }; ```
80
Explain the difference between `new` and `malloc` in C++.
`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 ```
81
What is RAII (Resource Acquisition Is Initialization)?
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
What is the difference between shallow copy and deep copy in C++?
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
What is SFINAE (Substitution Failure Is Not An Error) in C++?
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
What is the difference between `std::vector` and `std::array`?
`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
What is the Rule of Three in C++?
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
Explain the difference between `new` and `malloc` in C++.
`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
What is RAII (Resource Acquisition Is Initialization)?
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
What is the difference between shallow copy and deep copy in C++?
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
What is SFINAE (Substitution Failure Is Not An Error) in C++?
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
What is the difference between `std::vector` and `std::array`?
`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
What is the Rule of Three in C++?
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
Explain the difference between `new` and `malloc` in C++.
`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
What is RAII (Resource Acquisition Is Initialization)?
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
What is the difference between shallow copy and deep copy in C++?
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
What is SFINAE (Substitution Failure Is Not An Error) in C++?
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
What is the difference between `std::vector` and `std::array`?
`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
What is the Rule of Three in C++?
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
Explain the difference between `new` and `malloc` in C++.
`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
What is RAII (Resource Acquisition Is Initialization)?
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
What is the difference between shallow copy and deep copy in C++?
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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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
What is the difference between a deep copy and a shallow copy in C++?
**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
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
**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
Explain the memory layout of a C++ object.
**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
How does dynamic_cast work internally?
**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 ```