c++ / day04

1. 整理思维导图

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

#include 

using namespace std;

class Person
{
    int *_ptr_age;
    string &_ref_name;
    int happiness;
public:
    //Person constructor,
    Person(int age, string &ref_name):_ptr_age(new int(age)), _ref_name(ref_name)
    {
        cout << "Person constructor with 2 params" << endl;
    }

    //destructor
    ~Person()
    {
       cout << "Person destructor" << endl;
       delete _ptr_age;

    }

    //copy constructor. Note: param need to be initilized for space
    Person(const Person &src):_ptr_age(new int(*(src._ptr_age))), _ref_name(src._ref_name)
    {
        cout << "Person copy constructor" << endl;
    }

    //assign constructor. Note it has been initilized, has space alread, just need copy value
    Person &operator=(const Person &src)
    {
        this->_ptr_age =src._ptr_age;
        this->_ref_name = src._ref_name;
      cout << "Person assign constructor" << endl;
      return *this;
    }

    //arithmetic operator overloading
    Person operator+(Person &other );

    //condition operator overloading
    bool operator>(Person &other);

    //loginal operator overloading
    bool operator&&(Person &other);

    //inc/dec operation overloading;
    Person operator++();

    //cout operation overloading;
    friend ostream &operator<<(ostream &out, Person &person);



    int get_age();
    int get_happiness();
    void set_happiness(int h);
    string get_name();
};

Person Person::operator+(Person &other)
{
    Person person(*this->_ptr_age, this->_ref_name);
    person.happiness = this->happiness + other.happiness;
    return person;
}

bool Person::operator>(Person &other)
{
    if(this->happiness > other.happiness)
    {
        return true;
    }
    else {
        return false;
    }
}

bool Person::operator&&(Person &other)
{
    if(this->happiness && other.happiness)
    {
        return true;
    }else{
        return false;
    }
}

Person Person::operator++()
{
    ++this->happiness;
    return *this;
}

ostream &operator<<(ostream &out, Person &person)
{
    out << "happiness value:" << person.get_happiness() << endl;
    return out;
}

class Stu
{
    double *_ptr_score;
    Person _person;

public:
    //constuctor
    Stu(double score, int age, string name):_ptr_score(new double (score)), _person(age, name)
    {
        cout << "Stu constructor with 2 params" << endl;
    }
    //destructor
    ~Stu()
    {
       cout << "Stu destructor" << endl;
       delete _ptr_score;
    }
    //copy constructor
    Stu(const Stu &src):_ptr_score(new double(*(src._ptr_score))), _person(src._person)
    {
        cout << "Stu copy constructor" << endl;
    }

    //assign constructor
    Stu &operator = (const Stu &src)
    {
        this->_ptr_score = src._ptr_score;
        this->_person = src._person;
        return *this;
    }

    void show();
};

int Person::get_age()
{
    return *_ptr_age;
}

string Person::get_name()
{
    return _ref_name;
}

int Person::get_happiness()
{
    return happiness;
}
void Person::set_happiness(int h)
{
    this->happiness = h;
}

void Stu::show()
{
    cout << "score = "<< *_ptr_score << endl;
    cout << "person age = "<< _person.get_age() << endl;
    cout << "person name = "<< _person.get_name() << endl;

}


int main()
{
    int age_a = 28;
    int age_b = 29;
    string name_z = "zhangsan";
    string name_l ="lisi";

    //double score_a = 99;
    //double score_b =98;

    Person person_zs(age_a, name_z);
    Person person_ls(age_b, name_l);
    person_zs.set_happiness(10);
    person_ls.set_happiness(9);


    ++person_zs ;
    cout << "++person_zs = "<< person_zs.get_happiness() << endl;

    cout << "person_zs + person_ls:"<< (person_zs + person_ls).get_happiness() << endl;

    cout << "person_zs > person_ls:"<< (person_zs > person_ls) << endl;

    person_ls.set_happiness(0);
    cout << "person_zs && person_ls:"<< (person_zs && person_ls) << endl;

    cout << person_zs << "---cout overloadning---"<< endl;


    return 0;
}

 运行结果:

c++ / day04_第1张图片

你可能感兴趣的:(c++,开发语言)