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

We have seen that classes have several special member functions. One of them is:

ID: 3799213 • Letter: W

Question

We have seen that classes have several special member functions. One of them is:

Default constructor: A constructor is a class member function that is automatically called immediately after memory is allocated for an object. The constructor's job is to initialize the object's members. A default constructor is a version of a constructor that can be invoked without arguments. The default constructor is automatically called when an object is defined as in MyClass obj; or allocated via the new operator as in objptr = new MyClass;.

If the programmer doesn't define a default constructor for a class, the compiler implicitly defines one having no statements, meaning the constructor does nothing.

Good practice is always initializing variables, a programmer should similarly be explicit in defining a default constructor for a class, making sure to initialize each member.

Additional special member functions are:

Destructor: A destructor is a class member function that is automatically called when an object of the class is destroyed, as when the object goes out of scope or is explicitly destroyed as in delete obj;.

If the programmer doesn't define a destructor for a class, the compiler implicitly defines one having no statements, meaning the destructor does nothing.

Copy constructor: A copy constructor is another version of a constructor that can be called with a single pass by reference argument. The copy constructor is automatically called when an object is passed by value to a function such as for the function SomeFunction(MyClass localObj) and the call SomeFunction(anotherObj), when an object is initialized when defined such as MyClass classObj1 = classObj2;, or when an object is initialized when allocated via "new" as in obj1Ptr = new MyClass(classObj2);.

If the programmer doesn't define a copy constructor for a class, then the compiler implicitly defines one whose statements do a memberwise copy, i.e., classObj2.memberVal1 = classObj1.memberVal1, classObj2.memberVal2 = classObj1.memberVal2, etc.

Copy assignment operator: The assignment operator "=" can be overloaded for a class via a member function, known as the copy assignment operator, that overloads the built-in function "operator=", the member function having a reference parameter of the class type and returning a reference to the class type.

If the programmer doesn't define a copy assignment operator, the compiler implicitly defines one that does a memberwise copy.

For each of those three special member functions, the implicitly-defined behavior is often sufficient. However, for some cases such as when a class has a pointer member and the default constructor allocates memory for that member, then the programmer likely needs to explicitly define the behavior for all three of those special member functions.

The rule of three describes a practice that if a programmer explicitly defines any one of those three special member functions (destructor, copy constructor, copy assignment operator), then the programmer should explicitly define all three. For this reason, those three special member functions are sometimes called the big three.

A good practice is to always follow the rule of three and define the big three (destructor, copy constructor, copy assignment operator) if any one of these functions are defined.

Please write examples for each of the big three(destructor, copy constructor, copy assignment operator) with an explanation of how to write them. Write an example for each of these functions with comments explaining what is being done every line and why. These comments must explicitly explain each line of code step by step.

Explanation / Answer

1) Destructor: The following is an example code for a destructor.

#include <iostream>

class StraightLine {
public:
void setLength( double len );
double getLength( void );
StraightLine(); // This is the constructor for the StraightLine class
~StraightLine(); // This is the destructor for the StraightLine class

private:
double length;
};

// Definition of member fuctions of the StraightLine class

// Constructor

StraightLine::StraightLine(void) {
cout << "Object is being created" << endl;
}

// Destructor

StraightLine::~StraightLine(void) {
cout << "Object is being deleted" << endl;
}

// setLength method
void StraightLine::setLength( double len ) {
length = len;
}

// getLength method
double StraightLine::getLength( void ) {
return length;
}

// Main fmethod
int main( ) {
StraightLine line;

// set the line's length
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;

return 0;
}

2) Copy Constructor: The following is an example code for a copy constructor.

#include <iostream>


class StraightLine {
public:
int getLength( void );
StraightLine( int len ); // simple constructor
StraightLine( const StraightLine &obj); // copy constructor
~StraightLine(); // destructor

private:
int *ptr;
};

// Member functions

// Constructor
StraightLine::StraightLine(int len) {
cout << "Normal constructor allocating ptr" << endl;
  
// allocate memory for the pointer;
ptr = new int;
*ptr = len;
}

// Copy Constructor
StraightLine::StraightLine(const StraightLine &obj) {
cout << "Copy constructor allocating ptr." << endl;
ptr = new int;
*ptr = *obj.ptr; // copy the value
}

// Destructor
StraightLine::~StraightLine(void) {
cout << "Freeing memory!" << endl;
delete ptr;
}

// getLength method
int StraightLine::getLength( void ) {
return *ptr;
}

// display method
void display(StraightLine obj) {
cout << "Length of line : " << obj.getLength() <<endl;
}

// Main method
int main( ) {
StraightLine line(10);

display(line);

return 0;
}

3) Copy Assignment Operator: The following is an example code for explaing copy assignment operator, popularly known as assignment operator overloading. Once the assignment operator (or any other operator) is overloaded, it can be used to create objects similar to the copy constructor.

#include <iostream>

class Length {
private:
int ft; // 0 to infinite
int inch; // 0 to 12
public:
// required constructors
Length(){
ft = 0;
inch = 0;
}
      
Length(int f, int i){
ft = f;
inch = i;
}
      
void operator = (const Length &L ) {
ft = L.ft;
inch = L.inch;
}
      
// method to display length
void displayLength() {
cout << "F: " << ft << " I:" << inch << endl;
}
  
};

int main() {
Length L1(11, 10), L2(5, 11);

cout << "First Length : ";
L1.displayLength();
cout << "Second Length :";
L2.displayLength();

// use assignment operator
L1 = L2;
cout << "First Length :";
L1.displayLength();

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote