前言:我们先来回忆一下面向对象的三大特性。1.封装(更好的管理) 2.继承(是一种复用) 3. 多态(后续讲解,一种形态,不同的对象去完成时会产生不同的状态),今天就详细说下继承
一、继承的概念及定义
继承可以简单的理解为复用,允许程序在保持原有类特性的基础上进行扩展,增加新的功能,这样产生新的类称为派生
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
void Print() {
cout << "name: " << _name << endl;
cout << "age: " << _age << endl;
}
protected:
string _name = "henry";
int _age = 18;
};
class Student : public Person
{
protected:
int _stu_id;
};
class Teacher : public Person {
protected:
int _work_id;
};
int main() {
Student s;
Teacher t;
s.Print();
t.Print();