面向对象程序设计中最重要的一个概念是继承 (inheritance). 继承允许我们依据另一个类来定义一个类, 这使得创建和维护一个应用程序变得更统一. 这样做也达到了重用代码功能和提高执行效率的效果.
一个类中包含了若干数据成员和成员函数. 不同的类中的数据成员和成员函数各不相同. 但是有时两个类的内容基本相同. 例如:
继承 (inheritance) 就是在一个已存在的类的基础上建立一个新的类.
一个新类从已有的类获得其已有特性, 称为类的继承.
从已有的类 (父类) 产生一个新的子类, 称为类的派生.
单继承 (single inheritance) 指一个派生类只从一个基类派生.
多重继承 (multiple inheritance) 是指一个派生类有两个或多个基类. 派生类不仅可以从一个基类派生, 也可以从多个基类派生.
派生类的声明方式:
class 派生类名:[继承方式]基类名{ 派生类新增加的成员 };
成员访问限定符 (默认 private):
继承方式包括 (默认 private):
Student 类:
#ifndef PROJECT5_STUDENT_H #define PROJECT5_STUDENT_H #include <string> using namespace std; class Student { protected: int number; string name; char sex; public: Student(int num, string n, char s); void show(); }; #endif //PROJECT5_STUDENT_H
Student.cpp:
#include <iostream> #include "Student.h" using namespace std; Student::Student(int num, string n, char s) { number = num; name = n; sex = s; } void Student::show() { cout << "number: " << number << endl; cout << "name: " << name << endl; cout << "sex: " << sex << endl; }
Student 派生类:
#ifndef PROJECT5_STUDENT1_H #define PROJECT5_STUDENT1_H #include "Student.h" class Student1:public Student { private: int age; string address; public: Student1(int num, string n, char s, int a, string addr); void show1(); }; #endif //PROJECT5_STUDENT1_H
Student1.cpp:
#include <iostream> #include "Student1.h" using namespace std; Student1::Student1(int num, string n, char s, int a, string addr) : Student(num, n, s) { Student(num, n, s); age = a; address = addr; } void Student1::show1() { show(); cout << "age: " << age << endl; cout << "address: " << address << endl; }
mian:
#include <iostream> #include "Student1.h" int main() { Student1 student1(1, "Little White", 'f', 18, "火星"); student1.show1(); return 0; }
输出结果:
number: 1 name: Little White sex: f age: 18 address: 火星
派生类中的成员包括从基类继承过来的成员和自己增加的成员两大部分. 每一部分布分别包括数据成员和成员函数.
构造函数和析构函数:
构造函数的主要作用是对数据成员初始化析构函数在释放对象前做一些相关的处理
因为派生类还继承了基类的数据成员. 设计派生类的构造函数时, 不仅要考虑派生类所增加的数据成员的初始化, 还应当考虑基类的数据成员初始化. 于是我们在执行派生类的构造函数时, 调用基类的构造函数.
派生类构造函数名 (总形式参数表列) : 基类构造函数名 (实际参数表列) { 派生类中新增数据成员初始化语句 }
在类内定义派生类构造函数:
Student1::Student1(int num, string n, char s, int a, string addr) : Student(num, n, s), age(a), address(addr) {}
在类的外面定义派生类构造函数:
类内: Student1(int num, string n, char s, int a, string addr); 类外: Student1::Student1(int num, string n, char s, int a, string addr) : Student(num, n, s) { Student(num, n, s); // 基类 age = a; address = addr; }
建立派生类对象时, 执行构造函数的顺序:
在派生类对象释放时:
Base 类:
#ifndef PROJECT5_BASE_H #define PROJECT5_BASE_H class Base { protected: Base(); ~Base(); }; #endif //PROJECT5_BASE_H
Base.cpp:
#include <iostream> #include "Base.h" using namespace std; Base::Base() { cout << "基类构造" << endl; } Base::~Base() { cout << "基类析构" << endl; }
Derived 类:
#ifndef PROJECT5_DERIVED_H #define PROJECT5_DERIVED_H #include "Base.h" using namespace std; class Derived: public Base{ public: Derived(char c); ~Derived(); }; #endif //PROJECT5_DERIVED_H
Derived.cpp:
#include <iostream> #include "Derived.h" using namespace std; Derived::Derived(char c) { cout << "子类构造函数, 值:" << c << endl; } Derived::~Derived() { cout << "子类析构函数" << endl; }
main:
#include <iostream> #include "Derived.h" using namespace std; Derived::Derived(char c) { cout << "子类构造函数, 值:" << c << endl; } Derived::~Derived() { cout << "子类析构函数" << endl; }
输出结果:
基类构造 子类构造函数, 值:b 子类析构函数 基类析构
子对象 (sub object), 即对象中的对象. 类的数据成员是另一个类的对象.
Student1 类:
#ifndef PROJECT5_STUDENT1_H #define PROJECT5_STUDENT1_H #include "Student.h" class Student1:public Student { private: int age; string address; Student president; public: Student1(int num, string n, char s, int p_num, string p_n, char p_s, int a, string addr); void show1(); }; #endif //PROJECT5_STUDENT1_H
Student1.cpp:
#include <iostream> #include "Student1.h" using namespace std; Student1::Student1(int num, string n, char s, int p_num, string p_n, char p_s, int a, string addr) : Student(num, n, s), president(p_num, p_n, p_s) { age = a; address = addr; } void Student1::show1() { show(); cout << "age: " << age << endl; cout << "address: " << address << endl; cout << "==========班长信息==========" << endl; president.show(); }
main:
#include <iostream> #include "Student1.h" using namespace std; int main() { Student1 student1(1, "Little White", 'f', 2, "班长", 'm', 18, "火星"); student1.show1(); return 0; }
输出结果:
number: 1 name: Little White sex: f age: 18 address: 火星 ==========班长信息========== number: 2 name: 班长 sex: m