current position:Home>I created an object. How can I use the copy constructor to copy its members to another object and modify some members?

I created an object. How can I use the copy constructor to copy its members to another object and modify some members?

2022-01-27 04:28:01 CSDN Q & A

#include
#include
using namespace std;
class Employee{
public:

Employee()

{
name=" Zhang San ";
adress=" Peking University, ";
city=" The Beijing municipal ";
postcode="421001";
}

Employee(Employee &e){
};
void setname(string *n);
void setadress(string *a);
void setcity(string *c);
void setpostcode(string *p);

void display();
private:
string name;
string adress;
string city;
string postcode;
};

void Employee::setname(string n)
{
name=n;
}
;

void Employee::setadress(string a)
{
adress=a;
};

void Employee::setcity(string c)
{
city=c;
};

void Employee::setpostcode(string p)
{
postcode=p;
};
void Employee::display()
{
cout<<"name="<<name<<endl;
cout<<"adress="<<adress<<endl;
cout<<"city="<<city<<endl;
cout<<"postcode="<<postcode<<endl;
};

int main()
{
string n,a,c,p;
Employee E1,E2;
cin>>n>>a>>c>>p;
E1.setname(&n);
E1.setadress(&a);
E1.setcity( &c);
E1.setpostcode(&p);
E1.display();

return 0;
}
I also want to create one E2 The object of , It is from E1 Copied from , And you can also modify the copied members , How to use the copy constructor to realize ?




Refer to the answer 1:

Copy constructors can only copy , To modify, you need to call the member function to modify .
You need to add... To your code get Method to get the member variable , In this way, we can get... In the assignment constructor e Properties of .
There are still some errors in your code ,set There is no need to use a pointer in the method .
The code is modified as follows :

#include <iostream>#include <string>using namespace std;class Employee{public:    Employee()    {        name=" Zhang San ";        adress=" Peking University, ";        city=" The Beijing municipal ";        postcode="421001";    }    Employee(Employee &e){        this->name = e.getname();        this->city = e.getcity();        this->adress = e.getadress();        this->postcode = e.getpostcode();    };    void setname(string n);    void setadress(string a);    void setcity(string c);    void setpostcode(string p);    string getname(){
   
   
   
   return name;}    string getadress(){
   
   
   
   return adress;}    string getcity(){
   
   
   
   return city;}    string getpostcode(){
   
   
   
   return postcode;}    void display();private:    string name;    string adress;    string city;    string postcode;};void Employee::setname(string n){    name=n;};void Employee::setadress(string a){    adress=a;};void Employee::setcity(string c){    city=c;};void Employee::setpostcode(string p){    postcode=p;};void Employee::display(){    cout<<"name="<<name<<endl;    cout<<"adress="<<adress<<endl;    cout<<"city="<<city<<endl;    cout<<"postcode="<<postcode<<endl;};int main(){    string n,a,c,p;    Employee E1;    cin>>n>>a>>c>>p;    E1.setname(n);    E1.setadress(a);    E1.setcity(c);    E1.setpostcode(p);    E1.display();    Employee E2(E1); // Call copy constructor     E2.setname("haha"); // Call the method to modify the property     return 0;}



Refer to the answer 2:



Refer to the answer 3:

Employee(Employee &e), This is the copy constructor , But you wrote an empty




Refer to the answer 4:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270427570095.html

Random recommended