【C++重载】

发布时间:2024年01月02日
#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;
    }

    Person operator+(Person &other);
    Person operator-(Person &other);
    Person operator*(Person &other);
    Person operator/(Person &other);
    friend Person 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 bool operator||(Person &c1, Person &c2);
    void operator()();
    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 n = "";
Person temp(0, n);
Person Person::operator+(Person &other)
{
    temp.age = this->age + other.age;
    temp.name = this->name;
    return temp;
}

Person Person::operator-(Person &other)
{
    temp.age = this->age - other.age;
    temp.name = this->name;
    return temp;
}

Person Person::operator*(Person &other)
{
    temp.age = this->age * other.age;
    temp.name = this->name;
    return temp;
}

Person Person::operator/(Person &other)
{
    temp.age = this->age / other.age;
    temp.name = this->name;
    return temp;
}

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;
}

ostream &operator<<(ostream &out, Person &c1)
{
    out << c1.age << " " << c1.name;
    return out;
}

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 << "Stu的析构函数" << endl;
    }
    Stu(const Stu &other) : score(new double(*(other.score)))
    {
        cout << "Stu的拷贝构造函数" << endl

#include <iostream>
//2.写出两个类的构造函数、析构函数、拷贝构造和拷贝赋值函数,
//完成对Person的运算符重载(算术运算符、
//条件运算符、逻辑运算符、自增自减运算符、插入/提取运算符)
using namespace std;
string n1;
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;
  }
  friend Person operator+(Person &p1,Person &p2);
  friend bool operator > (Person &p1,Person &p2);
  friend bool operator && (Person &p1,Person &p2);
  friend Person operator ++(Person &p1);
  friend ostream &operator <<(ostream &out,Person &p1);
   operator int ()
  {
      return this->age;
  }
};
Person operator + (Person &p1,Person &p2)
{
    Person temp(1,n1);
    temp.age=p1.age+p2.age;
    temp.name=p1.name+p2.name;
    return temp;
}
bool operator > (Person &p1,Person &p2)
{
    if(p1.age>p2.age)
    {
        return p1.age>p2.age;
    }else if(p1.age==p2.age)
    {
        return p1.name>p2.name;
    }else if(p1.age<p2.age)
    {
        return p1.age>p2.age;
    }
}
bool operator && (Person &p1,Person &p2)
{
    return (p1.age==p2.age)||(p1.name==p2.name);
}
Person operator++(Person &p1)
{
    ++(p1.age);
    return p1;
}
ostream &operator<<(ostream &out,Person &p1)
{
    out<<p1.age<<":"<<p1.name<<endl;
    return out;
}
int main()
{
 
    return 0;
}
#include <iostream>
 
using namespace std;
class  Stu
{
    double *score;
 
public:
    //Stu有参构造
    Stu(double *score):score(score)
    {
        cout<<"Stu的有参构造"<<endl;
    }
    //Stu析构
    ~Stu()
    {
        cout<<"Stu的析构"<<endl;
        delete score;
    }
    //Stu拷贝构造
    Stu(const Stu &other):score(new double(*(other.score)))
    {
        cout<<"Stu拷贝构造"<<endl;
    }
    //Stu拷贝赋值
    Stu &operator =(const Stu& other)
    {
        *(this->score)=*(other.score);
        cout<<"Stu拷贝赋值"<<endl;
    }
};
int main()
{
    cout << "Hello World!" << endl;
    return 0;
}

在这里插入图片描述

文章来源:https://blog.csdn.net/jpk010820/article/details/135332709
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。