C++中繼承(inheritance)詳解及其作用介紹
概述
面向?qū)ο蟪绦蛟O(shè)計中最重要的一個概念是繼承 (inheritance). 繼承允許我們依據(jù)另一個類來定義一個類, 這使得創(chuàng)建和維護(hù)一個應(yīng)用程序變得更統(tǒng)一. 這樣做也達(dá)到了重用代碼功能和提高執(zhí)行效率的效果.
類的概念
一個類中包含了若干數(shù)據(jù)成員和成員函數(shù). 不同的類中的數(shù)據(jù)成員和成員函數(shù)各不相同. 但是有時兩個類的內(nèi)容基本相同. 例如:
繼承的概念
繼承 (inheritance) 就是在一個已存在的類的基礎(chǔ)上建立一個新的類.
- 已存在的類: 基類 (base class) 或父類 (father class)
- 新建立的類: 派生類 (derived class) 或子類 (son class)
一個新類從已有的類獲得其已有特性, 稱為類的繼承.
- 通過繼承, 一個新建的子類從已有的父類那里獲得父類的特性
- 派生類繼承了基類的所有數(shù)據(jù)成員和成員函數(shù), 并可以對成員做必要的增加或調(diào)整
從已有的類 (父類) 產(chǎn)生一個新的子類, 稱為類的派生.
- 類的繼承是用已有的類來建立專用新類的編程技術(shù)
- 一個基類可以派生出多個派生類, 每一個派生類又可以作為基類再派生出新的派生類. 因此基類和派生類是相對而言的
- 派生類是基類的具體化, 而基類則是派生類的抽象
單繼承
單繼承 (single inheritance) 指一個派生類只從一個基類派生.
- 單繼承關(guān)系形成的層次是一個樹形結(jié)構(gòu)
- 箭頭表示繼承的方向, 從派生類指向基類
多重繼承
多重繼承 (multiple inheritance) 是指一個派生類有兩個或多個基類. 派生類不僅可以從一個基類派生, 也可以從多個基類派生.
派生類
派生類的聲明方式:
class 派生類名:[繼承方式]基類名{ 派生類新增加的成員 };
成員訪問限定符 (默認(rèn) private):
- public (公用的)
- private (私有的)
- protected (受保護(hù)的)
繼承方式包括 (默認(rèn) private):
- public (公用的)
- private (私有的)
- protected (受保護(hù)的)
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; }
輸出結(jié)果:
number: 1 name: Little White sex: f age: 18 address: 火星
派生類中的成員包括從基類繼承過來的成員和自己增加的成員兩大部分. 每一部分布分別包括數(shù)據(jù)成員和成員函數(shù).
派生類的構(gòu)造函數(shù)和析構(gòu)函數(shù)
構(gòu)造函數(shù)和析構(gòu)函數(shù):
構(gòu)造函數(shù)的主要作用是對數(shù)據(jù)成員初始化析構(gòu)函數(shù)在釋放對象前做一些相關(guān)的處理
因為派生類還繼承了基類的數(shù)據(jù)成員. 設(shè)計派生類的構(gòu)造函數(shù)時, 不僅要考慮派生類所增加的數(shù)據(jù)成員的初始化, 還應(yīng)當(dāng)考慮基類的數(shù)據(jù)成員初始化. 于是我們在執(zhí)行派生類的構(gòu)造函數(shù)時, 調(diào)用基類的構(gòu)造函數(shù).
派生類構(gòu)造函數(shù)一般形式
派生類構(gòu)造函數(shù)名 (總形式參數(shù)表列) : 基類構(gòu)造函數(shù)名 (實際參數(shù)表列) { 派生類中新增數(shù)據(jù)成員初始化語句 }
類內(nèi)定義
在類內(nèi)定義派生類構(gòu)造函數(shù):
Student1::Student1(int num, string n, char s, int a, string addr) : Student(num, n, s), age(a), address(addr) {}
類外定義
在類的外面定義派生類構(gòu)造函數(shù):
類內(nèi): 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; }
構(gòu)造函數(shù)和析構(gòu)函數(shù)執(zhí)行的順序
建立派生類對象時, 執(zhí)行構(gòu)造函數(shù)的順序:
- 派生類構(gòu)造函數(shù)先調(diào)用基類構(gòu)造函數(shù)
- 再執(zhí)行派生類構(gòu)造函數(shù)本身 (即派生類構(gòu)造函數(shù)的函數(shù)體)
在派生類對象釋放時:
- 先執(zhí)行派生類析構(gòu)函數(shù) ~Derived()
- 再執(zhí)行其基類析構(gòu)函數(shù) ~Base()
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 << "基類構(gòu)造" << endl; } Base::~Base() { cout << "基類析構(gòu)" << 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 << "子類構(gòu)造函數(shù), 值:" << c << endl; } Derived::~Derived() { cout << "子類析構(gòu)函數(shù)" << endl; }
main:
#include <iostream> #include "Derived.h" using namespace std; Derived::Derived(char c) { cout << "子類構(gòu)造函數(shù), 值:" << c << endl; } Derived::~Derived() { cout << "子類析構(gòu)函數(shù)" << endl; }
輸出結(jié)果:
基類構(gòu)造 子類構(gòu)造函數(shù), 值:b 子類析構(gòu)函數(shù) 基類析構(gòu)
子對象派生
子對象 (sub object), 即對象中的對象. 類的數(shù)據(jù)成員是另一個類的對象.
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; }
輸出結(jié)果:
number: 1 name: Little White sex: f age: 18 address: 火星 ==========班長信息========== number: 2 name: 班長 sex: m
注意事項
- 當(dāng)不需要對派生類新增的成員函數(shù)進(jìn)行任何初始化操作時, 派生類構(gòu)造函數(shù)體可以為空
- 基類沒有構(gòu)造函數(shù)或構(gòu)造函數(shù)參數(shù)為空, 在派生類構(gòu)造函數(shù)中可不寫調(diào)用基類構(gòu)造函數(shù)的語句, 盜用派生類構(gòu)造函數(shù)時系統(tǒng)會自動調(diào)用基類的默認(rèn)構(gòu)造函數(shù)
- 基類中定義了有參的構(gòu)造函數(shù), 派生類構(gòu)造函數(shù)總必須寫出基類的構(gòu)造函數(shù)及其參數(shù)
- 基類中既定義無參數(shù)的構(gòu)造函數(shù),又重載了有參數(shù)的構(gòu)造函數(shù), 派生類構(gòu)造函數(shù)中可以調(diào)用帶參的基類構(gòu)造函數(shù), 也可以不調(diào)用基類的構(gòu)造函數(shù)
到此這篇關(guān)于C++中繼承(inheritance)詳解及其作用介紹的文章就介紹到這了,更多相關(guān)C++繼承內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Sublime Text 3 實現(xiàn)C語言代碼的編譯和運行(示例講解)
下面小編就為大家?guī)硪黄猄ublime Text 3 實現(xiàn)C語言代碼的編譯和運行(示例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09C語言之沒有main函數(shù)的helloworld示例
這篇文章主要介紹了C語言之沒有main函數(shù)的helloworld示例,本文分解了帶main函數(shù)的helloworld示例,從而分析出不需要main函數(shù)的helloworld示例,需要的朋友可以參考下2015-03-03關(guān)于C++出現(xiàn)Bus error問題的排查與解決
項目代碼中經(jīng)常出現(xiàn)莫名其妙的Bus error問題,并且代碼中增加很多try catch 后依然不能將錯誤捕獲,一旦Bus erro出現(xiàn),進(jìn)程直接崩潰掉,所以本文給大家介紹了關(guān)于C++出現(xiàn)Bus error問題的排查與解決,需要的朋友可以參考下2024-01-01關(guān)于C++靜態(tài)成員函數(shù)訪問非靜態(tài)成員變量的問題
靜態(tài)成員函數(shù)不能訪問非靜態(tài)成員,這是因為靜態(tài)函數(shù)屬于類而不是屬于整個對象,靜態(tài)函數(shù)中的 member可能都沒有分配內(nèi)存。靜態(tài)成員函數(shù)沒有隱含的this自變量。所以,它就無法訪問自己類的非靜態(tài)成員2013-10-10