The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Create a class called MyInteger. It should have a field of type pointer-to-int called pInteger. It should have a constructor that takes as a parameter an int - the constructor will then dynamically allocate memory for an int, using pInteger, and assign the parameter's value to that memory. The class should have a destructor that will deallocate that memory when the object is destroyed. You should write a copy constructor that will correctly make a separate copy of the memory pInteger points to, and make pInteger in the new object point to it. You should overload the = operator such that each of the two objects involved has its own separate copy of the memory that its own pInteger points to. The =operator should have a return type of MyInteger. There should be methods called setMyInt and getMyInt for getting and setting the value of the int that pInteger points to.
Â
In you have the following code in your main method:
MyInteger obj1(17); MyInteger obj2 = obj1; std::cout << obj1.getMyInt() << std::endl; std::cout << obj2.getMyInt() << std::endl; obj2.setMyInt(9); std::cout << obj1.getMyInt() << std::endl; std::cout << obj2.getMyInt() << std::endl; MyInteger obj3(42); obj2 = obj3; std::cout << obj2.getMyInt() << std::endl; std::cout << obj3.getMyInt() << std::endl; obj3.setMyInt(1); std::cout << obj2.getMyInt() << std::endl; std::cout << obj3.getMyInt() << std::endl;
Then the output should be :
17 17 17 9 42 42 42 1
The files must be named MyInteger.hpp and MyInteger.cpp.