编程语言

img bluejugar

C++ Design Pattern:Adapter

发表于2004/10/31 17:27:00  853人阅读

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);
}

阅读全文
0 0

相关文章推荐

img
取 消
img