Question: in c + + The copy constructor is a constructor which creates an object by initializing it with an object of the same class. Given

in c++ The copy constructor is a constructor which creates an object by initializing it with an object of the same class.
Given a 'class ProblemSolution', create a copy constructor which will copy all data members of the existing object and display it.
Input
10
20
where,
First line contains an integer x.
Second line contains an integer y.
Output
10
20
Assume that,
x an y are integers within the range [-2,147,483,648 to 2,147,483,647].
#include
using namespace std;
class ProblemSolution
{
private:
int x;
int *y;
public:
ProblemSolution(){};
void setValues(int x,int y){
this-> x = x;
*this-> y = y;
}
};
int main(){
int x,y;
cin >> x;
cin >> y;
ProblemSolution problemSolution;
problemSolution.setValues(x,y);
ProblemSolution problemSolutionCopy=problemSolution;
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!