文章目录 类与对象 构造函数、析构函数 get/set方法 函数:类内声明、类外定义 static
类与对象
# include <iostream>
# include <string>
using namespace std;
class Person { public : string name; int age; public : void eat ( ) { cout<< "eat()" << endl; } void show ( ) { cout<< "[name:" << name<< ", age:" << age<< "]" << endl; }
} ;
int main ( ) { Person p1; p1. name = "AAA" ; p1. age = 11 ; p1. eat ( ) ; p1. show ( ) ; return 0 ;
}
构造函数、析构函数
# include <iostream>
# include <string>
using namespace std; class Person { public : string name; int age; public : Person ( ) { cout<< "构造函数:Person()" << endl; } Person ( string _name, int _age) { name = _name; age = _age; cout<< "构造函数:Person(string _name,int _age)" << endl; } Person ( const Person& p) { name = p. name; age = p. age; cout<< "构造函数:Person(const Person& p)" << endl; } ~ Person ( ) { cout<< "~Person()" << name<< endl; } void show ( ) { cout<< "[name:" << name<< ", age:" << age<< "]" << endl; }
} ;
int main ( ) { Person p1; p1. name = "AAA" ; p1. age = 11 ; p1. show ( ) ; Person p2 ( "BBB" , 12 ) ; p2. show ( ) ; Person p3 ( p1) ; p3. show ( ) ; return 0 ;
}
get/set方法
# include <iostream>
# include <string>
using namespace std;
class Person { private : string name; int age; public : Person ( ) { cout<< "构造函数:Person()" << endl; } Person ( string _name, int _age) { name = _name; age = _age; cout<< "构造函数:Person(string _name,int _age)" << endl; } Person ( const Person& p) { name = p. name; age = p. age; cout<< "构造函数:Person(const Person& p)" << endl; } ~ Person ( ) { cout<< "~Person()" << name<< endl; } void setName ( string _name) { name = _name; } string getName ( ) { return name; } void setAge ( int _age) { age = _age; } int getAge ( ) { return age; } void show ( ) { cout<< "[name:" << name<< ", age:" << age<< "]" << endl; }
} ;
int main ( ) { Person p1;
p1. setName ( "AAA" ) ; p1. setAge ( 11 ) ; p1. show ( ) ; Person p2 ( "BBB" , 12 ) ; p2. show ( ) ; Person p3 ( p1) ; p3. setName ( "CCC" ) ; p3. show ( ) ; return 0 ;
}
函数:类内声明、类外定义
# include <iostream>
# include <string>
using namespace std; class Person { private : string name; int age; public : Person ( ) ; Person ( string _name, int _age) ; Person ( const Person& p) ; ~ Person ( ) ; void setName ( string _name) { name = _name; } string getName ( ) { return name; } void setAge ( int _age) { age = _age; } int getAge ( ) { return age; } void show ( ) { cout<< "[name:" << name<< ", age:" << age<< "]" << endl; }
} ;
Person :: Person ( ) { cout<< "构造函数:Person()" << endl;
}
Person :: Person ( string _name, int _age) { name = _name; age = _age; cout<< "构造函数:Person(string _name,int _age)" << endl;
}
Person :: Person ( const Person& p) { name = p. name; age = p. age; cout<< "构造函数:Person(const Person& p)" << endl;
}
Person :: ~ Person ( ) {
cout<< "析构函数:~Person()" << name<< endl;
} int main ( ) { Person p1;
p1. setName ( "AAA" ) ; p1. setAge ( 11 ) ; p1. show ( ) ; Person p2 ( "BBB" , 12 ) ; p2. show ( ) ; Person p3 ( p1) ; p3. setName ( "CCC" ) ; p3. show ( ) ; return 0 ;
}
static
# include <iostream>
# include <string>
using namespace std; class Person { private : string name; int age; public : static int cnt; Person ( ) ; Person ( string _name, int _age) ; Person ( const Person& p) ; ~ Person ( ) ; void setName ( string _name) { name = _name; } string getName ( ) { return name; } void setAge ( int _age) { age = _age; } int getAge ( ) { return age; } void show ( ) { cout<< "[name:" << name<< ", age:" << age<< "]" << endl; }
} ;
int Person:: cnt = 0 ;
Person :: Person ( ) { cnt ++ ; cout<< "构造函数:Person()" << endl;
}
Person :: Person ( string _name, int _age) { cnt ++ ; name = _name; age = _age; cout<< "构造函数:Person(string _name,int _age)" << endl;
}
Person :: Person ( const Person& p) { cnt ++ ; name = p. name; age = p. age; cout<< "构造函数:Person(const Person& p)" << endl;
}
Person :: ~ Person ( ) { cnt -- ;
cout<< "析构函数:~Person()" << name<< endl; cout<< Person:: cnt<< endl;
}
int main ( ) {
cout<< Person:: cnt<< endl; Person p1;
p1. setName ( "AAA" ) ; p1. setAge ( 11 ) ; p1. show ( ) ; cout<< Person:: cnt<< endl; Person p2 ( "BBB" , 12 ) ; p2. show ( ) ; cout<< Person:: cnt<< endl; Person p3 ( p1) ; p3. setName ( "CCC" ) ; p3. show ( ) ; cout<< Person:: cnt<< endl; cout<< p3. cnt<< endl; return 0 ;
}