#include
#include
using namespace std;
class persontype{
public:
persontype(string first="",string last="");
void print() const;
void setname(string first,string last);
persontype& setlastname(string last);
persontype& setfirstname(string first); //此处的&为什么不能去掉?
void getname(string& first,string& last);
private:
string firstname;
string lastname;
};
void persontype::print()const{
cout<<"the firstnameis"< cout<<"the lastname is"< }
void persontype::setname(string first,string last){
firstname=first;
lastname=last;
}
persontype& persontype::setlastname(string last){
lastname=last;
return *this;
}
persontype& persontype::setfirstname(string first){
firstname=first;
return *this; //此处的return *this 好象去掉也不影响结果,为什么?
} //那为什么要有这句?有什么作用呢?
persontype::persontype(string first,string last){
firstname=first;
lastname=last;
}
int main(){
persontype student1("huang","xianjun");
persontype student2;
persontype student3;
cout<<"line 4:student1";
student1.print();
cout< student2.setfirstname("yu").setlastname("guangbing");
cout<<"line 8:student2";
student2.print();
cout< student3.setfirstname("li");
cout<<"line 12:student3";
student3.print();
cout< student3.setlastname("zheng");
cout<<"line 16:student3";
student3.print();
cout< //return 0;
system("pause");
}
问题写在程序旁边了,高手们能不能把this指针和引用参数传递方面的讲的详细一点
小弟在此谢过
--------------------next---------------------
阅读(1184) | 评论(0) | 转发(0) |