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 will be celebrating 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月15日

Chapter 8 More on Classes

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

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


8.1 Class Destructors

∎ Simple destructor
// Destructor definition
CBox::~CBox()
{
    cout << "Destructor called." << endl;
}

∎ Dynamic memory allocation

// Constructor definition
CMessage::CMessage(const char* text = "Default message")
{
    cout << "Constructor called." << endl;
    size_t length {strlen(text) + 1};
    m_pMessage = new char [length];
    strcpy_s (m_pMessage, length, text); 


// Destructor to free memory allocated by new

CMessage::~CMessage()
{
    cout << "Destructor called." << endl;
    delete [] m_pMessage;
}


8.2 Implementing a Copy Constructor

∎ Copy constructor (different from a default copy constructor)  // Constructor definition
CMessage::CMessage(const CMessage& aMess)
{
    size_t length {strlen(aMess.m_pMessage) + 1};
    m_pMessage = new char [length];
    strcpy_s(m_pMessage, length, aMess.m_pMessage);
}


8.3 Operator Overloading
∎ Operator functions
// Main.cpp
if (mediumBox < bigBox)
    cout << "mediumBox < bigBox." << endl;
else
    cout << "mediumBox NOT < bigBox." << endl;

if (thatBox == mdeiumBox)

    cout << "thatBox = mediumBox." << endl;
else
    cout << "thatBox NOT = mediumBox." << endl;

if (mediumBox > 50.0)

    cout << "mediumBox capacity > 50." << endl;
else
    cout << "mediumBox capacity NOT > 50." << endl;

// Operator functions in CBox.cpp

bool CBox::operator<(const CBox& aBox) const
{
    return this->volume () < aBox.volume ();
}

bool CBox::operator==(const CBox& aBox) const

{
    return this->volume () == aBox.volume();
}

bool CBox::operator>(const double value) const

{
    return this->volume () > value;
}

∎ Overloaded assignment operator

// Overloaded assignment operator for CMessage objects
CMessage& CMessage::opeartor=(const CMessage& aMess)
{
    if (this != &aMess)
    {
        delete [] m_pMessage;
        size_t length {strlen(aMess.m_pMessage) + 1};
        m_pMessage = new char [length];
        strcpy_s(this->m_pMessage, length, aMess.m_pMessage);
    }
    return *this;
}

∎ Addition operator

// Function to add two CBox objects
CBox::CBox operator+(const CBox& aBox) const
{
    return CBox(std::max(m_length, aBox.m_length),
                          std::max(m_width, aBox.m_width),
                          m_height + aBox.m_height);
}


8.4 The Object Copying Problem

∎ Addition operator
// Main.cpp
motto3 = motto1 + motto2;

// Tracing object copy operations in CMessage.cpp

// Addition operator function (step 1)
CMessage::CMessage operator+(const CMessage& aMess) const
{
    cout << "Add operator function called." << endl;
    size_t length {strlen(m_pMessage) + strlen(aMess.m_pMessage) + 1};
    CMessage message;
    message.m_pMessage = new char [length];
    strcpy_s(message.m_pMessage, length, m_pMessage);
    strcat_s(message.m_pMessage, length, aMess.m_pMessage);
    return message;
}

// Copy constructor (step 2)

CMessage::CMessage(const CMessage& aMess)
{
    cout "Copy constructor called." << endl;
    size_t length {strlen(aMess.m_pMessage) + 1};
    m_pMessage = new char [length];
    strcpy_s(m_pMessage, length, aMess.m_pMessage);
}

// Assignment operator function (step 3)

CMessage& CMessage::operator=(const CMessage& aMess)
{
    cout << "Assignment operator function called." << endl;
    if  this != aMess)
    {
        delete [] m_pMessage;
        size_t length {strlen(aMess.m_pMessage) + 1};
        m_pMessage = new char [length];
        strcpy_s(this->m_pMessage, length, aMess.m_pMessage);
    } 
    return *this;
}

∎ Move constructor (cf. copy constructor)

CMessage:: CMessage(Cmessage&&aMess)
{
    cout << "Move constructor called." << endl;
    m_pMessage = aMess.m_pMessage;
    aMess.m_pMessage = nullptr; // Not const, so permitted
}

∎ Move assignment operator function (cf. assignment operator function)

CMessage& CMessage::operator=(CMessage&& aMess)
{
    cout << "Move assignment operator function called." << endl;
    delete [] m_pMessage;
    m_pMessage = aMess.m_pMessage;
    aMess.m_pMessage = nullptr;
    return *this;
}


8.5 Default Class Members


8.6 Class Templates


8.7 Perfect Forwarding


8.8 Default Arguments for Template Parameters


8.9 Aliases for Class Templates


8.10 Template Specialization


8.11 Using Classes


8.12 Organizing Your Program Code


8.13 Library Classes for Strings


8.14 Summary

沒有留言:

張貼留言