Substitute the variable name for the object constructed or destructed. Use a comma to separate if multiple functions are run on a line.
Need help with a C++ commenting question:
Indicate where the different constructors, assignment operator= and desctructor will run with a comment under the relevant line of code.
Start in main and use the following tags to indicate the function that will run:
Default constructor <object name>
String constructor <object name>
Copy constructor <object name>
Assignment operator
Destructor <object name>
Substitute the variable name for the object constructed or destructed. Use a comma to separate if multiple functions are run on a line.
| #include <iostream> | ||
| 2 | using namespace std; | |
| 3 | ||
| 4 | class Student { | |
| 5 | public: | |
| 6 | friend Student OutputDuplicate (Student obj); // friend function | |
| 7 | ||
| 8 | Student (); // Default constructor | |
| 9 | ||
| 10 | Student (string n); // String constructor | |
| 11 | ||
| 12 | Student (const Student & obj); // Copy constructor | |
| 13 | ||
| 14 | Student& operator= (const Student & right); // Assignment operator | |
| 15 | ||
| 16 | ~Student() ; // Destructor | |
| 17 | ||
| 18 | private: | |
| 19 | string name; | |
| 20 | ||
| 21 | }; | |
| 22 | ||
| 23 | // Default constructor | |
| 24 | Student::Student() : name("no name") | |
| 25 | { | |
| 26 | // Intentionally left blank | |
| 27 | } | |
| 28 | ||
| 29 | // String constructor | |
| 30 | Student::Student(string n) : name(n) | |
| 31 | { | |
| 32 | // Intentionally left blank | |
| 33 | } | |
| 34 | ||
| 35 | // Copy constructor | |
| 36 | Student::Student(const Student & obj) : name(obj.name) | |
| 37 | { | |
| 38 | // Intentionally left blank | |
| 39 | } | |
| 40 | ||
| 41 | // Assignment operator | |
| 42 | Student& Student::operator= (const Student & right) | |
| 43 | { | |
| 44 | name=right.name; | |
| 45 | return (*this); | |
| 46 | } | |
| 47 | ||
| 48 | // Destructor | |
| 49 | Student::~Student() | |
| 50 | { | |
| 51 | // Intentionally left blank | |
| 52 | } | |
| 53 | ||
| 54 | ////////////////////////////////////////////////////////////////////////////// | |
| 55 | // MARK UP FROM HERE DOWN | |
| 56 | ////////////////////////////////////////////////////////////////////////////// | |
| 57 | ||
| 58 | // friend function | |
| 59 | Student OutputDuplicate (Student obj) | |
| 60 | { | |
| 61 | Student tmp("Jack"); | |
| 62 | ||
| 63 | cout <<"Creating a new student Jame smith\n"; | |
| 64 | Student * anotherObj = new Student ("Jane Smith"); | |
| 65 | ||
| 66 | *anotherObj = obj; | |
| 67 | return tmp; | |
| 68 | } | |
| 69 | ||
| 70 | int main() | |
| 71 | { | |
| 72 | Student a, b ("John Smith"); | |
| 73 | ||
| 74 | cout <<"calling OutputDuplicate\n\n"; | |
| 75 | ||
| 76 | a = OutputDuplicate (b); | |
| 77 | ||
| 78 | cout <<"\nExiting\n"; | |
| 79 | } |
The given is a c++ code which consist of below features:
1. A class with name Student
2. A default consutructor
3. A String constructor
4. An assignment operator implementation
The requirement is to find the statements in the main function that calls constructor, destructor and other operators.
Step by step
Solved in 2 steps with 1 images









