设为首页 加入收藏

TOP

1.11 从UML到C++:以电冰箱为例
2013-10-07 00:06:36 来源: 作者: 【 】 浏览:80
Tags:1.11 UML 电冰箱

1.11  从UML到C++(www.cppentry.com):以电冰箱为例

目前,嵌入式程序员大多使用C语言来编写系统,其次是C++(www.cppentry.com)。所以在本书中,所有的范例程序除了以C编写之外,也都以C++(www.cppentry.com)编写,让您能使用C和C++(www.cppentry.com)双语同步开发组件化嵌入式软件系统。
在上一节中,以C编写了Light和Refrigerator两个类,现在就以C++(www.cppentry.com)编写,如下所示。

---- 小灯Light的C++(www.cppentry.com)定义文件 ----
/*  light.h   */
#ifndef LIGHT_H
#define LIGHT_H
class Light
{ private:
int state_var;
public:
Light();
void turnOn();
void turnOff();
void performAction();
};
#endif
---- 小灯Light的C++(www.cppentry.com)实现文件 ----
/*  light.cpp  */
#include <iostream>
using namespace std;
#include "light.h"
extern void signal_to_light_UI(int);
Light::Light() { state_var = 0; }
void Light::turnOn()
{  if (state_var != 0)return;   
state_var = 1;
this->performAction(); }
void Light::turnOff()
{  if (state_var != 1)return;   
state_var = 0;
this->performAction();  }
void Light::performAction()
{  if (state_var == 0)
signal_to_light_UI(0);
else if (state_var == 1) 
signal_to_light_UI(1);  }
//end
---- 冰箱Refrigerator的C++(www.cppentry.com)定义文件 ----
/*  refrigerator.h   */
#ifndef REFRIGERATOR_H
#define REFRIGERATOR_H
#include "light.h"
class Refrigerator
{ private:
int state_var;
Light* light_obj;
public:
Refrigerator();
void openTheDoor();
void closeTheDoor();
void performAction();
};
#endif
---- 冰箱Refrigerator的C++(www.cppentry.com)实现文件 ----
/*  refrigerator.cpp   */
#include <iostream>
using namespace std;
#include "refrigerator.h"
#include "mb.h"
extern void signal_to_button1_UI(int);
extern void signal_to_button2_UI(int);
void Refrigerator::openTheDoor()
{  if (state_var != 0)return;   
state_var = 1;
this->performAction(); }
void Refrigerator::closeTheDoor()
{  if (state_var != 1)return;   
state_var = 0;
this->performAction(); }
void Refrigerator::performAction()
{  Light* light_obj;
light_obj = MB::get_light_obj();
if (state_var == 0)
{    signal_to_button1_UI(0);
signal_to_button2_UI(1);
light_obj->turnOff(); }
else if (state_var == 1) 
{     signal_to_button1_UI(1);
signal_to_button2_UI(0);
light_obj->turnOn(); }}
Refrigerator::Refrigerator()
{ state_var = 0;
signal_to_button1_UI(0);
signal_to_button2_UI(1);  }
//end
---- MB的C++(www.cppentry.com)定义文件 ----
/*  mb.h   */
#ifndef MB_H
#define MB_H
#include "refrigerator.h"
#include "light.h"
class MB
{ private:
static Refrigerator* refri_obj;    
static Light* light_obj;
public:
MB();
static void initial();
static Refrigerator* get_refrigerator_obj();
static Light* get_light_obj();
};
#endif
---- MB的C++(www.cppentry.com)实现文件 ----
/*  mb.cpp  */
#include <iostream>
using namespace std;
#include "mb.h"
#include "refrigerator.h"
#include "light.h"
MB::MB(){}
void MB::initial()
{    refri_obj = new Refrigerator(); 
light_obj = new Light();  }

Refrigerator* MB::get_refrigerator_obj()
{     return refri_obj; }
Light* MB::get_light_obj()
{    return light_obj; }

