在Student类中演示这三种构造函数
#include <QCoreApplication>
#include "iostream"
using namespace std;
class Student{
private:
string name;
int age;
public:
Student(){
}
Student(string _name, int _age){
name = _name;
age = _age;
}
Student(const Student& _s){
name = _s.name;
age = _s.age;
}
Student& operator = (const Student& _s){
if (this == &_s){
return *this;
}
this->name = _s.name;
this->age = _s.age;
return *this;
}
~Student(){
}
string getName(){
return name;
}
int getAge(){
return age;
}
void setName(string _name){
name = _name;
}
void setAge(int _age){
age = _age;
}
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
Student s;
s.setName("loupeihuang");
s.setAge(3);
cout<<s.getName()<<" "<<s.getAge()<<endl;
Student s1("suntian",6);
cout<<s1.getName()<<" "<<s1.getAge()<<endl;
Student s2(s1);
cout<<s2.getName()<<" "<<s2.getAge()<<endl;
Student s3 = s1;
cout<<s3.getName()<<" "<<s3.getAge()<<endl;
return app.exec();
}
运行结果
