//构造函数没有返回类型,函数名必须与类名相同,可以进行重构,赋默认值
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
// 定义数据成员封装函数setName()
Student():m_strName("hhh")//初始化列表,在所初始的值 被const 定义时只能用初始化列表;
{
// m_strName="kkkkj"; 这样会报错
}
Student(const Student &stu)
{
cout<<"对象的拷贝";//当没有自定义的拷贝构造函数时,系统自动生成一个拷贝构造函数,拷贝构造函数的参数是确定的,不能重载
}
~Student()//析构函数,释放内存 ,系统也会默认生成,因为没有参数,不能重构;
{
cout<<"hhhh";
}
string getName()
{
return m_strName;
}
//定义Student类私有数据成员m_strName
private:
const string m_strName;
};
int main()
{
Student str;
cout<<str.getName();
Student st=str;
Student s(str);
return 0;
}