Section No. 01: Inheritance
Review
Object-oriented 4 features:
A (abstract) P (polymorphic) I (inheritance) E (encapsulation)
First two units: AE
This module: PI
Section No. 02: Constructors and destructors
Members inherited by derived classes
Do derived classes inherit ctor and Dtor?
Special functions not inherited by derived classes
Constructor (C++11 allowed inheritance)
Destructors
Friend function as privileged position
Assignment operator functions
#include <iostream>struct a{a (int i) {}a (double d, int i) {}//...}; struct b:a{using a::a;//Inherit base class constructor int d{0};//New variable initialization method};int main () {b b (1);//B.D initialized to 0}
Calling base class constructors (call base constructor)
Ctors of base class can is invoked from the constructors of the derived classes. (a base class constructor can only be called by a derived class constructor)
The syntax to invoke it is as follows:
DerivedClass (parameterlist): BaseClass () {//Perform initialization}//Orderivedclass (parameterlist): BaseClass ( ArgumentList) {//Perform initialization}
No-arg Constructor in base class (non-parametric constructors for base classes)
Rules for invoke constructors in derived class
A constructor in a derived class must always invoke a constructor on its base class. (The derived class constructor must call the base class constructor)
If a base constructor is not invoked explicitly, the base class ' s No-arg constructor are invoked by default. (The Destructors class ctor is not explicitly called, and the parameterless constructor of the base class is called)
Constructor and destructor Chaining (construction and destructor chain)
Constructor chaining (constructor chain)
Constructing an instance of a class invokes all the base class along the inheritance chain. (A constructed class instance invokes all base classes along the inheritance chain ctor)
Invoke Sequence:base First, derive next
destructor Chaining (destructor chain)
Conversely, the destructors is automatically invoked in reverse order (Dtor and ctor are just the opposite)
Invoke sequence:derive First, base next
No-arg Constructor (no parameter constructor)
If A class is designed to be extended, provide a no-arg constructor. (If your class wants to be extended by others, then provide a parameterless constructor)
34. File extension: Header files with. h, source files with. cpp (c + +, CC also available)
A class should is declared in a header file and defined in a source file where the name of the files match the name of The class.
35. The class should be declared in the header file and defined in the source file, the two file names should be the same as the class name
Example: MyClass.h, myclass.c++
The exception to this is that the declaration and definition of the template class are placed in the header file
The. Class variables should never is declared public.
49. Class member variables cannot be declared public
Description: Public variables violate the information hiding principle of C + +. The exception to this is that if class is just a data structure, similar to a struct in C, you can declare class variables as public
#include <iostream>class fruit{public:fruit () {}fruit (int id) {}std::string s;}; Class Apple:public Fruit{public:apple (): Fruit () {}};int main () {Apple Apple;}
Section No. 03: function redefinition
Section No. 04: Polymorphism and Virtual functions
Section No. 05: Access Control (visibility control)
Section No. 06: Abstract classes and pure virtual functions
Section No. 07: Dynamic Type Conversions
Unit 6th Job "2"-online programming (difficulty: Medium) Submission deadline: October 7, 2016 23:30/pending submission phase
Unit 6th Job "1"-Online programming (difficulty: Easy) Submission deadline: October 7, 2016 23:30/pending submission phase
Introduction to the _c++ program design of NetEase Cloud Classroom (top) _ 6th: Dan Feng, although old and still polymorphic-inheritance and polymorphism