赞
踩
如图所示的类中只有两个数据成员,语法:构造函数后面要用冒号隔开,对于多个数据成员之间要用逗号隔开,赋值的时候要有括号而不能用等于号复制。
初始化列表由于构造函数执行:编译器会先给初始列表的成员。
赋值再执行构造函数的代码。
初始化列表只适用于构造函数。
初始化列表可以初始化多个成员数据。
大家可能会想初始化列表的工作构造函数完全能够完成,为什么还要存在初始化列表呢!下面看一个例子。
用构造函数来给常量赋值会发生语法错误
但是用初始化列表却可以完成这个工作。
main函数文件:
#include<iostream> #include<stdlib.h> #include<string> #include"teacher.h" using namespace std; /****************************************************************/ /*自定义Teacher类 *自定义有参默认构造函数 *使用初始化列表初始化数据 *数据成员: * 名字 * 年龄 *成员函数 * 数据成员的封装函数 *扩展 #定义可以带最多学生个数,此为常量 /****************************************************************/ int main(void) { Teacher t1("Merry",2,1000); cout << t1.getName() << " " << t1.getAge() <<" "<<t1.getMax()<< endl; //cout << t2.getName() << " " << t2.getAge() << endl; system("pause"); return 0; }
Teacher.h文件
#include<string> using namespace std; class Teacher { public: Teacher(string _name ="Jim", int _age = 1,int m=100); void setName(string _name); string getName(); void setAge(int _age); int getAge(); int getMax(); private: string m_strName; int m_iAge; const int m_iMax; };
Teacher.cpp文件(初始化列表)
#include<iostream> #include<stdlib.h> #include<string> #include "Teacher.h" using namespace std; Teacher::Teacher(string _name, int _age,int m):m_strName(_name),m_iAge(_age),m_iMax(m) { cout << "Teacher(string _name,int _age)" << endl; } int Teacher::getMax() { return m_iMax; } void Teacher::setName(string _name) { m_strName = _name; } string Teacher::getName() { return m_strName; } void Teacher::setAge(int _age) { m_iAge = _age; } int Teacher::getAge() { return m_iAge; }
可以尝试不用初始化列表初始化const常量会发现错误
如图当我们实例化对象stu1、stu2、stu3时本应该自动调用构造函数就会打印出三个Student的,但是事实是只打印了一个。原因就是后面两个调用的是拷贝构造函数。
特点:
*拷贝构造函数的参数是固定的不可以重载
*如果有自定义拷贝构造函数则系统会自动生成一个默认的拷贝构造函数
*当采用直接初始化或者复制初始化实例对象时系统会自动调用拷贝构造函数
main函数文件:
#include<iostream> #include<stdlib.h> #include<string> #include"teacher.h" using namespace std; /****************************************************************/ /*自定义Teacher类 *自定义拷贝构造函数 *数据成员: * 名字 * 年龄 *成员函数 * 数据成员的封装函数 /****************************************************************/ int main(void) { Teacher t1; Teacher t2 = t1; Teacher t3(t1); system("pause"); return 0; }
Teacher.h文件
#include<string> using namespace std; class Teacher { public: Teacher(string _name = "Jim", int _age = 1); Teacher(const Teacher &tea); void setName(string _name); string getName(); void setAge(int _age); int getAge(); private: string m_strName; int m_iAge; };
Teacher.cpp文件
#include<iostream> #include<stdlib.h> #include<string> #include "Teacher.h" using namespace std; Teacher::Teacher(string _name, int _age):m_strName(_name),m_iAge(_age) { cout << "Teacher(string _name,int _age)" << endl; } Teacher::Teacher(const Teacher &tea) { cout << "Teacher(const Teacher &tea)" << endl; } void Teacher::setName(string _name) { m_strName = _name; } string Teacher::getName() { return m_strName; } void Teacher::setAge(int _age) { m_iAge = _age; } int Teacher::getAge() { return m_iAge; }
运行结果显示t2、t3的实例化都调用的是拷贝构造函数。
**注意:**拷贝构造函数不仅在前面实例化对象的时候会被调用,当出现参数传递的时候一样会自动调用拷贝构造函数。如图当test函数引用t1的时候一样会自动调用拷贝构造函数。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。