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月15日

Chapter 7 Defining Your Own Date Types

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

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


7.1 The Struct in C++
∎ Struct

// Struct definition
struct Rectangle
{
    int left;
    int top;
    int right;
    int bottom;
};


7.2 Types, Classes, Objects, and Instances
∎ Class

// Definition of a class
class CBox
{
    public:
        double m_length; // Data members
        double m_width;
        double m_height;
};

∎ Class
// Main.cpp
CBox box1;  // Declare object box1 of type CBox
CBox box2;

if (box1 > box2)  // ">" is an operator acting on the objects

    box1.fill();  // "fill()" is a member function acting on object 1
else
    box2.fill();


7.3 Understanding Classes
∎ Accessing the data members of a class

// Main.cpp
CBox box1;
CBox box2;
box1.m_length = box1.m_width = box1.m_height = 2.0;
box2 = box1;  // Member-wise copying from box1 to box2

∎ Adding a member function to CBox

// Member function
double volume()
{
    return m_length * m_width * m_height;

}


7.4 Class Constructors
∎ Adding a Constructor to the CBox class  

// Constructor definition
CBox(double lv, double wv, double hv)
{
    cout << "Constructor called." << endl;
    m_length = lv;  // Set values of data members
    m_width = wv;
    m_height = hv;
}

∎ Default constructor

// Default constructor definition
CBox()
{
    cout << "Default constructor called." << endl;
}

∎ Constructor initialization list

// Constructor definition
CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0) :
             m_length {lv}, m_width{wv}, m_height{hv}
{
    cout << "Constructor called." << endl;
}

cf. CCandyBox(double lv, double wv, double hv, const char* str = "Candy") :
             : CBox {lv, wv, hv}  // In the inherited class CCandyBox

∎ Making a constructor explicit 

// Make a constructor more specific, instead of generalCBox box;
box = 99.0;
CBox(double side) : m_Length {side}, m_Width {side}, m_Height {side}  {}  // Will compile
explicit CBox(double side)
: m_Length {side}, m_Width {side}, m_Height {side} {}  // Will not, unless CBox box {4.0} is used 



7.5 Private Members of a Class
∎ Private data members

// CBox.h
class CBox
{
    private:
        double m_length;
        double m_width;
        double m_height;
};

// Main.cpp

box1.m_length = 4.0; // Error message: cannot access private data member declared in class CBox

∎ Friend function of a class

// Friend function prototype (in CBox.h) 
friend double boxSurface(const CBox& aBox);

// A function that is not a member of the class, but with special privilege

double boxSurface(const CBox& aBox)
{
    return 2.0 * (aBox.m_length * aBox.m_width + aBox.m_width * aBox.m_height
               + aBox.m_height * aBox.m_length); 
}

∎ Copying information between instances

// Initialize box2 with box1 (done by the default copy constructor)
CBox box2{box1};

// .h

CBox (const CBox& initB);

// .cpp

CBox::CBox(const CBox& initB):
            m_length{initB.m_length}, m_width{initB.m_width}, m_height{initB.m_height}


7.6 The Pointer This
∎ Explicit use of this

// main.cpp
if (cigar.compare(match))
    cout << "cigar is larger than match" << endl;
else
    cout << "cigar is not larger than match" << endl;

// .cpp

bool compare(CBox& aBox)
{
    return this->volume() > aBox.volume();
}



7.7 Const Objects
∎ Constant member functions of a class

// Non-const object
CBox cigar{8.0, 5.0, 1.0};
bool CBox::compare(CBox& aBox)

// const object

const CBox cigar{8.0, 5.0, 1.0};
bool CBox::compare(const CBox& aBox) const

∎ Member function definitions outside the class


7.8 Array of Objects


7.9 Static Members of a Class


∎ Counting instances of a class type (ex7_12)
static int objectCount;  // Count of objects in existence

int CBox:: objectCount{};  // Initialize static data member of CBox class; no keyword "static"


7.10 Pointers and References to Objects


7.11 Summary

沒有留言:

張貼留言