C++_默认值拷贝构造函数_构造函数顺序
1、创建类时除了析构和构造函数还有一种值拷贝构造函数
注意:值拷贝构造函数是用于整个类对象给另一个类对象赋值的比如这样(Person per2(per);)把实例化的per里面的参数值复制给per2,他们使用的是同一片内存空间,当per释放的时候,per2也不能使用了
#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)"<<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()
{
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;
}
};
int main(int argc, char **argv)
{
Person per("zhangsan", 18);
Person per2(per);
per2.printInfo();
return 0;
}
2、实现自己的值拷贝函数
注意:实现过程实际就是自己写一个(传入参数是对象)重载构造函数
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);
}
3、构造函数的顺序
注意:
①按照运行中定义对象的顺序调用构造函数
②静态对象只调用一次构造函数
③全局对象在main函数执行前被调用
④局部对象在执行完会被析构函数销毁
#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;
}
};
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;
}
类里面嵌套其他对象(类似结构体中包含其他结构体)
注意:
①当自己已经定义了一个带有参数的构造函数,那么系统将不再给你分配默认的无参数的构造函数,必须自己加上一个无参的构造函数
②析构函数的调用顺序和构造函数相反的
③下面例子可以完成在初始化学生类时候,一起把他包含的父母类也一起初始化了
④当学生类里面包含其他类的时候,学生类实例化的时候,构造函数的顺序是先父亲再母亲最后自己
#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 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) : 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;
}
~Student()
{
cout<<"~Student()"<<endl;
}
};
int main(int argc, char **argv)
{
Student s(100, "bill", "lily");
return 0;
}