12-29

本文详细介绍了C++中的类Person和Stu,包括它们的构造函数、析构函数、拷贝构造函数、拷贝赋值函数以及各种运算符重载,如算术运算、比较运算和逻辑运算。
摘要由CSDN通过智能技术生成

#include <iostream>
#include <cstring>
using namespace std;
 
 
class Person
{
    int age;
    string &name;
 public:
    //构造函数
 
    Person(int age,string &name):name(name)
    {
        cout<<"P的构造函数"<<endl;
        this->age = age;
    }
    //析构函数
    ~Person()
    {
        cout<<"P的析构函数"<<endl;
    }
    //拷贝构造函数
    Person (const Person &other):age(other.age),name(other.name)
    {
        cout<<"P的拷贝构造函数"<<endl;
    }
    //拷贝赋值函数
    Person &operator = (const Person &other)
    {
        this->age = other.age;
        this->name = other.name;
        cout<<"P的拷贝赋值函数"<<endl;
        return *this;
    }
    //算数运算符
    Person operator+(const Person &other);
    Person operator-(const Person &other);
    Person operator*(const Person &other);
    Person operator/(const Person &other);
    Person operator%(const Person &other);
    friend Person operator+(Person &p1,Person &p2);
    friend Person operator-(Person &p1,Person &p2);
    friend Person operator*(Person &p1,Person &p2);
    friend Person operator/(Person &p1,Person &p2);
    friend Person operator%(Person &p1,Person &p2);
    /*------------------------------------*/
    //条件运算符
    bool operator>=(const Person &other);
    bool operator<=(const Person &other);
    bool operator==(const Person &other);
    //逻辑
    bool operator&&(const Person &other);
    //自增/自减
    Person operator--(int);//成员函数后--
    friend Person operator++(Person &p);//全局前++
 
    //插入/输出
    friend ostream &operator<<(ostream &out,Person &p1);
    friend istream &operator>>(istream &out,Person &p1);
    void show();
};
void Person::show()
{
    cout<<age<<endl;
}
/*--------------------类外定义------------------*/
Person Person::operator+(const Person &other)
{
    string s = "a";
    Person temp(1,s);
    temp.age = this->age+other.age;
 
    return temp;
}
Person Person::operator-(const Person &other)
{
    string s = "a";
    Person temp(1,s);
    temp.age = this->age-other.age;
 
    return temp;
}
Person Person::operator*(const Person &other)
{
    string s = "a";
    Person temp(1,s);
    temp.age = this->age*other.age;
 
    return temp;
}
Person Person::operator/(const Person &other)
{
    string s = "a";
    Person temp(1,s);
    temp.age = this->age/other.age;
 
    return temp;
}
Person Person::operator%(const Person &other)
{
    string s = "a";
    Person temp(1,s);
    temp.age = this->age%other.age;
 
    return temp;
}
 
bool Person::operator>=(const Person &other)
{
    if(this->age >= other.age)
    {
        return this->age >= other.age;
    }else
    {
        return this->age >= other.age;
    }
}
bool Person::operator<=(const Person &other)
{
    if(this->age <= other.age)
    {
        return this->age <= other.age;
    }else
    {
        return this->age <= other.age;
    }
}
bool Person::operator==(const Person &other)
{
   return this->age==other.age;;
}
//逻辑
bool Person::operator&&(const Person &other)
{
    return  (this->age) && other.age;
}
//全局 前自增
Person operator++(Person &p)
{
    ++p.age;
    return p;
}
//成员函数后自减
Person Person::operator--(int)
{
    string s = "a";
    Person temp(1,s);
    temp.age = this->age--;
    return temp;
 
}
//
ostream &operator<<(ostream &out,Person &p1)
{
    out<<"age="<<p1.age;
    return out;
}
istream &operator>>(istream &in,Person &p1)
{
    in>>p1.age;
    return in;
}
/*----------------全局运算符--------------------*/
Person operator+(Person &p1,Person &p2)
{
    string s = "a";
    Person temp(1,s);
    temp.age = p1.age+p2.age;
    return temp;
}
Person operator-(Person &p1,Person &p2)
{
    string s = "a";
    Person temp(1,s);
    temp.age = p1.age-p2.age;
    return temp;
}
Person operator*(Person &p1,Person &p2)
{
    string s = "a";
    Person temp(1,s);
    temp.age = p1.age*p2.age;
    return temp;
}
Person operator/(Person &p1,Person &p2)
{
    string s = "a";
    Person temp(1,s);
    temp.age = p1.age/p2.age;
    return temp;
}
Person operator%(Person &p1,Person &p2)
{
    string s = "a";
    Person temp(1,s);
    temp.age = p1.age%p2.age;
    return temp;
}
/*--------------------------------------------------------------*/
class  Stu
{
    double *score;
public:
    //构造函数
    Stu (double score):score(new double(score))
    {
        cout<<"Stu的构造函数"<<endl;
    }
    //析构函数
    ~Stu ()
    {
        delete score;
        cout<<"S的析构函数"<<endl;
    }
    //拷贝构造函数
    Stu (const Stu &other):score(other.score)
    {
        cout<<"S的构造函数"<<endl;
    }
    //拷贝赋值
    Stu &operator =(const Stu &other)
    {
        *(this->score)=*(other.score);
        cout<<"S的拷贝赋值"<<endl;
        return *this;
    }
    void show();
};
void Stu::show()
{
    cout<<*score<<endl;
}
int main()
{
    string cc="lls";
    string dd="sll";
 
    Person p1(18,cc);
    Person p2(12,dd);
    cout<<p1<<"" <<p2<<"lllll"<<endl;
    cin>>p1>>p2;
    p1.show();
    p2.show();
//    Person p3 = operator*(p1,p2);
//    p3 = p1.operator*(p2);
//    cout<<p1.operator&&(p2)<<endl;
//    Person p3(1,dd);
//    p3 = p1.operator--(3);
//    p3 = operator++(p1);
//    p1.show();
//    p3.show();
//    p1.show();
//    cout<<"---------------"<<endl;
//    Person p2=p1;
//    p2.show();
//    cout<<"---------------"<<endl;
//    Person p3(17,dd);
//    p3.show();
//    p3 = p2;
//    p3.show();
//    Stu s1(98.9);
//    s1.show();
//    cout<<"---------------"<<endl;
//    Stu s2(s1);
//    s2.show();
//    cout<<"---------------"<<endl;
//    Stu s3(60);
//    s3.show();
//    s3 = s2;
//    s3.show();
 
    return 0;
}
 

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值