C++day4
2024-01-02 09:28:29
?
#include <iostream>
using namespace std;
class Person
{
int age;
string &name;
public:
Person(int age,string &name):age(age),name(name){cout << "Person的构造函数" << endl;}
~Person(){cout << "Person的析构函数"<< endl;}
Person(const Person &other):age(other.age),name(other.name)
{
cout << "Person的拷贝构造函数" << endl;
}
Person &operator=(const Person &other)
{
this->age=other.age;
this->name=other.name;
cout << "Person的拷贝赋值函数" << endl;
return *this;
}
void operator()();
friend bool operator>(Person &c1,Person &c2);
friend bool operator<(Person &c1,Person &c2);
friend bool operator==(Person &c1,Person &c2);
friend bool operator>=(Person &c1,Person &c2);
friend bool operator<=(Person &c1,Person &c2);
friend bool operator!=(Person &c1,Person &c2);
friend bool operator&&(Person &c1,Person &c2);
friend bool operator||(Person &c1,Person &c2);
friend Person operator++(Person &c1);
friend Person operator--(Person &c1);
Person operator++(int);
Person operator--(int);
friend ostream &operator<<(ostream &out,Person &c1);
friend istream &operator>>(istream &out,Person &c1);
};
string nn="";
Person temp(0,nn);
//%运算符重载
Person operator%(Person &c1,Person &c2)
{
temp.age=c1.age%c2.age;
temp.name=c1.name;
return temp;
}
//>运算符重载
bool operator>(Person &c1,Person &c2)
{
return c1.age>c2.age;
}
//<运算符重载
bool operator<(Person &c1,Person &c2)
{
return c1.age<c2.age;
}
//==运算符重载
bool operator==(Person &c1,Person &c2)
{
return c1.age==c2.age;
}
//>=运算符重载
bool operator>=(Person &c1,Person &c2)
{
return c1.age>=c2.age;
}
//<=运算符重载
bool operator<=(Person &c1,Person &c2)
{
return c1.age<=c2.age;
}
//!=运算符重载
bool operator!=(Person &c1,Person &c2)
{
return c1.age!=c2.age;
}
//&&运算符重载
bool operator&&(Person &c1,Person &c2)
{
return c1.age&&c2.age;
}
//||运算符重载
bool operator||(Person &c1,Person &c2)
{
return c1.age||c2.age;
}
//()运算符重载
void Person::operator()()
{
cout << "hello" << endl;
}
//前自增
Person operator++(Person &c1)
{
++(c1.age);
return c1;
}
//前自减
Person operator--(Person &c1)
{
--(c1.age);
return c1;
}
//后自增
Person Person::operator++(int)
{
temp.age=this->age++;
temp.name=this->name;
return temp;
}
//后自减
Person Person::operator--(int)
{
temp.age=this->age--;
temp.name=this->name;
return temp;
}
//cout运算符重载
ostream &operator<<(ostream &out,Person &c1)
{
out << c1.age << " " << c1.name;
return out;
}
//cin运算符重载
istream &operator>>(istream &in,Person &c1)
{
in >> c1.age;
in >> c1.name;
return in;
}
class Stu
{
double *score;
public:
Stu(double score):score(new double(score)){cout << "Stu的构造函数" << endl;}
~Stu()
{
delete score;
cout << "析构函数"<< endl;
}
Stu(const Stu &other):score(new double(*(other.score)))
{
cout << "拷贝构造函数" << endl;
}
Stu &operator=(const Stu &other)
{
*(this->score)=*(other.score);
cout << "拷贝赋值函数" << endl;
return *this;
}
};
int main()
{
string n1="王五";
Person s1(88,n1);
return 0;
}
文章来源:https://blog.csdn.net/zdy_cl/article/details/135332813
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!