Question: 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
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 return a reference to the object pointed to by the this pointer.
How do I integrate the this pointer, into my =operater?
/********************************************************************* ** Author: Tim Kelly ** Date: 5/16/17 ** Description: Header file to create a class called MyInteger. Has a ** function for constructor, and copy constructor, destructor, and = overload operator ** as well as a get/set function. *********************************************************************/
#ifndef MYINTEGER #define MYINTEGER
#include
using std::cout; using std::endl;
class MyInteger { private: int *pInteger;
public: MyInteger(int); //constructor MyInteger(const MyInteger &obj); //copy constructor void setMyInt(int); int getMyInt(); ~MyInteger(); //destructor
void operator=(const MyInteger &otherObj); //equal operator for overloading
};
#endif // !MYINTEGER
/********************************************************************* ** Author: Tim Kelly ** Date: 5/16/17 ** Description: Implementation file for functions, including *********************************************************************/
#include "MyInteger.hpp"
MyInteger::MyInteger(int i) { pInteger = new int(); *pInteger = i; //create memory }
MyInteger::MyInteger(const MyInteger & obj) { pInteger = new int(); *pInteger = *obj.pInteger; //copy constructor }
void MyInteger::setMyInt(int i) { *pInteger = i; }
int MyInteger::getMyInt() { return *pInteger; }
MyInteger::~MyInteger() { delete pInteger; //destructor }
int MyInteger::operator=(const MyInteger &otherObj) {
*pInteger = *otherObj.pInteger; //operator function
return }
int main() {
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;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
