找错,谢谢
#include<iostream>#include<string>
using namespace std ;
class Person
{
public :
Person ( const Person &p ) ;
~Person () ;
void setAge ( int x ) { age = x ; }
void print () ;
private :
char * name ;
int age ;
} ;
Person :: Person ( const Person &p )
{
name = new char [ strlen ( p.name ) + 1 ] ;
strcpy ( name , p.name ) ;
age = p.age ;
cout << " Copy constructor called ! " << endl ;
}
Person :: ~Person ()
{
cout << " Destructor called ! " << endl ;
delete name ;
}
void Person :: print () { cout << " name : " << name << " age : "<< age << endl ; }
int main ()
{
Person p1 ( " 张三 " , 21 ) ;
Person p2 = p1 ;
p1.setAge (1) ;
p2.setAge (2) ;
p1.print () ;
p2.print () ;
return 0 ;
}
错在了哪里