1.对象的构造过程,构造顺序
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Person {
private:
char *name;
int age;
char *work;
public:
Person() {//cout <<"Pserson()"<<endl;
name = NULL;
work = NULL;
}
Person(char *name)
{
//cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = NULL;
}
Person(char *name, int age, char *work = "none")
{
cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = new char[strlen(work) + 1];
strcpy(this->work, work);
}
Person(Person &per)
{
cout <<"Pserson(Person &)"<<endl;
this->age = per.age;
this->name = new char[strlen(per.name) + 1];
strcpy(this->name, per.name);
this->work = new char[strlen(per.work) + 1];
strcpy(this->work, per.work);
}
~Person()
{
cout << "~Person()"<<endl;
if (this->name) {
cout << "name = "<<name<<endl;
delete this->name;
}
if (this->work) {
cout << "work = "<<work<<endl;
delete this->work;
}
}
void printInfo(void)
{
//printf("name = %s, age = %d, work = %s\n", name, age, work);
cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
}
};
Person per_g("per_g", 10); //全局变量
void func()
{
Person per_func("per_func", 11); //局部变量
static Person per_func_s("per_func_s", 11); //静态变量
}
int main(int argc, char **argv)
{
Person per_main("per_main", 11); //局部变量
static Person per_main_s("per_main_s", 11); //静态变量
for (int i = 0; i < 2; i++)
{
func();
Person per_for("per_for", i);
}
return 0;
}
执行结果:
1.全局变量 per_g
然后是main 函数,顺序执行,碰到哪一个对象就执行哪一个构造函数
3.per_main
4.per_main_s
第一次进入for循环
5.per_func
6.per_func_s
退出func(),销毁局部变量,但是不会销毁静态实例化对象static
7.~Person()name=per_func work=none
8.per_for
第一次退出for循环,
9.~Person()name=per_for work=none
第二次进入for循环
10.per_func
不会再次实例化静态对象per_func_s
退出func()
11.~Person()name=per_func work=none
12.per_for
13.~Person()name=per_for work=none
退出main
14.~Person()name=per_main work=none
15.~Person()name=per_func_s work=none
16.~Person()name=per_main_s work=none
构造顺序:
按进行中定义对象的顺序调用构造函数,
静态对象值调用一次构造函数,
全局对象在main函数执行前被构造
2.student类里面含有对象
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
class Person {
private:
char *name;
int age;
char *work;
public:
Person() {
cout <<"Pserson()"<<endl;
name = NULL;
work = NULL;
}
Person(char *name)
{
//cout <<"Pserson(char *)"<<endl;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = NULL;
}
Person(char *name, int age, char *work = "none")
{
cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->work = new char[strlen(work) + 1];
strcpy(this->work, work);
}
Person(Person &per)
{
cout <<"Pserson(Person &)"<<endl;
this->age = per.age;
this->name = new char[strlen(per.name) + 1];
strcpy(this->name, per.name);
this->work = new char[strlen(per.work) + 1];
strcpy(this->work, per.work);
}
~Person()
{
cout << "~Person()"<<endl;
if (this->name) {
cout << "name = "<<name<<endl;
delete this->name;
}
if (this->work) {
cout << "work = "<<work<<endl;
delete this->work;
}
}
void setName(char *n)
{
name = n;
}
int setAge(int a)
{
if (a < 0 || a > 150)
{
age = 0;
return -1;
}
age = a;
return 0;
}
void printInfo(void)
{
//printf("name = %s, age = %d, work = %s\n", name, age, work);
cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
}
}; //class Person
class Student {
private:
Person father;
Person mother;
int student_id;
public:
Student()
{
cout<<"Student()"<<endl;
}
Student(int id, char *father, char *mother, int father_age = 40, int mother_age = 39)
{
cout<<"Student(int id, char *father, char *mother, int father_age = 40, int mother_age = 39)"<<endl;
}
~Student()
{
cout<<"~Student()"<<endl;
}
};
int main(int argc, char **argv)
{
Student s(100, "bill", "lily");
return 0;
}
执行结果:
Pserson()
Pserson()
Student(int id, char *father, char *mother, int father_age = 40, int mother_age = 39)
~Student()
~Pserson()
~Pserson()
3.构造顺序与析构顺序
Student(int id, char *father, char *mother, int father_age = 40, int mother_age = 39) : mother(mother, mother_age), father(father, father_age)
{
cout<<"Student(int id, char *father, char *mother, int father_age = 40, int mother_age = 39)"<<endl;
}
Pserson(char*, int), name = bill age= 40
Pserson(char*, int), name = lily age= 39
Student(int id, char *father, char *mother, int father_age = 40, int mother_age = 39)
~Student()
~Pserson name = lily ...
~Pserson name = bill ...