module 2 Flashcards
(14 cards)
Write the matching destructor for this constructor:
T* p = new T;
delete p;
Write the matching destructor for this constructor:
T* p = new T[n];
delete []p;
Write an overloaded operator new(size_t)
using the <cstdlib>
#include <cstddef> #include <cstdlib> using namespace std; void* operator new(size_t data_size) { void* p = malloc(data_size); return p; }
Write an overloaded operator delete(void*)
using the <cstdlib>
#include <cstdlib> void operator delete(void* data_ptr) { free(data_ptr); }
Write a function called punning()
that takes an int
as a parameter and outputs the bytes using pointer punning.
view the bytes with a decimal base
#include <iostream> using namespace std; void punning(int num) { char* p = reinterpret_cast<char*>(&num); for (int i = 0; i < sizeof(int); ++i) { cout << int(p[i]) << " "; } }
Overload the operator new()
and operator delete()
as non-public class allocation funcitons in class C
to disallow allocating objects on the heap.
class C { private: void* operator new(size_t){ return nullptr; } void operator delete(void*) {} };
What is the output of the following code?
#include <iostream> using namespace std; int counter() { static int count{0}; return ++count; } int main() { for (int i = 0; i < 5; ++i) { cout << counter() << ' '; } }
1 2 3 4 5
Write a simple fibonacci function
#include <iostream> using namespace std; int fibonacci() { static int cur{0}; static int next{1}; int result = cur; int temp = next; next += cur; cur = temp; return result; }
What is the output of the following code?
#include <iostream> using namespace std; class Member1 { public: Member1() {cout << "Constructing a Member1 object\n";} }; class Member2 { public: Member2() {cout << "Constructing a Member2 object\n";} }; class Wrapper { Member2 mem2; Member1 mem1; public: Wrapper() {cout << "Constructing a Wrapper\n";} }; int main() { Wrapper w; }
Constructing a Member2 object Constructing a Member1 object Constructing a Wrapper
Write a deep copy constructor for the following class:
#include <cstring> using namespace std; class String { char* data; public: String(const char* s = "") { data = new char[strlen(s) + 1]; strcpy(data,s); } ~String() {delete [] data;} };
String(const String& s) { data = new char[strlen(s.data) + 1]; strcpy(data, s.data); }
What is the compiler-generated Copy Constructor for the following class?
#include <cstring> using namespace std; class String { char* data; public: String(const char* s = "") { data = new char[strlen(s) + 1]; strcpy(data,s); } ~String() {delete [] data;} };
String(const String& s) : data(s.data) {}
a shallow copy
What is the compiler-generated assignment operator=
for the following class:
#include <cstring> using namespace std; class String { char* data; public: String(const char* s = "") { data = new char[strlen(s) + 1]; strcpy(data,s); } ~String() {delete [] data;} String(const String& s) { data = new char[strlen(s.data) + 1]; strcpy(data, s.data); } };
String& String::operator=(const String& s) { data = s.data; return *this; }
Correctly overwrite the assignment operator=
for the following class:
#include <cstring> using namespace std; class String { char* data; public: String(const char* s = "") { data = new char[strlen(s) + 1]; strcpy(data,s); } ~String() {delete [] data;} String(const String& s) { data = new char[strlen(s.data) + 1]; strcpy(data, s.data); } };
String& operator=(const String& s) { if (&s != this) { char* new_data = new char[strlen(s.data) + 1]; strcpy(new_data, s.data); delete [] data; data = new_data; } return *this; }
Define a struct that will keep the Synthesized Default Constructor
struct S { S() = default; }