module 2 Flashcards

(14 cards)

1
Q

Write the matching destructor for this constructor:

T* p = new T;
A
delete p;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write the matching destructor for this constructor:

T* p = new T[n];
A
delete []p;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write an overloaded operator new(size_t) using the <cstdlib>

A
#include <cstddef>
#include <cstdlib>
using namespace std;
void* operator new(size_t data_size) {
     void* p = malloc(data_size);
     return p;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Write an overloaded operator delete(void*) using the <cstdlib>

A
#include <cstdlib>
void operator delete(void* data_ptr) {
     free(data_ptr);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A
#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]) << " ";
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Overload the operator new() and operator delete() as non-public class allocation funcitons in class C to disallow allocating objects on the heap.

A
class C {
private:
   void* operator new(size_t){
	      return nullptr;
    }
    void operator delete(void*) {}
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

1 2 3 4 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a simple fibonacci function

A
#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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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;
}
A
Constructing a Member2 object
Constructing a Member1 object
Constructing a Wrapper
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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;}
};
A
String(const String& s) {
      data = new char[strlen(s.data) + 1];
      strcpy(data, s.data);
 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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;}
};
A
String(const String& s) : data(s.data) {}

a shallow copy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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);
    }
};
A
String& String::operator=(const String& s) {
      data = s.data;
      return *this;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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);
    }
};
A
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;
 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Define a struct that will keep the Synthesized Default Constructor

A
struct S {
     S() = default;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly