#include<iostream>
#include<string>
using namespace std;
class Person{
public:
Person(string name,int age){
this->name=name;
this->age=age;
}
void show(){
cout<<name<<" "<<age<<endl;
}
void operator()(){
cout<<"class the ()function"<<endl;
}
private:
string name;
int age;
};
int main(){
Person p("guangdong",22);
p.show();
p();
//p.operator()();
return 0;
}
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
Person(int age){
this->age=age;
}
Person& operator++(){
++age;
return *this;
}
Person operator++(int){
int a=this->age;
Person pp(a);
++age;
return pp;
}
void show(){
cout<<age<<endl;
}
private:
int age;
};
int main(){
Person p(5);
p.show();
//++p;
//p.operator ++();
//p++;
//++p;
//Person pp=p++; //后置
Person pp=++p; //前置
pp.show();
return 0;
}
C++类与运算符重载示例
本文介绍了一个简单的C++程序,通过定义Person类来演示构造函数、成员函数及运算符重载的使用。其中包括显示个人信息的方法以及前缀和后缀递增运算符的重载实现。
2816

被折叠的 条评论
为什么被折叠?



