12.29作业

  1. 整理思维导图
  2. 定义一个Person类,私有成员int age,string &name,定义一个Stu类,包含私有成员double *score,写出两个类的构造函数、析构函数、拷贝构造和拷贝赋值函数,完成对Person的运算符重载(算术运算符、条件运算符、逻辑运算符、自增自减运算符、插入/提取运算符)

2

#include 
//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的拷贝构造函数"<age=other.age;
      this->name=other.name;
      cout<<"Person的拷贝赋值函数"< (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.agep2.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<
#include 

using namespace std;
class  Stu
{
    double *score;

public:
    //Stu有参构造
    Stu(double *score):score(score)
    {
        cout<<"Stu的有参构造"<score)=*(other.score);
        cout<<"Stu拷贝赋值"<

你可能感兴趣的:(c++)