0% found this document useful (0 votes)
10 views

Deep and Shallow Copy Code

The document defines a calculator class with private data members and public member functions to perform addition and display the result. It demonstrates passing pointers to functions and using a copy constructor for deep copying.

Uploaded by

ALEENA AZHAR
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Deep and Shallow Copy Code

The document defines a calculator class with private data members and public member functions to perform addition and display the result. It demonstrates passing pointers to functions and using a copy constructor for deep copying.

Uploaded by

ALEENA AZHAR
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

using namespace std;

//class
class calculator
{
private:
int a, b, c;
int *p;
public:
int sum(int x, int y, int *z){ //Changing z to *z
a = x;
b = y;
*p = *z; //changing p =z to *p = *z
c = a+b;
}
int show()
{
cout<<"Sum is: "<<c;
}
calculator(){
p = new int;
}
calculator(const calculator &c){
a = c.a;
b = c.b;
//p = c.p;//shallow copy
p = new int;
*p = *(c.p);//Deep Copy
}
};
int main() {
calculator c1;
int ptr = 4;
c1.sum(2,3,&ptr); //passing 4's address to a pointer variable not 4 itself
//c1.show();
//Copy constructor
calculator c2 = c1;
//c2 = c1; //implicit operator overloading
c2.show();
return 0;
}

You might also like