In all questions involving function or method headers, be sure to think of wheth
ID: 3739648 • Letter: I
Question
In all questions involving function or method headers, be sure to think of whether function arguments and return values should be references, whether they should be const, etc. (Remember that a function header is the first line of a function definition/implementation.)
What is the keyword this?
What is a destructor?
Write the method header for the destructor for class Widget.
Write the method header for the copy constructor for class Widget.
Write the method header for the overloaded assignment operator for class Widget.
What are two situations where a copy constructor may be called?
What is the logic for an overloaded assignment operator?
Explanation / Answer
Please find the answer below:
What is the keyword this?
this’ is a constant pointer that holds the memory address of the current object.
See example below:
#include<iostream>
using namespace std;
/* local variable is same as a member's name */
class Test
{
private:
int x;
public:
void setX (int x)
{
// The 'this' pointer is used to retrieve the object's x
// hidden by the local variable 'x'
this->x = x; //it will set the value to the object that is calling this function
}
void print() { cout << "x = " << x << endl; }
};
int main()
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;
}
here output will be x = 20
What is a destructor?
Destructor is a member function which destructs or deletes an object. Whenver a program is about to finish destructor will deallocate all the memory those were using by the class varaibles.
A destructor function is called automatically when the object goes out of scope:
Destructor definition will be same as class constructor but having ~ at the start.
Write the method header for the destructor for class Widget.
class Widget
{
private:
char *s;
int size;
public:
Widget(char *); // constructor
~Widget(); // destructor
};
Widget::Widget(char *c)
{
size = strlen(c);
s = new char[size+1];
strcpy(s,c);
}
Widget::~Widget()
{
delete []s;
}
Write the method header for the copy constructor for class Widget.
class Widget
{
private:
int size;
public:
// Copy constructor
Widget(const Widget &w2) {size = w2.size; }
};
Write the method header for the overloaded assignment operator for class Widget.
class Widget
{
private:
char *s;
int size;
public:
Widget(char *c)// constructor
{
size = strlen(c);
s = new char[size+1];
strcpy(s,c);
}
~Widget()// destructor
{
delete []s;
}
Widget & operator = (const Widget &t); //declare overloaded assignment operator
};
Widget & Widget::operator = (const Widget &t)
{
// Check for self assignment
if(this != &t)
*s = *(t.s);
size = t.size
return *this;
}
What are two situations where a copy constructor may be called?
In C++, a Copy Constructor may be called in following cases:
1. When an object of the class is returned by value.
2. When an object of the class is passed (to a function) by value as an argument.
3. When an object is constructed based on another object of the same class.
What is the logic for an overloaded assignment operator?
If a class doesn’t contain pointers, then there is no need to write assignment operator and copy constructor. The compiler creates a default copy constructor and assignment operators for every class. The compiler created copy constructor and assignment operator may not be sufficient when we have pointers or any run time allocation of resource like file handle, a network connection..etc. For example, consider the following program.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.