C++ Language - Session 8: Inheritance (cont) - FPT University
Bạn đang xem 20 trang mẫu của tài liệu "C++ Language - Session 8: Inheritance (cont) - FPT University", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
Tài liệu đính kèm:
- c_language_session_8_inheritance_cont_fpt_university.ppt
Nội dung text: C++ Language - Session 8: Inheritance (cont) - FPT University
- Session 8-Inheritance(2) Derived Classes with Depth Multiple Inheritance Abstract Base Classes Session 8 –Inheritance(2) 1/21
- Objectives • Derived Classes with Depth – Constructors and Destructor – Copy Constructor – Assignment Operator • Multiple Inheritance – Virtual Inheritance • Abstract Base Classes – Pure Virtual Functions – Abstract Base Classes – Virtual Operators Session 8 –Inheritance(2) 2/21
- Derived Classes with Depth • Constructor(s), copy constructor, assignment operator and destructor of derived class called ? • Call the base class ctor, copy ctor, dtor and =op explicitly. Session 8 –Inheritance(2) 3/21
- Constructors and Destructor Demo Session 8 –Inheritance(2) 4/21
- Copy Constructor(1) • A derived class has its own copy constructor. • Derived class copy constructor is distinct from the copy constructor of the base class. • If we don't define a copy constructor for the derived class, the compiler inserts one that performs and shallow copy. Before performing the shallow copy, this constructor calls the base class copy constructor. Session 8 –Inheritance(2) 5/21
- Copy Constructor(2) • The definition of a copy constructor for a derived class: DerivedClass ( const DerivedClass& identifier ) : BaseClass ( identifer ) { // } Session 8 –Inheritance(2) 6/21
- Copy Constructor(3) • The copy constructor for a derived class copies the instance variables of the derived class. • The sequence of steps: – base class copy first • allocate memory for the base class instance variables in the order of their declaration, • execute the base class copy constructor code, – derived class copy next • allocate memory for the derived class instance variables in the order of their declaration, • execute the derived class copy constructor code. Session 8 –Inheritance(2) 7/21
- Successive Calls • The call to the base class copy constructor is to the copy constructor of the immediate (not, ultimate) base class. Session 8 –Inheritance(2) 8/21
- Assignment Operator • The default assignment operator for a derived class performs a shallow member-wise copy and calls the assignment operator for the base class. • If we replace the default operator with our own definition, we need to call the base class operator explicitly. • A call to the base class assignment operator syntax: BaseClass::operator=(identifier); Session 8 –Inheritance(2) 9/21
- Assignment Operator Demo Session 8 –Inheritance(2) 10/21
- Multiple Inheritance • A class can be derived from several base classes. • Syntax: class Derived : base class list { }; Session 8 –Inheritance(2) 11/21
- Multiple Inheritance Demo(1) Session 8 –Inheritance(2) 12/21
- Multiple Inheritance Demo(2) Session 8 –Inheritance(2) 13/21
- VIRTUAL INHERITANCE(1) • To avoid duplication of the instance of the base class, the Box and ColouredShape object can share the same Shape object. • To implement this design, we identify the Shape class as virtual Session 8 –Inheritance(2) 14/21
- VIRTUAL INHERITANCE(2) Demo Session 8 –Inheritance(2) 15/21
- Abstract Base Classes • An abstract class is a class that models a concept. • It holds the form of objects that are to implement the concept. • The class is abstract to the extent that it lacks certain implementation details. • The term interface refers to an abstract class that contains no data members. • To complete an implementation, we derive a new class from its abstract class and add the missing details to the derived class Session 8 –Inheritance(2) 16/21
- Pure Virtual Functions(1) • A pure virtual function is a virtual function that has form but no implementation. • Declaration syntax: virtual type identifier(parameters) = 0; Session 8 –Inheritance(2) 17/21
- Pure Virtual Functions(2) Session 8 –Inheritance(2) 18/21
- ABSTRACT BASE CLASSES • A class that includes at least one pure virtual function is an abstract base class. • An attempt to create an instance of an abstract base class will generate a compiler error. Session 8 –Inheritance(2) 19/21
- Virtual Operators • Declaration syntax: virtual bool operator op_symbol (const ClassName&) const = 0; Session 8 –Inheritance(2) 20/21
- Summary • Derived Classes with Depth – Constructors and Destructor – Copy Constructor – Assignment Operator • Multiple Inheritance Q&A – Virtual Inheritance • Abstract Base Classes – Pure Virtual Functions – Abstract Base Classes – Virtual Operators Session 8 –Inheritance(2) 21/21