c++day3
2023-12-29 10:07:20
#include <iostream>
using namespace std;
class Person
{
int *age;
string &name;
public:
Person(int age,string &name):age(new int(age)),name(name)
{
cout<<"Person两个参数的有参构造函数"<<endl;
}
Person(const Person &other):age(new int(*(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()
{
cout<<"Person析构函数"<<endl;
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一个参数有参构造函数"<<endl;
}
Stu(const Stu &other):score(new double(*(other.score))),p1(other.p1)
{
cout<<"Stu拷贝构造函数"<<endl;
}
Stu &operator=(const Stu &other)
{
*(this->score)=*(other.score);
this->p1=other.p1;
cout<<"Stu拷贝赋值函数"<<endl;
return *this;
}
~Stu()
{
cout<<"Stu析构函数"<<endl;
delete score;
}
void show(Person p1);
};
int Person::get_age()
{
return *age;
}
string Person::get_name()
{
return name;
}
void Stu::show(Person p1)
{
cout<<"年龄"<<p1.get_age()<<endl;
cout<<"姓名"<<p1.get_name()<<endl;
cout<<"成绩"<<*score<<endl;
}
int main()
{
Person p1(18,"张三");
Stu s1(88);
s1.show(p1);
return 0;
}
文章来源:https://blog.csdn.net/2301_79218085/article/details/135282527
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!