C++引入继承的目的 继承的目的:实现代码重用。 ■派生的目的:当新的问题出现,原有程 序无法解决(或不能完全解决)时,需 要对原有程序进行改造
◼ 继承的目的:实现代码重用。 ◼ 派生的目的:当新的问题出现,原有程 序无法解决(或不能完全解决)时,需 要对原有程序进行改造。 C++引入继承的目的
派生类生成示例 Person Person Student char name [201 char name 20] char name 20 int age Int age Int age int sex int sex int sex Person: Person Person: Person int id[9] GetName GetName float score GetAge GetAge Person: Person Getsex GetSex GetName Display Display GetAge GetSex Student Person: Display int id 9 Geld float score GetScore GetId Display GetScore Display
char name[20] int age int sex GetName GetAge GetSex Display Person::Person Person char name[20] int age int sex GetName GetAge GetSex Person::Display Person::Person int id[9] float score GetId GetScore Display Student char name[20] int age int sex GetName GetAge GetSex Display Person::Person Person int id[9] float score GetId GetScore Display Student 派生类生成示例
派生类的生成过程 分析派生新类的过程可知,派生类的生 成经历了三个步骤: 吸收基类成员; 改造基类成员; ■添加派生类新成员
◼ 分析派生新类的过程可知,派生类的生 成经历了三个步骤: ◼ 吸收基类成员; ◼ 改造基类成员; ◼ 添加派生类新成员。 派生类的生成过程