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:
#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() ;
}