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

cpp//4 2- What is a destructor and how and when it is called? 3- What safe empty

ID: 3820133 • Letter: C

Question

cpp//4

2- What is a destructor and how and when it is called?

3- What safe empty state?

4- Having the following class fully implemented: (you can copy the code for mark from name.cpp)

5- Add Two constructors: A- No argument that puts the Name in a safe empty state (It is your call to decide what is a safe empty state for a Mark) B- A 2 argument constructor that sets a Name. C- Add a method calls isEmpty() that returns a Boolean. It returns true if the Mark is in a safe empty state. D- A display function that displays the full name.

   class Name

{    char buffer[51];   

char m_first[21];   

char m_last[31];

public:

   void firstname(const char* value); // sets the name   

void lastname(const char* value); // sets the last name   

const char* fullname(); // returns a string containing full name };

Explanation / Answer

2.

A destructor as the name suggests (opposite of constructor) is a special member function which is called when an object's life ends. It is used to free any resources locked by object in its lifetime.

Syntax : ( ~ sign is used for destructor)

It doesn't take any parameter, not return anything and no return data type

~ <class_name>()

{

}

3.

A constructor ensures well defined state of object at the time of its creation thus ensuring safe empty state, until setters change that state.If member functions are called on objects in safe empty states, behaviour works as expected

4.

class Name

{   

char buffer[51];   

char m_first[21];   

char m_last[31];

public void firstname(const char* value) // sets the name   

{

this.m_first

}

public void lastname(const char* value) // sets the last name   

{

this.m_last=value;

}

public const char* fullname() // returns a string containing full name };

{

strcpy(buffer, m_first);
strcat(buffer, mlast);

return buffer;

}