From this day onwards,I will start to learn C++ Design pattern.& I will write down my comprehensions and feelings.(for the purpose of self-examination,and communicating with newbies as me.) (Adapterd from lxgljj's article in JAVA.) #include <iostream> #include <string> using namespace std;
class PostedBook { public: PostedBook( const string& rhs) { this->m_StringNO_ = rhs; } PostedBook( const PostedBook& rhs ) { this->m_StringNO_ = rhs.m_StringNO_; } const PostedBook& operator=( const PostedBook& rhs ) { this->m_StringNO_ = rhs.m_StringNO_; return *this; } string GetPhoneNoFromBook() { return ( m_StringNO_ ); } private: string m_StringNO_; };
class TellFriend { public: void TellSth() { cout<<"Azure's phone number: "; } };
class TellFriendPhoneNo:public TellFriend { public: TellFriendPhoneNo( const PostedBook& pb ):m_PostedBook_(pb) { //Nothing needed to be done. } void TellPhoneNumber() { cout<<m_PostedBook_.GetPhoneNoFromBook()<<endl; } private: PostedBook m_PostedBook_; };
int main() { PostedBook pb("13973284801"); TellFriendPhoneNo gp(pb); gp.TellSth(); gp.TellPhoneNumber();
return (1); } 
|