Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Define the class Extensions that holds a list of four digit phone extensions

ID: 3857903 • Letter: C

Question

C++

Define the class Extensions that holds a list of four digit phone extensions. The class should have a private data member extn that is an integer pointer, and a second data member size that is also an integer. The class should have a default constructor and a constructor with parameters. The default constructor should initialize the private data member size to number 10 and initialize extn by dynamically allocating enough memory shown in the value of size (which is 10). The constructor with parameter should have an integer parameter that is used to initialize the data member size and use this number to dynamically allocate enough memory for the private date member extn.

Include a copy constructor for this class

Include a assignment operator for this class

Include a destructor for this class

Explanation / Answer

PROGRAM CODE:

#include <iostream>

using namespace std;

//Extensions class

class Extensions

{

//private memebers - extn and size

private:

int *extn;

int size;

public:

//default constructor

Extensions()

{

size = 10;

extn = new int[size];

}

//parameterised constructor

Extensions(int aSize)

{

size = aSize;

extn = new int[size];

}

//copy constructor

Extensions(Extensions &other)

{

size = other.size;

extn = new int[size];

for(int i=0; i<other.size; i++)

extn[i] = other.extn[i];

}

//assignment operator

void operator = (const Extensions &e )

{

size = e.size;

extn = new int[size];

for(int i=0; i<e.size; i++)

extn[i] = e.extn[i];

}

//destructor

~Extensions()

{

size = 0;

delete extn;

}

};

// main function for testing

int main() {

Extensions extnIndia(3);

Extensions extnUS(5);

extnUS = extnIndia;

return 0;

}