构造函数?
#include <iostream>#include <string>
using namespace std ;
class Preson
{
public :
Person ( char * Name , int Age ) ;
~Person () ;
void setAge ( int x ) { age = x ; }
void print () ;
private :
char * name ;
int age ;
} ;
Person :: Person ( char * Name , int Age )
{
name = new char [ strlen ( Name ) + 1 ] ;
strcpy ( name , Name ) ;
age = Age ;
cout << " 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 ;
}错在了那里?