学习算法集市文章:
C++的三种构造函数:
- 无参数构造函数:以类作为函数名,没有形参;
- 一般构造函数:有初始化列表方式和内部赋值方式两种;
- 复制构造函数:根据一个已存在的对象复制出一个新的对象;
class Student{
public:
int m_age;
int m_score;
//无参构造函数
Student(){
m_age=10;
m_score=99;
cout<<"无参构造函数"<<endl;
}
//一般构造函数
//Student(int age,int score):
// m_age(age),
// m_score(score)
// {}
Student(int age,int score){
m_age=age;
m_score=score;
cout<<"一般构造函数"<<endl;
}
//复制构造函数
Student(Student& s){
m_age=s.m_age;
m_score=s.m_score;
cout<<"复制构造函数"<<endl;
}
};
复制构造函数:根据一个已存在的对象复制出一个新的对象;
等号运算符重载:将等号(=)右边的本类对象的值复制给等号左边的对象;
class Student{
public:
int m_age