Light* MB::light_obj = NULL;
Refrigerator* MB::refri_obj = NULL;
//end
---- Win32的GUI定义/实现文件 ----
/*  main.cpp  */
#include <windows.h>
#include "resource.h"
#include "refrigerator.h"
#include "mb.h"
const char ClassName[] = "MainWindowClass";
HWND hWndButton1, hWndButton2, hWndButton3;
PAINTSTRUCT ps;
HDC hdc;
int st;
Refrigerator* refri;
void signal_to_button1_UI(int ab)
{ EnableWindow(hWndButton1, ab); }
void signal_to_button2_UI(int ab)
{  EnableWindow(hWndButton2, ab); }
void signal_to_light_UI(int st)
{  if (st == 1) SetWindowText(hWndButton3, "Light(ON)");
else        SetWindowText(hWndButton3, "Light(OFF)");  }
LRESULT CALLBACK WndProc( HWND    hWnd,  UINT    Msg,
WPARAM  wParam, LPARAM  lParam )
{   switch (Msg){
case WM_CREATE: {
HINSTANCE hInstance
= (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
hWndButton1= CreateWindowEx( 0, "BUTTON", "Door(close)",
WS_VISIBLE | WS_CHILD, 10, 30, 120, 35,
hWnd, (HMENU)IDB_BUTTON1, hInstance, NULL);

hWndButton2= CreateWindowEx( 0, "BUTTON", "Door(open)",
WS_VISIBLE | WS_CHILD, 10, 120, 120, 35,
hWnd, (HMENU)IDB_BUTTON2, hInstance, NULL);

hWndButton3= CreateWindowEx( 0, "BUTTON", "Light(OFF)",
WS_VISIBLE | WS_CHILD, 200, 60, 120, 35,
hWnd, (HMENU)IDB_BUTTON3, hInstance, NULL);
}
//--------------------------------------
MB::initial();
refri = MB::get_refrigerator_obj();
//--------------------------------------    
break;   
case WM_COMMAND: {
switch(LOWORD(wParam)){
case IDM_FILE_EXIT:
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
case IDB_BUTTON1: {
switch (HIWORD(wParam)){
case BN_CLICKED:
refri->closeTheDoor();
break;
} }
break;
case IDB_BUTTON2: {
switch (HIWORD(wParam)){
case BN_CLICKED:
refri->openTheDoor();
break;
} }
break;
case IDM_ITEM:
MessageBox(NULL, "Not Implemented", "",
MB_OK | MB_ICONINFORMATION);
break;
}
return 0;
}
break;
case WM_CLOSE: DestroyWindow(hWnd);
break;
case WM_DESTROY: PostQuitMessage(0);
break;
default:
return (DefWindowProc(hWnd, Msg, wParam, lParam));
}
return 0;
}
INT WINAPI WinMain( HINSTANCE  hInstance, HINSTANCE  hPrevInstance,
LPSTR      lpCmdLine, INT        nCmdShow )
{
WNDCLASSEX    wc;
wc.cbSize            = sizeof(WNDCLASSEX);
wc.style            = 0;
wc.lpfnWndProc       = (WNDPROC)WndProc;
wc.cbClsExtra        = 0;
wc.cbWndExtra        = 0;
wc.hInstance         = hInstance;
wc.hIcon             = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
wc.hIconSm           = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
wc.hCursor           = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground     = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName      = MAKEINTRESOURCE(IDR_MENU);
wc.lpszClassName     = ClassName;
if (!RegisterClassEx(&wc))return 0;
HWND    hWnd;
hWnd = CreateWindowEx( WS_EX_CLIENTEDGE,ClassName, "Class Menu",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
400, 300, NULL, NULL, hInstance, NULL);
if (!hWnd)return 0;
ShowWindow(hWnd, SW_SHOW);

UpdateWindow(hWnd);
MSG    Msg;
while (GetMessage(&Msg, NULL, 0, 0)){
TranslateMessage(&Msg);
DispatchMessage(&Msg);   }
return Msg.wParam;
}

此C++(www.cppentry.com)程序的执行情境与上一节中的C程序是一样的。此程序除了main.cpp与Windows平台有关之外,冰箱Refrigerator和小灯Light两个类代码都是跨平台的。

【责任编辑:雪花 TEL:(010)68476606】

回书目   上一节   下一节

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇1.3.2 为什么用C 下一篇Visual C++ 2008特性包的Beta版可..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: