copy data from one object to another object

For example I want to copy p1's name to p2's name and when I run
"p2.write();" the result should be the same as "p1.write()";


How can I do and where can I learn this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 #include <iostream>

using namespace std;

class person
{
private:
	string name;
public:
	void givename(string n)
	{
		name = n;
	} 
	int write()
	{
		return name;
	}
};

int main()
{
	person p1, p2;
	p1.givename("jasper");
        p1.write();
	p2.write();
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

class person
{
private:
	string name;
public:
	void givename(string n)
	{
		name = n;
	} 
	void write()           // <**** void
	{
		cout << name;      // <**** cout
	}
	string getName()       // A "getter" for private variables
	{
		return name;
	}
};

int main()
{
	person p1, p2;
	p1.givename("jasper");
	p1.write();
	p2.givename(p1.getName());
	p2.write();
}


How about writing some constructors?


logarech wrote:
where can I learn this?

http://www.cplusplus.com/doc/tutorial/classes/
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <string>
#include <iostream>

using namespace std;

class person
{
private:
    string name;
public:
    void givename(string n)
    {
        name = n;
    }
    
    string write()
    {
        return name;
    }
};

int main()
{
    person p1, p2;
    p1.givename("jasper");
    p2.givename(p1.write());
    
    cout << p1.write() << '\n';
    cout << p2.write() << '\n';
}
You can use a copy constructor to create a new object as a copy of an existing one. You can use an assignment operator to assign to an existing object, the values of another existing object:

http://www.cplusplus.com/articles/y8hv0pDG/
https://www.geeksforgeeks.org/copy-constructor-vs-assignment-operator-in-c/

If you want all objects of the same type, to share the same value for one of the data members, you can use a static data member:

https://www.bogotobogo.com/cplusplus/statics.php
https://www.learncpp.com/cpp-tutorial/811-static-member-variables/

OR, ( be careful though, this works easily because there is only a name)

1
2
3
4
5
6
7
8
9
10
11

int main()
{
    person p1;
    p1.givename("Jasper");
    person p2 = p1;
    
    cout << p1.write() << '\n';
    cout << p2.write() << '\n';
}
OR, care needed here too

1
2
3
4
5
6
7
8
9
10
int main()
{
    person p1;
    p1.givename("Jasper");
    
    person* p2 = &p1;
    
    cout << p1.write() << '\n';
    cout << p2->write() << '\n';
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>

class person
{
    private: std::string name ;

    public:

        // constructor https://en.cppreference.com/w/cpp/language/initializer_list
        // initialise the member name with (as a copy of) the string passed to the constructor
        explicit person( std::string n ) : name(n) {}

        // note: the trailing const is important; should not be omitted
        // (it means that this function will not modify the object;
        //  ie. it will not change the object’s logical state.)
        void print() const { std::cout << "person { name: " << name << " }\n" ; }
};

int main()
{
    const person a( "logarech" ) ; // construct a person with this name
    a.print() ;

    const person b = a ; // copy construct (b is a copy of a; b.name is initialised as a copy of a.name)
                         // (the compiler provides an implicitly declared copy constructor)

    b.print() ;
}

http://coliru.stacked-crooked.com/a/7519575b6ead2e4d
thank you all
Topic archived. No new replies allowed.