c++类class链式调用函数的例子
Admin | 2025-10-15 8:58:29 | 被阅次数 | 3
c++类class链式调用函数的例子
#include <iostream>
using namespace std;
class productinfor{
private:
string name;
float age;
public:
productinfor setname(const string n){
name = n;
return *this;
}
productinfor setage(const float v){
age = v;
return *this;
}
void printnameage(){
cout << "My name is " << name << ", and I am " << age << " years old." << endl;
}
};
int main(){
productinfor p;
p.setname("青山").setage(44.3).printnameage();
p.setname("南山").setage(43.4).printnameage();
}
输出
My name is 青山, and I am 44.3 years old.
My name is 南山, and I am 43.4 years old.