Welcome Message

「流行起於高分子,變化盡藏微宇宙」! 歡迎光臨「流變學好簡單 | The RheoMaster」部落格,成立於 2019.2.22,已於 2024 年初屆滿 5 年!旨在提供簡單的中文流變學知識,包括高分子流變學、輸送現象、高分子加工、流變量測等。您可至右方進行關鍵字搜尋,若有任何建議,請至文章留言或來信 yuhowen@gmail.com。 Welcome to "The RheoMaster" Blog. This website was established in Feb 2019, and has celebrated its 5th anniversary in eary 2024. In view of the lack of Chinese literature on rheology, here we offer basic knowledge relevant to polymer rheology, transport phenomena, polymer processing, rheometry, etc. If you have any suggestion, please leave a message on the post you are reading or email us at yuhowen@gmail.com.

精選文章

網誌作者近期國際期刊論文發表 (Recent SCI Journal Articles by the Blogger)

  Extensional Rheology of Linear and Branched Polymer Melts in Fast Converging Flows 線型、分支型高分子融體於高速收縮流之拉伸流變 Rheol. Acta 62 , 183–204 (2023)...

2019年12月23日

Chapter 9 Class Inheritance and Virtual Functions

第 9 章基本 C++ 程式碼整理如下


Reference: I Horton, Ivor Horton's Beginning Visual C++ 2013 (Wiley 2014).

9.1 Object-Oriented Programming Basics

9.2 Inheritance in Classes

∎ Deriving classes form a base class
Ex9_01.cpp (main function; #include "CandyBox.h")
CandyBox.h (derived class; #include "Box.h"; class CCandyBox : CBox)
Box.h (base class
; class CBox)



9.3 Access Control under Inheritance

∎ Accessing private members of the base classclass
CCandyBox : public CBox vs. CBox (or private CBox)

∎ The function members defined in the derived class cannot access the private date members of a base class. To access the private members of the base class, one has to move the function "double volume() const" from the derived class to the base class.


∎ Constructor operation in a derived class
CCandyBox(double lv, double wv, double hv, const char* str = "Candy") : CBox {lv, wv, hv}  // 1
explicit CCandyBox(const char* str = "Candy")  // 2

∎ The access level of inherited class member



9.4 The Copy Constructor in a Derived Class

∎ The copy constructor in a derived class (Ex9_05)
CCandyBox(const CCandyBox& initCB) vs. CCandyBox(const CCandyBox& initCB) : CBox{initCB}  // All copy constructors in both header files have to be called


9.5 Preventing Class Derivation

∎ Modifier "final"


9.6 Class Members as Friends



9.7 Virtual Functions

∎ Using an inherited function: early binding (Ex9_06)
myBox.showVolume();           // Call the volume() function in Box.h
myGlassBox.showVolume();  // Still call the volume() function in Box.h, not the one in GlassBox.h 

∎ Fixing the CGlassBox: late binding (Ex9_07)
virtual double volume() const  // Box.h
virtual double volume() const  // GlassBox.h; don't forget to specify volume() as const, otherwise you won't override the base function

∎ Ensuring correct virtual function operation (Modifier "override")

virtual double volume() const override  // The keyword "const" still cannot be omitted, however

∎ Preventing function overriding (Modifier "final)

virtual double volume() const final        // If you don't want this volume() function to be overridden

∎ Points to base and derived classes (Ex9_08)

CBox myBox{ 2.0, 3.0, 4.0 };                    // Define a base box
CGlassBox myGlassBox{ 2.0, 3.0, 4.0 };  // Define derived box of same size
CBox* pBox{};              // A pointer to base class objects
pBox = &myBox;           // Set pointer to address of base object
pBox->showVolume();   // Display volume of base box
pBox = &myGlassBox;  // Set pointer to derived class object

pBox->showVolume();   // Display volume of derived box




∎ Using references with virtual functions (Ex9_09)

output(myBox);           // Output volume of base class object
output(myGlassBox);  // Output volume of derived class object

void output(const CBox& aBox)  // Accept an object of any class derived from CBox as an argument

{
    aBox.showVolume();
}

∎ An abstract class (Ex9_10)

// Container.h
virtual double volume() const = 0;  // No content; pure virtual function in the abstract base class


// Main.cpp

// Pointers to abstract base class, initialized with address of CBox object or CCan object
CContainer* pC1{ new CBox{ 2.0, 3.0, 4.0 } };
CContainer* pC2{ new CCan{ 6.5, 3.0 } };

pC1->showVolume();  // Output the volumes of the two objects ...

pC2->showVolume();  // ... pointed to

delete pC1;  // Clean up ...

delete pC2;  // ... the free space

∎ More than one level of inheritance (Ex9_11)

// Class CGlassBox is derived from its direct base class CBox and indirect base class CContainer
// Inheritance & Constructor (GlassBox.h)

class CGlassBox : public CBox          
CGlassBox(double lv, double wv, double hv) : CBox{ lv, wv, hv } {}
  


∎ Calling the wrong destructor (Ex9_12) 



9.8 Casting between Class Types

∎ 


9.9 Nested Classes

∎ 


9.10 Summary

∎ 

沒有留言:

張貼留言