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;
}
};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
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(); }
};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
};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
";
}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};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;
}
};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
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(); }
};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
};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
";
}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};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;
}
};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
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(); }
};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
};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
";
}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};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;
}
};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
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(); }
};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
};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
";
}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};