小企业网站建设一般收费,wordpress 制作portfolio,上海排名优化工具价格,湖南省水运建设投资集团网站定义一个Person类#xff0c;私有成员int age#xff0c;string name#xff0c;定义一个Stu类#xff0c;包含私有成员double *score#xff0c;写出两个类的构造函数、析构函数、拷贝构造和拷贝赋值函数#xff0c;完成对Person的运算符重载(算术运算符、条件运算… 定义一个Person类私有成员int agestring name定义一个Stu类包含私有成员double *score写出两个类的构造函数、析构函数、拷贝构造和拷贝赋值函数完成对Person的运算符重载(算术运算符、条件运算符、逻辑运算符、自增自减运算符、插入/提取运算符)
#include iostream
using namespace std;class Person
{int age;string name;
public://构造函数void show();void show1();Person(string a):name(a){}Person(int a,string b):age(a),name(b){}//拷贝构造函数Person(const Person other):age(other.age),name(other.name){}//拷贝赋值函数Person operator(const Person other){this-ageother.age;this-nameother.name;return *(this);}//算数运算符Person operator(Person other);//类内定义friend Person operator(Person a1,Person a2);//类外定义Person operator-(Person other);//类内定义friend Person operator-(Person a1,Person a2);//类外定义Person operator*(Person other);//类内定义friend Person operator*(Person a1,Person a2);//类外定义Person operator/(Person other);//类内定义friend Person operator/(Person a1,Person a2);//类外定义Person operator%(Person other);//类内定义friend Person operator%(Person a1,Person a2);//类外定义//条件运算符bool operator(Person other);//类内定义friend bool operator(Person a1,Person a2);//类外定义bool operator(Person other);//类内定义friend bool operator(Person a1,Person a2);//类外定义bool operator(Person other);//类内定义friend bool operator(Person a1,Person a2);//类外定义bool operator(Person other);//类内定义friend bool operator(Person a1,Person a2);//类外定义bool operator(Person other);//类内定义friend bool operator(Person a1,Person a2);//类外定义bool operator!(Person other);//类内定义friend bool operator!(Person a1,Person a2);//类外定义//逻辑运算符bool operator(Person other);//类内定义friend bool operator(Person a1,Person a2);//类外定义bool operator||(Person other);//类内定义friend bool operator||(Person a1,Person a2);//类外定义//自增自减运算符Person operator(int);//类内定义后置friend Person operator(Person other,int);//类外定义后置Person operator();//类内定义前置friend Person operator(Person other);//类外定义前置Person operator--(int);//类内定义后置friend Person operator--(Person other,int);//类外定义后置--Person operator--();//类内定义前置--friend Person operator--(Person other);//类外定义前置--//插入提取运算符friend ostream operator(ostream out,Person other);friend istream operator(istream in,Person other);//析构函数~Person(){}
};class Stu
{double *score;
public:void show();//构造函数Stu(){}Stu(double score):score(new double(score)){}//拷贝构造函数Stu(const Stu other):score(new double(*other.score)){}//拷贝赋值函数Stu operator(const Stu other){*(this-score) *(other.score);return (*this);}//析构函数~Stu(){}
};void Person::show()
{cout age age endl;cout name name endl;
}
void Person::show1()
{cout age age endl;
}void Stu::show()
{cout *score *score endl;
}Person Person::operator(Person other)
{string str(zhangsan);Person temp(str);temp.agethis-ageother.age;//temp.namethis-nameother.name;return temp;
}
Person operator(Person a1,Person a2)
{string str(aaa);Person temp(str);temp.agea1.agea2.age;//temp.namea1.namea2.name;return temp;
}
Person Person::operator-(Person other)
{string str(zhangsan);Person temp(str);temp.agethis-age-other.age;//temp.namethis-nameother.name;return temp;
}
Person operator-(Person a1,Person a2)
{string str(aaa);Person temp(str);temp.agea1.age-a2.age;//temp.namea1.namea2.name;return temp;
}
Person Person::operator*(Person other)
{string str(zhangsan);Person temp(str);temp.agethis-age*other.age;//temp.namethis-nameother.name;return temp;
}
Person operator*(Person a1,Person a2)
{string str(aaa);Person temp(str);temp.agea1.age*a2.age;//temp.namea1.namea2.name;return temp;
}
Person Person::operator/(Person other)
{string str(zhangsan);Person temp(str);temp.agethis-age/other.age;//temp.namethis-nameother.name;return temp;
}
Person operator/(Person a1,Person a2)
{string str(aaa);Person temp(str);temp.agea1.age/a2.age;//temp.namea1.namea2.name;return temp;
}
Person Person::operator%(Person other)
{string str(zhangsan);Person temp(str);temp.agethis-age%other.age;//temp.namethis-nameother.name;return temp;
}
Person operator%(Person a1,Person a2)
{string str(aaa);Person temp(str);temp.agea1.age%a2.age;//temp.namea1.namea2.name;return temp;
}bool operator(Person a1,Person a2)
{return a1.age a2.age;}
bool Person::operator(Person a1)
{return this-age a1.age;
}
bool operator(Person a1,Person a2)
{return a1.age a2.age;}
bool Person::operator(Person a1)
{return this-age a1.age;
}
bool operator(Person a1,Person a2)
{return a1.age a2.age;}
bool Person::operator(Person a1)
{return this-age a1.age;
}
bool operator(Person a1,Person a2)
{return a1.age a2.age;}
bool Person::operator(Person a1)
{return this-age a1.age;
}
bool operator(Person a1,Person a2)
{return a1.age a2.age;}
bool Person::operator(Person a1)
{return this-age a1.age;
}
bool operator!(Person a1,Person a2)
{return a1.age ! a2.age;}
bool Person::operator!(Person a1)
{return this-age a1.age;
}bool Person::operator(Person other)
{return this-age other.age;
}
bool operator(Person a1,Person a2)
{return a1.age a2.age;
}
bool Person::operator||(Person other)
{return this-age || other.age;
}
bool operator||(Person a1,Person a2)
{return a1.age || a2.age;
}Person Person::operator(int)
{string strzhangsan;Person temp(str);temp.age this-age;return temp;
}Person operator(Person other,int)
{string strzhangsan;Person temp(str);temp.ageother.age;return temp;
}
Person Person::operator()
{(this-age);return (*this);
}
Person operator(Person other)
{(other.age);return other;
}
Person Person::operator--(int)
{string strzhangsan;Person temp(str);temp.age this-age--;return temp;
}Person operator--(Person other,int)
{string strzhangsan;Person temp(str);temp.ageother.age--;return temp;
}
Person Person::operator--()
{--(this-age);return (*this);
}
Person operator--(Person other)
{--(other.age);return other;
}ostream operator(ostream out,Person other)
{out other.age endl;return out;
}
istream operator(istream out,Person other)
{out other.age;return out;
}int main()
{cout ---person构造函数--- endl;string strshangsan;Person a1(1,str);a1.show();cout ---person拷贝构造函数--- endl;Person a2a1;a2.show();cout ---person拷贝赋值函数--- endl;Person a3(str);a3a1;a3.show();cout ---Stu构造函数--- endl;Stu b1(100);b1.show();cout ---stu拷贝构造函数--- endl;Stu b2b1;b2.show();cout ---stu拷贝赋值函数--- endl;Stu b3;b3b1;b3.show();string str1(hello);string str2( world);Person a4(10,str1);Person a5(50,str2);Person a6operator(a4,a5);a6.show1();a6operator-(a4,a5);a6.show1();a6operator*(a4,a5);a6.show1();a6operator/(a4,a5);a6.show1();a6operator%(a4,a5);a6.show1();bool c1operator(a4,a5);bool c2a4.operator(a5);cout c1 c1 endl;cout c2 c2 endl;c1operator(a4,a5);c2a4.operator(a5);cout c1 c1 endl;cout c2 c2 endl;c1operator(a4,a5);c2a4.operator(a5);cout c1 c1 endl;cout c2 c2 endl;c1operator(a4,a5);c2a4.operator(a5);cout c1 c1 endl;cout c2 c2 endl;c1operator(a4,a5);c2a4.operator(a5);cout c1 c1 endl;cout c2 c2 endl;c1operator!(a4,a5);c2a4.operator!(a5);cout c1 c1 endl;cout c2 c2 endl;c1operator(a4,a5);c2a4.operator(a5);cout c1 c1 endl;cout c2 c2 endl;cout aaaa endl;Person a7a4.operator();a7.show1();a7operator(a4);a7.show1();a7a5.operator(10);a7.show1();a7operator(a5,10);a7.show1();a7a4.operator--();a7.show1();a7operator--(a4);a7.show1();a7a5.operator--(10);a7.show1();a7operator--(a5,10);a7.show1();couta4 a5 endl;operator(cout,a4);operator(cout,a5);Person a8(str);cina8;a8.show1();operator(cin,a8);a8.show1();return 0;
}