1.整理思维导图
2.整理上课代码,完成优化
Person类,私有成员(姓名,年龄,身高,体
重),公有成员方法(有参构造函数、析构函数、
show函数)
Stu类,继承派生自Person类,私有成员(成
绩,班级),公有成员方法(有参构造函数、析构函
数、show函数),实例化一个Stu对象并调用show
函数
#include <iostream>
using namespace std;
class Person
{
string name;
int age;
int high;
int weight;
public:
// Person(){cout << "Person无参构造" << endl;}
Person(string name,int age,int high,int weight):name(name),age(age),high(high),weight(weight)
{
this->name = name;
this->age = age;
this->high = high;
this->weight = weight;
cout << "Person有参构造" << endl;
}
~Person()
{
cout << "Person析构函数" << endl;
}
void show()
{
cout << name << age << high << weight << endl;
}
};
class Stu:public Person
{
int score;
string class_;
public:
// Stu(){cout << "Stu无参构造" << endl;}
Stu(int score,string class_,string name,int age,int high,int weight):Person(name,age,high,weight)
{
this->score = score;
this->class_ = class_;
}
~Stu()
{
cout << "Stu析构函数" << endl;
}
void show()
{
cout << "分数 " << score << "班级 " << class_ << endl;
}
};
int main()
{
Stu s1(88,"三二","zz",18,180,70);
s1.Person::show();
s1.show();
return 0;
}
运行结果:
Person有参构造
zz 18 180 70
分数 88 班级 三二
Stu析构函数
Person析构函数