Industrial Training




Object Oriented Programming in C

Embedded software development is slowly moving towards object oriented analysis, design and programming. The introduction of object oriented technologies in some systems has not happened due to lack of C++ support on some platforms. This article focuses on platforms where C++ compilers are not available. The developers working on these platforms should still be able to use object oriented analysis and design. When it comes to final code development they can use C. The following sections cover an example of C++ code and its implementation in C.

  • C++ Source Code: C++ source files implementing TerminalManager and Terminal classes.
  • C Source Code: TerminalManager and Terminal implemented in C for a platform that does not support C++. 


C++ Source Code

Terminal Manager and Terminal C++ header and source files are shown below:

Terminal Manager

TerminalManager.hpp



#include 
class TerminalManager
{
  Terminal terminals[MAX_TERMINALS];
public:
  TerminalManager();
  ~TerminalManager();
  Terminal *FindTerminal(int terminalId) const;
  void HandleMessage(Msg* pMsg);
};


TerminalManager.cpp

#include 
      
TerminalManager::TerminalManager()
 
{
   . . .
}
 
TerminalManager::~TerminalManager()
{
}
 


TerminalManager::HandleMessage(Msg* pMsg);
{
    int terminalId = pMsg->GetTerminalId();
    switch (pMsg->getType())
    {
    case CREATE_TERMINAL: 
      terminals[terminalId].Activate((TerminalCreateMsg *)pMsg);
      break;
    case DELETE_TERMINAL:
      terminals[terminalId].Deactivate((DeleteTerminalMsg *) pMsg);
      break;
    case RUN_DIAGNOSTICS:
      status = terminals[terminalId].HandleRunDiagnostics((RunDiagnosticsMsg *) pMsg);
      break;
    case PERFORM_SWITCHOVER:
      status1 = terminals[terminalId].HandleOutOfService();
      status2 = terminals[pMsg->GetOtherTerminalId()].HandleInService();
      break;
    }
}


Terminal *TerminalManager::FindTerminal(int terminalId)
{
   if (terminalId < MAX_TERMINALS)
   {
       return (&terminals[terminalId));
   }
   else
   {
       return NULL;
   }
}


Terminal

Terminal.hpp

class Terminal
{
  TerminalId terminalId;
  in terminalType;
  int terminalStatus;
  void SendMessage(Message *pMsg);
  
public:
  Terminal();
  ~Terminal();
  Status HandleRunDiagnostics(RunDiagnosticsMsg *pMsg);
  Status HandleOutOfService();
  Status HandleInService();
  void Activate(TerminalCreateMsg *pMsg);
  void Deactivate(TerminalDeleteMsg *pMsg);
};


Hi I am Pluto.