#include <iostream>
using namespace std;
class Per
{
private:
string name;
int age;
int *height;
double *weight;
public:
//无参
Per(){cout << "Per::无参构造函数" << endl;}
//有参
Per(string name,int age,int height,double weight):name(name),age(age),height(new int(height)),weight(new double(weight))
{
cout << "Per::有参构造函数" << endl;
}
~Per()
{
cout << "Per::析构函数" << endl;
delete(height);
delete(weight);
}
void show()
{
cout << name << " " << age << " " << *height << " " << *weight << " " << endl;
}
};
class Stu
{
private:
double score;
Per p1;
public:
//无参
Stu(){cout << "Per::无参构造函数" << endl;}
//有参
Stu(double score,string name,int age,int height,double weight):score(score),p1(name,age,height,weight)
{
cout << "Stu::有参构造函数" << endl;
}
~Stu()
{
cout << "Stu::析构函数" << endl;
}
void show()
{
cout << score << " ";
p1.show();
}
};
int main()
{
Per p1;
Per p2("zzk",23,187,75);
Stu s1;
Stu s2(99.9,"llc",24,191,80);
p2.show();
s2.show();
return 0;
}
c++day3作业
最新推荐文章于 2024-07-17 18:07:42 发布