#include <iostream>
using namespace std;
class per
{
string name;
int age;
int *high;
int *weight;
public:
//构造函数
per():high(new int),weight(new int)
{
cout << "调用per无参构造函数" << endl;
}
per(int high,int weight)
{
this->high = new int(high);
this->weight = new int(weight);
cout << "调用per有参构造函数" << endl;
}
per(string name,int age,int high,int weight):name(name),age(age)
{
this->high = new int(high);
this->weight = new int(weight);
cout << "调用per有参构造函数" << endl;
}
//拷贝构造函数
per(per &other)
{
this->name = other.name;
this->age = other.age;
this->high = new int(*(other.high));
this->weight = new int(*(other.weight));
cout << "调用per拷贝构造函数" << endl;
}
//析构函数
~per()
{
delete high;
delete weight;
cout << "调用per析构函数" << endl;
}
//设置、获取各个类成员
void set_high(int high)
{
*(this->high) = high;
}
int get_high()
{
return *high;
}
void set_weight(int weight)
{
*(this->weight) = weight;
}
int get_weight()
{
return *weight;
}
void set_name(string name)
{
this->name = name;
}
string get_name()
{
return name;
}
void set_age(int age)
{
this->age = age;
}
int get_age()
{
return age;
}
//输出地址
void showaddr()
{
cout << "high = " << high << "\tweight = " << weight << endl;
}
//输出打印值
void show()
{
cout << "名字为" << name << "年龄为" << age << "身高为" << *high << "体重为" << *weight << endl;
}
};
class stu
{
int score;
per p1;
public:
stu():p1(0,0)
{
cout << "调用stu无参构造函数" << endl;
}
stu(int score,per p1):score(score),p1(p1)
{
cout << "调用stu有参构造函数" << endl;
}
stu(int score,string name,int age,int high,int weight):score(score),p1(name,age,high,weight)
{
cout << "调用stu有参构造函数" << endl;
}
stu(stu &other)
{
this->score = other.score;
this->p1.set_high(other.p1.get_high());
this->p1.set_weight(other.p1.get_weight());
this->p1.set_name(other.p1.get_name());
this->p1.set_age(other.p1.get_age());
}
~stu()
{
cout << "调用stu析构函数" << endl;
}
per get_per()
{
// cout << &p1 << endl;
return p1;
}
void show()
{
p1.show();
cout << "成绩为" << score << "\t";
}
};
int main()
{
per p1(180,80);
p1.show();
p1.showaddr();
stu s1(10,"zhangsan",18,180,70);
s1.show();
s1.get_per().show();
s1.get_per().showaddr();
return 0;
}
思维导图