C++day4

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

#include 
#include 
using namespace std;


class Person
{
    int age;
    string &name;
 public:
    //构造函数

    Person(int age,string &name):name(name)
    {
        cout<<"P的构造函数"<age = age;
    }
    //析构函数
    ~Person()
    {
        cout<<"P的析构函数"<age = other.age;
        this->name = other.name;
        cout<<"P的拷贝赋值函数"<=(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+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="<>(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的构造函数"<score)=*(other.score);
        cout<<"S的拷贝赋值"<>p1>>p2;
    p1.show();
    p2.show();
//    Person p3 = operator*(p1,p2);
//    p3 = p1.operator*(p2);
//    cout<

思维导图

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