In: Computer Science
class S {
public:
S(int init, int size, int id) :m_num(init), m_size(size)
{
m_unique = new int[m_num];
for (int i = 0; i < m_num; i++) m_unique[i] = id;
}
S() :m_num(0) { m_unique = nullptr; }
void set(int num) { m_num = num; }
int get() const { return m_num; } private:
int m_num;
int m_size;
int *m_unique;
};
S d1, d2(3,1), d3(-11,6);
S *sp1, **sp2, ***sp3;
Solutions:
a) Set sp1 to point to d1.
sp1=&d1;
b) Using sp2 change the value of m_num in d1 to the value of m_num in d2 (you may not use d1).
sp2 = &sp1;
(*sp2)->set(d2.get());
c) Using sp3 make sp1 point to d3 (you may not use sp1)
sp3 = &sp2;
**sp3 = &d3;
d) Referring to the object S in previous problem:
e) Implement a copy constructor for the object S. Assume this is done outside the class definition.
S::S(const S& var) :m_num(var.m_num), m_size(var.m_size)
{
m_unique = new int[m_num]; //dynamically alloacte memory
for (int i = 0; i < m_num; i++) //iterating till m_num
m_unique[i] = var.m_unique[i]; // initialising m_unique
}
f) Overload the assignment operator for the object S. Assume this is done outside the class definition.
S& S::operator=(const S& var) {
m_num = var.m_num; //initialising num
m_size = var.m_size; //initialising size
delete[] m_unique; // deallocating memory
m_unique = new int[m_num]; //dynamically alloacte memory
for (int i = 0; i < m_num; i++)
m_unique[i] = var.m_unique[i]; //initialising in m_inique
return *this; //return
g)Implement the destructor for S. Assume this is done outside the class definition
S::~S(void)
{
cout << "Object is being deleted" << endl;
}
NOTE:We have found that students are not providing feedback of answer please provide feedback its very precious for us .If you like the answer please upvote it .If you have any doubt let me know in comment section.I will try to answer ASAP.