c++day3

#include 
using namespace std;
class Person
{
    int *age;
    string &name;
public:
    Person(int age,string &name):age(new int(age)),name(name)
    {
        cout<<"Person两个参数的有参构造函数"< 
  
    }
    Person(const Person &other):age(new int(*(other.age))),name(other.name)
    {
        cout<<"Person拷贝构造函数"< 
  
    }
    Person&operator=(const Person &other)
    {
        *(this->age)=*(other.age);
        this->name=other.name;
        cout<<"Person拷贝赋值函数"< 
  
        return *this;
    }
    ~Person()
    {
        cout<<"Person析构函数"< 
  
        delete age;
    }
    int get_age();
    string get_name();
};
 
  
 
  
class Stu
{
    double *score;
    Person p1;
public:
    Stu(double score):score(new double(score)),p1(p1)
    {
        cout<<"Stu一个参数有参构造函数"< 
  
    }
    Stu(const Stu &other):score(new double(*(other.score))),p1(other.p1)
    {
        cout<<"Stu拷贝构造函数"< 
  
    }
    Stu &operator=(const Stu &other)
    {
        *(this->score)=*(other.score);
        this->p1=other.p1;
        cout<<"Stu拷贝赋值函数"< 
  
        return *this;
    }
    ~Stu()
    {
        cout<<"Stu析构函数"< 
  
        delete score;
    }
    void show(Person p1);
};
int Person::get_age()
{
    return *age;
}
string Person::get_name()
{
    return name;
}
void Stu::show(Person p1)
{
    cout<<"年龄"< 
  
    cout<<"姓名"< 
  
    cout<<"成绩"<<*score< 
  
}
int main()
{
    Person p1(18,"张三");
    Stu s1(88);
    s1.show(p1);
    return 0;
}

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