Click here to Skip to main content
13,597,584 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


17 bookmarked
Posted 20 May 2013
Licenced CPOL

Adapter Design Pattern in C++

, 29 Jun 2016
Rate this:
Please Sign up or sign in to vote.
Adapter Design Pattern in C++


In this tip, I will discuss what an adapter design pattern is with an example from our daily life.

In my early days of programming, I always used to find design patterns as a dangerous subject that always used to haunt me in team meetings and training. Because the example taken to describe these patterns were often language oriented and complex, I always thought if I could get an example as simple that a layman can understand it. So today, I will try and explain what an Adapter patterns is and how simple it is to understand that.

Using the Code

An adapter pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

It comprises three components:

  • Target: This is the interface with which the client interacts.
  • Adaptee: This is the interface the client wants to interact with, but can’t interact without the help of the Adapter.
  • Adapter: This is derived from Target and contains the object of Adaptee.

When I shifted from India to London, I took along many of my electrical appliances with me. But there was one problem using them, the pin shape. In India, all the appliances have round pins whereas in London they are flat pinned. Now how do we overcome this problem as I was not ready to buy new plugs. So the adapter came to my rescue (and saved lots of pounds!!!)

I got hold of an adapter plug which has round pins as input and the other end with flat pins (India to UK adapters). This is how I used them with the adapter pattern.

Class description:

  • <AbstractPlug>: Abstract Target class
  • <Plug>: Concrete Target class
  • <AbstractSwitchBoard>: Abstract Adaptee class
  • <SwitchBoard>: Concrete Adaptee class
  • <Adapter>: Adapter class, our saviour

// Abstract Target
class AbstractPlug {
  void virtual RoundPin(){}
  void virtual PinCount(){}
// Concrete Target
class Plug : public AbstractPlug {
  void RoundPin() {
    cout << " I am Round Pin" << endl;
  void PinCount() {
    cout << " I have two pins" << endl;
// Abstract Adaptee
class AbstractSwitchBoard {
  void virtual FlatPin() {}
  void virtual PinCount() {}
// Concrete Adaptee
class SwitchBoard : public AbstractSwitchBoard {
  void FlatPin() {
        cout << " Flat Pin" << endl;
  void PinCount() {
        cout << " I have three pins" << endl;
// Adapter
class Adapter : public AbstractPlug {
  AbstractSwitchBoard *T;
  Adapter(AbstractSwitchBoard *TT) {
        T = TT;
  void RoundPin() {
  void PinCount() {
// Client code
void _tmain(int argc, _TCHAR* argv[])
  SwitchBoard *mySwitchBoard = new SwitchBoard; // Adaptee
  // Target = Adapter(Adaptee)
  AbstractPlug *adapter = new Adapter(mySwitchBoard);

Points of Interest

Whenever I try to write a blog, I always think how easy and interesting I can make an article so that you never forget its concept and can make other people understand it with a simple example! I hope to do that here as well.


  • 20th May, 2013: First version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Priyanka Sabharwal81
Software Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

SuggestionI think we can improve the code to make it a pure adapter. :) Pin
Biswajit Sadhu5-Feb-15 2:00
memberBiswajit Sadhu5-Feb-15 2:00 
QuestionGood but should be better Pin
isoyavuz212-Mar-14 23:30
memberisoyavuz212-Mar-14 23:30 
Thanks your effort to explain the pattern with real life application. But you say abstract classes but implementing virtual methods in them so they are actually not abstract but base classes. Another subtle point is lack of deleting pointers after memory allocation in the main. Because you use polymorphic pointers without virtual base destructor, newcomers can get accustomed to erronous implementation by examining your examples.
QuestionThank you very much Pin
Shaje7-Oct-13 7:26
memberShaje7-Oct-13 7:26 
Questionnice post.....How to test??? Pin
milon12-Jun-13 22:30
membermilon12-Jun-13 22:30 
GeneralGood explaination Pin
Manish K. Agarwal3-Jun-13 0:37
memberManish K. Agarwal3-Jun-13 0:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180621.3 | Last Updated 29 Jun 2016
Article Copyright 2013 by Priyanka Sabharwal81
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid