. Create a class Number with a pointer as a data data member of integer type. Write user defined copy constructor to the class and implement the deep copy for the Number class objects. So when a new object is created from an old object of the Number class, pointer data member can point different memory block.
#include<iostream>
using namespace std;
class Number
{
int *ptr;
public:
Number (int i = 0) { ptr = new int(i); }
void setValue (int i) { *ptr = i; }
void print() { cout << *ptr << endl; }
Number & operator = (const Number &t);
};
Number & Number::operator = (const Number &t)
{
// Check for self assignment
if(this != &t)
*ptr = *(t.ptr);
return *this;
}
int main()
{
Number t1(5);
Number t2;
t2 = t1;
t1.setValue(10);
t2.print();
return 0;
}
Comments
Dear Venkat, please use panel for submitting new questions
Define a class Employee with data members as name, emp_id, age. Write a program to write the data of three employee’s using class object. Read the records of the employee and print them to console.
Leave a comment