#include <iostream>
#include <string>
using namespace std;
// 定义一个类 Student
class Student
{
private:
string name;
int age;
string address;
public:
// 存
void setname(string s){ name = s;}
void setage(int y){age = y; }
void setaddress(string add){address = add;}
// 取
string getname(){return name;}
int getage(){return age;}
string getaddress(){return address;}
// 返回 指向Student 对象的指针,因为this是指针,且是指向当下对象的指针
Student* Show()
{
cout<<"姓名: "<<name<<" --- 年龄: "<<age<<" ---- 住址: "<<address<<endl;
return this;
}
protected:
};
int main(void)
{
Student x;
x.setname("柳絮飘");
x.setage(22);
x.setaddress("学明路115号");
// 类的成员函数Show返回的是this指针,
// x.Show()并没有用一个指针变量来接受它的返回值就直接用了.
// 有返回值的函数,调用时并不一定要搞个变量接受它的返回值.
x.Show()->Show()->Show()->Show(); //
cout<<"--------------------------------------------------"<<endl;
// 搞个指针变量来接受吧.
Student * y =NULL;
y = x.Show();
y->Show()->Show()->Show();
// --------------------
return 0;
}
---