Thursday, October 30, 2008
UsingWin32Api2---Understanding The Message Loop
What is a Message?
什么是消息?
A message is an integer value.
消息是一个整型数据.
They are used to communicate pretty much everything in windows at least on basic levels.
在窗口里,最起码在底层,他们被用在交流差不多所有的事件.
Each message have two parameters, wParam and lParam ,but they don't have to be used. In Win32 wParam and lParam are both 32 bit.
每个消息都有两个参数,wParam和lParam.但不一定都要用.在Win32里,wParam和lParam都是32位.
To send a message you can use PostMessage() or SendMessage().
你可以用PostMessage()或SendMessage()来发送消息.
PostMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
it puts the message into the Message Queue and returns immediatly. That means when it is done the message may or may not have been processed yet.
它把消息放到消息队列并马上返回.这是指,当它完成时,消息可能还没被执行.
SendMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam) it sends the message directly to the window and does not return untill the window has finished processing it.
它把消息直接发送到窗口并等到窗口完成了消息后再返回.
What is the Message Queue?
什么是消息队列?
Lets say you were busy handling the WM_PAINT message and suddenly the user types a bunch of stuff on the keyboard. Whatshould happen? Should you be interrupted in your drawing to handle the keys or should the keys just be discarded? Wrong! Obviously neither of these options is reasonable, so we have the message queue, when messages are posted they are added to the message queue and when you handle them they are removed. This ensure that you aren't going to miss messages.
假设你是窗口,正忙于操作WM_PAINT消息.突然用户输入一大串字符.什么事会发生呢?你会打断你的画图去处理键盘的输入,还是无视掉键盘的输入呢?错了!明显两种选择都是不合理的.因此我们有消息队列.当收到消息时,消息被加到消息队列里.当你处理了它们后,它们就移除.这样就确保你在处理一条消息时,不会丢失其他消息.
What is a Message Loop?
什么是消息循环?
while(GetMessage(&Msg,NULL,0,0)>0){
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
}
1.The message loop calls GetMessage(), which looks in your message queue. If the message queue is empty your program basically stops and waits for one(it Blocks).
1.消息循环调用查看消息队列的GetMessage().如果消息队列是空的,程序基本上停止并等待(它就堵塞).
2.When an event occures causing a message to be added to the queue (for example the system registers a mouse click). GetMessages() returns a positive value indicating there is a message to be processed, and that it has filled in the members of the MSG structure we passed it. It returns 0 if it hits WM_QUIT, and -1 if an error occured.
2.当事件发生造成了消息被添加到队列(例如登记系统点击了一下鼠标).GetMessages()返回一个大于0的价值,说明有一个消息需要被处理,并MSG结构中空缺的成员.如果它遇到WM_QUIT,它就返回0.如果发生了错误就是-1。
3.We take the message (in the Msg variable) and pass it to TranslateMessage(), this does a bit of additional processing, translating virtual key messages into character messages. This step is actually optional, but certain things won't work if it's not there.
3.我们提取消息(在MSG的变量里),并将其交给TranslateMessage(),这需要一点额外的处理,把虚拟的键消息翻译成实质的字符消息.这一步实际上是可选的,但是如果它不存在,某些事情将不会工作.
4.Once that's done we pass the message to DispatchMessage(). What DispatchMessage() does is take the message, checks which window it is for and then looks up the Window Procedure for the window. It then calls that procedure, sending as parameters the handle of the window, the message, and wParam and lParam.
4.上一步一完成,我们就把消息传给DispatchMessage().DispatchMessage()拿到消息,检查它属于哪一个窗口,然后查找该窗口的窗口过程.然后它以窗口句柄,消息,wParam,lParam作为参数调用的窗口过程.
5.In your window procedure you check the message and it's parameters, and do whatever you want with them! If you aren't handling the specific message, you almost always call DefWindowProc() which will perform the default actions for you (which often means it does nothing).
5.在你的窗口过程中,你检查消息,和它的参数,并你想让它做的事!如果你不处理具体的消息,你几乎总是调用DefWindowProc.为您执行默认的事件(这往往意味着什么都不做).
6.Once you have finished processing the message, your windows procedure returns, DispatchMessage() returns, and we go back to the beginning of the loop.
6.一旦你完成消息处理,你的窗口过程就会返回,DispatchMessage()也返回,我们也回到开始的循环.
-----------------------------------------------------------------------------------------------
Actually, I copy them from book, because I don't think I have such high level to write down this beautiful explanation. But I do add some personal viewpoint. Also, i translate it by myself.
whatever, i strongly suggest you to read this if you want to due with win32, or even MFC.
什么是消息?
A message is an integer value.
消息是一个整型数据.
They are used to communicate pretty much everything in windows at least on basic levels.
在窗口里,最起码在底层,他们被用在交流差不多所有的事件.
Each message have two parameters, wParam and lParam ,but they don't have to be used. In Win32 wParam and lParam are both 32 bit.
每个消息都有两个参数,wParam和lParam.但不一定都要用.在Win32里,wParam和lParam都是32位.
To send a message you can use PostMessage() or SendMessage().
你可以用PostMessage()或SendMessage()来发送消息.
PostMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
it puts the message into the Message Queue and returns immediatly. That means when it is done the message may or may not have been processed yet.
它把消息放到消息队列并马上返回.这是指,当它完成时,消息可能还没被执行.
SendMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam) it sends the message directly to the window and does not return untill the window has finished processing it.
它把消息直接发送到窗口并等到窗口完成了消息后再返回.
What is the Message Queue?
什么是消息队列?
Lets say you were busy handling the WM_PAINT message and suddenly the user types a bunch of stuff on the keyboard. Whatshould happen? Should you be interrupted in your drawing to handle the keys or should the keys just be discarded? Wrong! Obviously neither of these options is reasonable, so we have the message queue, when messages are posted they are added to the message queue and when you handle them they are removed. This ensure that you aren't going to miss messages.
假设你是窗口,正忙于操作WM_PAINT消息.突然用户输入一大串字符.什么事会发生呢?你会打断你的画图去处理键盘的输入,还是无视掉键盘的输入呢?错了!明显两种选择都是不合理的.因此我们有消息队列.当收到消息时,消息被加到消息队列里.当你处理了它们后,它们就移除.这样就确保你在处理一条消息时,不会丢失其他消息.
What is a Message Loop?
什么是消息循环?
while(GetMessage(&Msg,NULL,0,0)>0){
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
}
1.The message loop calls GetMessage(), which looks in your message queue. If the message queue is empty your program basically stops and waits for one(it Blocks).
1.消息循环调用查看消息队列的GetMessage().如果消息队列是空的,程序基本上停止并等待(它就堵塞).
2.When an event occures causing a message to be added to the queue (for example the system registers a mouse click). GetMessages() returns a positive value indicating there is a message to be processed, and that it has filled in the members of the MSG structure we passed it. It returns 0 if it hits WM_QUIT, and -1 if an error occured.
2.当事件发生造成了消息被添加到队列(例如登记系统点击了一下鼠标).GetMessages()返回一个大于0的价值,说明有一个消息需要被处理,并MSG结构中空缺的成员.如果它遇到WM_QUIT,它就返回0.如果发生了错误就是-1。
3.We take the message (in the Msg variable) and pass it to TranslateMessage(), this does a bit of additional processing, translating virtual key messages into character messages. This step is actually optional, but certain things won't work if it's not there.
3.我们提取消息(在MSG的变量里),并将其交给TranslateMessage(),这需要一点额外的处理,把虚拟的键消息翻译成实质的字符消息.这一步实际上是可选的,但是如果它不存在,某些事情将不会工作.
4.Once that's done we pass the message to DispatchMessage(). What DispatchMessage() does is take the message, checks which window it is for and then looks up the Window Procedure for the window. It then calls that procedure, sending as parameters the handle of the window, the message, and wParam and lParam.
4.上一步一完成,我们就把消息传给DispatchMessage().DispatchMessage()拿到消息,检查它属于哪一个窗口,然后查找该窗口的窗口过程.然后它以窗口句柄,消息,wParam,lParam作为参数调用的窗口过程.
5.In your window procedure you check the message and it's parameters, and do whatever you want with them! If you aren't handling the specific message, you almost always call DefWindowProc() which will perform the default actions for you (which often means it does nothing).
5.在你的窗口过程中,你检查消息,和它的参数,并你想让它做的事!如果你不处理具体的消息,你几乎总是调用DefWindowProc.为您执行默认的事件(这往往意味着什么都不做).
6.Once you have finished processing the message, your windows procedure returns, DispatchMessage() returns, and we go back to the beginning of the loop.
6.一旦你完成消息处理,你的窗口过程就会返回,DispatchMessage()也返回,我们也回到开始的循环.
-----------------------------------------------------------------------------------------------
Actually, I copy them from book, because I don't think I have such high level to write down this beautiful explanation. But I do add some personal viewpoint. Also, i translate it by myself.
whatever, i strongly suggest you to read this if you want to due with win32, or even MFC.
Sunday, October 26, 2008
UsingWin32Api----most simple window
#include < windows.h>
LRESULT CALLBACK myWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
  WNDCLASS wc;
  HWND hwnd;
  MSG msg;
  wc.cbClsExtra=0;
  wc.cbWndExtra=0;
  wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
  wc.hCursor=LoadCursor(NULL,IDC_ARROW);
  wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  wc.hInstance=hInstance;
  wc.lpfnWndProc=myWindowProc;
  wc.lpszClassName="myClass";
  wc.lpszMenuName=NULL;
  wc.style=CS_HREDRAW|CS_VREDRAW;
  RegisterClass(&wc);
  hwnd=CreateWindow("myClass", "myWindow", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 600, 400, NULL, NULL, hInstance, 0);
  ShowWindow(hwnd,nShowCmd);
  UpdateWindow(hwnd);
  while(GetMessage(&msg, NULL, 0, 0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return msg.wParam;
}
LRESULT CALLBACK myWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch(msg)
  {
  case WM_CREATE:
    MessageBox(hwnd, "this is WM_CREATE message", "Message", 0);
    break;
  case WM_LBUTTONDOWN:
    MessageBox(hwnd, "this is WM_LBUTTONDOWN message", "Message", 0);
    break;
  case WM_DESTROY:
    MessageBox(hwnd, "this is WM_DESTROY message", "Message", 0);
    PostQuitMessage(0);
    break;
  default:
    return DefWindowProc(hwnd, msg, wParam, lParam);
  }
  return 0;
}
--------------------------------------------------------------------------------
This is the most simple window.
function "myWindowProc" which called callback funtion, is called by OS, so we don't have to invoke it. but we have to design it for processes messages which is sent to a window.
"WNDCLASS" is a struct contains the window class attributes. after you set up it, don't forget using "RegisterClass" to register it.
for this program, when "CreateWindow" is called, OS will send a WM_CREATE message to the window procedure. so if we have defined some activities for it, then the activities will be executed when callback funtion get it, just like what i do for WM_CREATE, WM_LBUTTONDOWN and WM_DESTROY.
after you create your window which is mean you call "CreateWindow" function, your window will not show itself immediately. you still have to call "ShowWindow" to show it.
after all of them, you program will go into a message loop. at this time, your program will be waiting for message until it get WM_QUIT. all message will be executed in callback function. and that is the system that the program has. taking care, WM_DESTROY is just destory the window that you see, and the program is still running on your computer.
at last, don't forget to return a "DefWindowProc", so that the message you don't care about will be done in a default way.
LRESULT CALLBACK myWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
  WNDCLASS wc;
  HWND hwnd;
  MSG msg;
  wc.cbClsExtra=0;
  wc.cbWndExtra=0;
  wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
  wc.hCursor=LoadCursor(NULL,IDC_ARROW);
  wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  wc.hInstance=hInstance;
  wc.lpfnWndProc=myWindowProc;
  wc.lpszClassName="myClass";
  wc.lpszMenuName=NULL;
  wc.style=CS_HREDRAW|CS_VREDRAW;
  RegisterClass(&wc);
  hwnd=CreateWindow("myClass", "myWindow", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 600, 400, NULL, NULL, hInstance, 0);
  ShowWindow(hwnd,nShowCmd);
  UpdateWindow(hwnd);
  while(GetMessage(&msg, NULL, 0, 0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return msg.wParam;
}
LRESULT CALLBACK myWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch(msg)
  {
  case WM_CREATE:
    MessageBox(hwnd, "this is WM_CREATE message", "Message", 0);
    break;
  case WM_LBUTTONDOWN:
    MessageBox(hwnd, "this is WM_LBUTTONDOWN message", "Message", 0);
    break;
  case WM_DESTROY:
    MessageBox(hwnd, "this is WM_DESTROY message", "Message", 0);
    PostQuitMessage(0);
    break;
  default:
    return DefWindowProc(hwnd, msg, wParam, lParam);
  }
  return 0;
}
--------------------------------------------------------------------------------
This is the most simple window.
function "myWindowProc" which called callback funtion, is called by OS, so we don't have to invoke it. but we have to design it for processes messages which is sent to a window.
"WNDCLASS" is a struct contains the window class attributes. after you set up it, don't forget using "RegisterClass" to register it.
for this program, when "CreateWindow" is called, OS will send a WM_CREATE message to the window procedure. so if we have defined some activities for it, then the activities will be executed when callback funtion get it, just like what i do for WM_CREATE, WM_LBUTTONDOWN and WM_DESTROY.
after you create your window which is mean you call "CreateWindow" function, your window will not show itself immediately. you still have to call "ShowWindow" to show it.
after all of them, you program will go into a message loop. at this time, your program will be waiting for message until it get WM_QUIT. all message will be executed in callback function. and that is the system that the program has. taking care, WM_DESTROY is just destory the window that you see, and the program is still running on your computer.
at last, don't forget to return a "DefWindowProc", so that the message you don't care about will be done in a default way.
Tuesday, October 21, 2008
UsingPretreatment
Marco:
#define NAME rest of line
where NAME is encountered as a token, it is replace by rest of line.
ex:
named = NAME//named = rest of line
#define MAC(x,y) argument1:x argument2:y
when MAC is used,two argument strings must be presented. They will replace x and y when MAC() is expanded.
ex:
expanded = MAC(foo bar, yuk yuk)//expanded = argument1:foo bar argument2:yuk yuk
ATTENTION: marco is NOT function. marco names can NOT be overloaded, and the macro preprocessor can NOT handle recursive calls.do NOT using them unless you have to. they just simply rearrange the program text before the compiler sees it. following the convention to name macros using lots of CAP letters.
#undef X
ensures that no macro called X is defined.
Conditional Compilation:
#ifdef EXPRESSION1
statement1
#endif
for the compiler to see statement1 unless a macro called EXPRESSION1 has been #defind.
#ifndef EXPRESSION2
statement2
#endif
for the compiler to see statement2 unless a macro called EXPRESSION2 hasnot been #defind.
#if EXPRESSION3
statement3
#elif EXPRESSION4
statement4
#else
statement5
#endif
for the compiler, if EXPRESSION3 is true(not 0), compile statement3. or if EXPRESSION4 is true, compile statement4. if all of them is not true, then comiple statement5.
Including File Compilation:
#include < iostream>//standard library header file
#include < iostream.h>//old-style standard library header file
#include "IO.h" //user identitiy header file
#include "../file.h" //header file under unix parent directory
#include "/usr/local/file.h" //complete path name under unix
#include "..\file.h" //header file under dos parent directory
#include "\usr\local\file.h" //complete path name under dos
difference between #include<...> and #include"..."
#include <...> //compiler starts to search form standard library path name
#include "..." //compiler starts to search form user working path name
--------------------------------------------------------------------------------
not just those pretreatments are in cpp. but they are the most famous we see. they are too powerful to easy to lose control. i suggest thinking again before you use them. but, unfortunately, we have to use them a lot. so it is importance that design before you writing. whatever, programing is not a easy, we have to keep workhard.
#define NAME rest of line
where NAME is encountered as a token, it is replace by rest of line.
ex:
named = NAME//named = rest of line
#define MAC(x,y) argument1:x argument2:y
when MAC is used,two argument strings must be presented. They will replace x and y when MAC() is expanded.
ex:
expanded = MAC(foo bar, yuk yuk)//expanded = argument1:foo bar argument2:yuk yuk
ATTENTION: marco is NOT function. marco names can NOT be overloaded, and the macro preprocessor can NOT handle recursive calls.do NOT using them unless you have to. they just simply rearrange the program text before the compiler sees it. following the convention to name macros using lots of CAP letters.
#undef X
ensures that no macro called X is defined.
Conditional Compilation:
#ifdef EXPRESSION1
statement1
#endif
for the compiler to see statement1 unless a macro called EXPRESSION1 has been #defind.
#ifndef EXPRESSION2
statement2
#endif
for the compiler to see statement2 unless a macro called EXPRESSION2 hasnot been #defind.
#if EXPRESSION3
statement3
#elif EXPRESSION4
statement4
#else
statement5
#endif
for the compiler, if EXPRESSION3 is true(not 0), compile statement3. or if EXPRESSION4 is true, compile statement4. if all of them is not true, then comiple statement5.
Including File Compilation:
#include < iostream>//standard library header file
#include < iostream.h>//old-style standard library header file
#include "IO.h" //user identitiy header file
#include "../file.h" //header file under unix parent directory
#include "/usr/local/file.h" //complete path name under unix
#include "..\file.h" //header file under dos parent directory
#include "\usr\local\file.h" //complete path name under dos
difference between #include<...> and #include"..."
#include <...> //compiler starts to search form standard library path name
#include "..." //compiler starts to search form user working path name
--------------------------------------------------------------------------------
not just those pretreatments are in cpp. but they are the most famous we see. they are too powerful to easy to lose control. i suggest thinking again before you use them. but, unfortunately, we have to use them a lot. so it is importance that design before you writing. whatever, programing is not a easy, we have to keep workhard.
Saturday, October 18, 2008
UsingClass3-inheritance
//animal.h
#include < iostream.h>
class Animal_T{
public:
  Animal_T():age(1){
    name=new char();
  }
  bool ChangeTheName(){
    cin>>name;
    return true;
  }
  char* DisplayName(){
    return name;
  }
  int DisplayAge(){
    return age;
  }
  bool PassDay(){
    cout<<"a day pass..."<< endl;
    age++;
    return true;
  }
  virtual bool ChangeName()=0;
  virtual bool Display()=0;
  virtual bool Cry()=0;
private:
  char *name;
  int age;
};
class Dog_T:public Animal_T{
public:
  Dog_T(){
    ChangeName();
  }
  bool ChangeName(){
    cout<<"input your dog's new name:";
    Animal_T::ChangeTheName();
    return true;
  }
  bool Display(){
    cout<<"your dog's name is "<< DisplayName()<< endl;
    cout<<"your dog is "<< DisplayAge()<<" days old"<< endl;
    return true;
  }
  bool Cry(){
    cout<<"WoWo"<< endl;
    return true;
  }
};
class Cat_T:public Animal_T{
public:
  Cat_T(){
    ChangeName();
  }
  bool ChangeName(){
    cout<<"input your cat's new name:";
    Animal_T::ChangeTheName();
    return true;
  }
  bool Display(){
    cout<<"your cat's name is "<< DisplayName()<< endl;
    cout<<"your cat is "<< DisplayAge()<<" days old"<< endl;
    return true;
  }
  bool Cry(){
    cout<<"MiaoMiao"<< endl;
    return true;
  }
};
//main.cpp
#include "animal.h"
int main(){
  int n;
  Animal_T *pet;
  int kind=0;
  ///////////////////
  //菜单
  ///////////////////
  do{
    while(!(kind==1 || kind==2)){
      cout<<"1.cat 2.dog"<< endl;
      cout<<"your input is:";
      cin>>kind;
      cout<< endl;
      if(kind==1){
        pet=new Cat_T();
      }else if(kind==2){
        pet=new Dog_T();
      }
    };
    do{
      cout<< endl;
      cout<<"Menu:"<< endl;
      cout<<" 1.Display info"<< endl;
      cout<<" 2.Past one day"<< endl;
      cout<<" 3.Cry"<< endl;
      cout<<" 4.Charge name"<< endl;
      cout<<" 5.Charge pet"<< endl;
      cout<<" 6.Exit"<< endl;
      cout<<"and your input is:";
      cin>>n;
      cout<< endl;
    }while(n<1 || n>6);
    switch(n){
    case 1:
      pet->Display();
      break;
    case 2:
      pet->PassDay();
      break;
    case 3:
      pet->Cry();
      break;
    case 4:
      pet->ChangeName();
      break;
    case 5:
      delete pet;
      kind=0;
      break;
    }
  }while(n!=6);
  ///////////////////////
  ///////////////////////
  cout<<"See you"<< endl;
  return 0;
}
--------------------------------------------------------------------------------
this is a example of class inheritance with virtual functions. so, i don't want to talk to much for this.
but, if you have any question of this, please ask. i will try my best answer you.
#include < iostream.h>
class Animal_T{
public:
  Animal_T():age(1){
    name=new char();
  }
  bool ChangeTheName(){
    cin>>name;
    return true;
  }
  char* DisplayName(){
    return name;
  }
  int DisplayAge(){
    return age;
  }
  bool PassDay(){
    cout<<"a day pass..."<< endl;
    age++;
    return true;
  }
  virtual bool ChangeName()=0;
  virtual bool Display()=0;
  virtual bool Cry()=0;
private:
  char *name;
  int age;
};
class Dog_T:public Animal_T{
public:
  Dog_T(){
    ChangeName();
  }
  bool ChangeName(){
    cout<<"input your dog's new name:";
    Animal_T::ChangeTheName();
    return true;
  }
  bool Display(){
    cout<<"your dog's name is "<< DisplayName()<< endl;
    cout<<"your dog is "<< DisplayAge()<<" days old"<< endl;
    return true;
  }
  bool Cry(){
    cout<<"WoWo"<< endl;
    return true;
  }
};
class Cat_T:public Animal_T{
public:
  Cat_T(){
    ChangeName();
  }
  bool ChangeName(){
    cout<<"input your cat's new name:";
    Animal_T::ChangeTheName();
    return true;
  }
  bool Display(){
    cout<<"your cat's name is "<< DisplayName()<< endl;
    cout<<"your cat is "<< DisplayAge()<<" days old"<< endl;
    return true;
  }
  bool Cry(){
    cout<<"MiaoMiao"<< endl;
    return true;
  }
};
//main.cpp
#include "animal.h"
int main(){
  int n;
  Animal_T *pet;
  int kind=0;
  ///////////////////
  //菜单
  ///////////////////
  do{
    while(!(kind==1 || kind==2)){
      cout<<"1.cat 2.dog"<< endl;
      cout<<"your input is:";
      cin>>kind;
      cout<< endl;
      if(kind==1){
        pet=new Cat_T();
      }else if(kind==2){
        pet=new Dog_T();
      }
    };
    do{
      cout<< endl;
      cout<<"Menu:"<< endl;
      cout<<" 1.Display info"<< endl;
      cout<<" 2.Past one day"<< endl;
      cout<<" 3.Cry"<< endl;
      cout<<" 4.Charge name"<< endl;
      cout<<" 5.Charge pet"<< endl;
      cout<<" 6.Exit"<< endl;
      cout<<"and your input is:";
      cin>>n;
      cout<< endl;
    }while(n<1 || n>6);
    switch(n){
    case 1:
      pet->Display();
      break;
    case 2:
      pet->PassDay();
      break;
    case 3:
      pet->Cry();
      break;
    case 4:
      pet->ChangeName();
      break;
    case 5:
      delete pet;
      kind=0;
      break;
    }
  }while(n!=6);
  ///////////////////////
  ///////////////////////
  cout<<"See you"<< endl;
  return 0;
}
--------------------------------------------------------------------------------
this is a example of class inheritance with virtual functions. so, i don't want to talk to much for this.
but, if you have any question of this, please ask. i will try my best answer you.
Thursday, October 16, 2008
a little Thought
Google Blog is difficult to use.
first, in "Posting", it is too much different between "Edit html" and "Compose". and i cannot change those two mode when i am editing. because once i change it, lost of thing will be changed.
second, i can use "SPACE" easily when i want to leave some blank space. i have to use & nbsp instead. this make me a lot of trouble. and thank god, i know a little html.
third, it is powerful, but away makes things complicated. too many html i have to use. i just want to posting sth simply and beautifully.
fourth, things away go wrong. it makes me feeling it is a beta vision.
but, however, comparing with msn space, it have much more freedom. and i think it will become better and better.
Saturday, October 11, 2008
Using Class 2--similar class
//dog.h
class Dog_T{
public:
  Dog_T();
  ~Dog_T();
  bool Cry();
  bool ChangeName();
  bool PassDay();
  bool Display();
private:
  char *name;
  int age;
};
//dog.cpp
........
//cat.h
class Cat_T{
public:
  Cat_T();
  ~Cat_T();
  bool Cry();
  bool ChangeName();
  bool PassDay();
  bool Display();
private:
  char *name;
  int age;
};
//cat.cpp
.......
//main.cpp
int main(){
  Cat_T *cat;
  Dog_T *dog;
  int n=1;
  int k=0;
  while(n!=6){
  while(!(k==1||k==2)){
    cout<<"1.cat 2.dog\nyour choice:";
    cin>>k;
    if(k==1)
      cat=new Cat_T();
    else if(k==2)
      dog=new Dog_T();
    }
    do{
      cout<< endl;
      cout<<"1.change name"<< endl;
      cout<<"2.pass a day"<< endl;
      cout<<"3.display"<< endl;
      cout<<"4.cry"<< endl;
      cout<<"5.change animal"<< endl;
      cout<<"6.exit"<< endl;
      cout<<"your input:";
      cin>>n;
      cout<< endl;
    }while(n<1 || n>6);
    switch(n){
    case 1:
      if(k==1)
        cat->ChangeName();
      if(k==2)
        dog->ChangeName();
      break;
    case 2:
      if(k==1)
        cat->PassDay();
      if(k==2)
        dog->PassDay();
        break;
    case 3:
      if(k==1)
        cat->Display();
      if(k==2)
        dog->Display();
      break;
    case 4:
      if(k==1)
        cat->Cry();
      if(k==2)
        dog->Cry();
      break;
    case 5:
      if(k==1)
        delete cat;
      if(k==2)
        delete dog;
      k=0;
      break;
    default:
      break;
    }
  }
  return 0;
}
--------------------------------------------------------------------------------
this a two classes. and they make a lot complexity. then, what about three, four or more classes?
this is a bad examle for using two similar classes. i wrote it down because i want to give a better one (i don't dare to say it's the best) on this weekend. i wish to make the next one feeling much better than this one.
i skiped the dog.cpp and cat.cpp because they're similar to the last time i have given. but, anyway you can easily guess what it should be if you want.
this is.see you
class Dog_T{
public:
  Dog_T();
  ~Dog_T();
  bool Cry();
  bool ChangeName();
  bool PassDay();
  bool Display();
private:
  char *name;
  int age;
};
//dog.cpp
........
//cat.h
class Cat_T{
public:
  Cat_T();
  ~Cat_T();
  bool Cry();
  bool ChangeName();
  bool PassDay();
  bool Display();
private:
  char *name;
  int age;
};
//cat.cpp
.......
//main.cpp
int main(){
  Cat_T *cat;
  Dog_T *dog;
  int n=1;
  int k=0;
  while(n!=6){
  while(!(k==1||k==2)){
    cout<<"1.cat 2.dog\nyour choice:";
    cin>>k;
    if(k==1)
      cat=new Cat_T();
    else if(k==2)
      dog=new Dog_T();
    }
    do{
      cout<< endl;
      cout<<"1.change name"<< endl;
      cout<<"2.pass a day"<< endl;
      cout<<"3.display"<< endl;
      cout<<"4.cry"<< endl;
      cout<<"5.change animal"<< endl;
      cout<<"6.exit"<< endl;
      cout<<"your input:";
      cin>>n;
      cout<< endl;
    }while(n<1 || n>6);
    switch(n){
    case 1:
      if(k==1)
        cat->ChangeName();
      if(k==2)
        dog->ChangeName();
      break;
    case 2:
      if(k==1)
        cat->PassDay();
      if(k==2)
        dog->PassDay();
        break;
    case 3:
      if(k==1)
        cat->Display();
      if(k==2)
        dog->Display();
      break;
    case 4:
      if(k==1)
        cat->Cry();
      if(k==2)
        dog->Cry();
      break;
    case 5:
      if(k==1)
        delete cat;
      if(k==2)
        delete dog;
      k=0;
      break;
    default:
      break;
    }
  }
  return 0;
}
--------------------------------------------------------------------------------
this a two classes. and they make a lot complexity. then, what about three, four or more classes?
this is a bad examle for using two similar classes. i wrote it down because i want to give a better one (i don't dare to say it's the best) on this weekend. i wish to make the next one feeling much better than this one.
i skiped the dog.cpp and cat.cpp because they're similar to the last time i have given. but, anyway you can easily guess what it should be if you want.
this is.see you
Wednesday, October 8, 2008
the most simple class
//dog.h
#ifndef IOSTREAM
#define IOSTREAM
#include < iostream.h>
#endif
class Dog_T
{
public:
  Dog_T();
  bool ChangeName();
  bool PastOneDay();
  bool DisplayInfo();
private:
  int iAge;
  char* cpName;
};
//dog.cpp
#include "dog.h"
Dog_T::Dog_T():iAge(1){
  cpName=new char();
  ChargeName();
}
bool Dog_T::ChargeName(){
  cout<< "Please input your dog`name:";
  cin>>cpName;
  cout<< endl;
  return true;
}
bool Dog_T::DisplayInfo(){
  cout<<"Dog`s name is "<< cpName<<"."<< endl;
  cout<<"Dog is "<< iAge<<" days old."<< endl;
  cout<< endl;
  return true;
}
bool Dog_T::PastOneDay(){
  iAge++;
  return true;
}
//main.cpp
#ifndef IOSTREAM
#define IOSTREAM
#include
#endif
#include "dog.h"
////////////////menu//////////////
int main(){
  int n;
  Dog_T myDog;
  do{
    do{
      cout<<"Menu:"<< endl;
      cout<<" 1.Display Dog`s info"<< endl;
      cout<<" 2.Past one day"<< endl;
      cout<<" 3.Charge Dog`name"<< endl;
      cout<<" 4.Exit"<< endl;
      cout<<"and your input is:";
      cin>>n;
      cout<< endl;
    }while(n<1 || n>4);
    switch(n){
      case 1:
        myDog.DisplayInfo();
        break;
      case 2:
        myDog.PastOneDay();
        break;
      case 3:
        myDog.ChargeName();
        break;
      }
    }while(n!=4);
//////////////////////////////////////////////
  cout<<"See you"<< endl;
  return 0;
}
-------------------------------------------------------------------------
this is the most simple class i think.
BUT,there is still has some problem that i don't know how to deal with. It is once i input the letter insteal of number or a long number. it will entry a dead loop... i hope someone could tell me why.
and I think there must be something wrong about my English grammar or spell. if i do, please tell me.
at last, if you have any suggest or other, please tell me also.
#ifndef IOSTREAM
#define IOSTREAM
#include < iostream.h>
#endif
class Dog_T
{
public:
  Dog_T();
  bool ChangeName();
  bool PastOneDay();
  bool DisplayInfo();
private:
  int iAge;
  char* cpName;
};
//dog.cpp
#include "dog.h"
Dog_T::Dog_T():iAge(1){
  cpName=new char();
  ChargeName();
}
bool Dog_T::ChargeName(){
  cout<< "Please input your dog`name:";
  cin>>cpName;
  cout<< endl;
  return true;
}
bool Dog_T::DisplayInfo(){
  cout<<"Dog`s name is "<< cpName<<"."<< endl;
  cout<<"Dog is "<< iAge<<" days old."<< endl;
  cout<< endl;
  return true;
}
bool Dog_T::PastOneDay(){
  iAge++;
  return true;
}
//main.cpp
#ifndef IOSTREAM
#define IOSTREAM
#include
#endif
#include "dog.h"
////////////////menu//////////////
int main(){
  int n;
  Dog_T myDog;
  do{
    do{
      cout<<"Menu:"<< endl;
      cout<<" 1.Display Dog`s info"<< endl;
      cout<<" 2.Past one day"<< endl;
      cout<<" 3.Charge Dog`name"<< endl;
      cout<<" 4.Exit"<< endl;
      cout<<"and your input is:";
      cin>>n;
      cout<< endl;
    }while(n<1 || n>4);
    switch(n){
      case 1:
        myDog.DisplayInfo();
        break;
      case 2:
        myDog.PastOneDay();
        break;
      case 3:
        myDog.ChargeName();
        break;
      }
    }while(n!=4);
//////////////////////////////////////////////
  cout<<"See you"<< endl;
  return 0;
}
-------------------------------------------------------------------------
this is the most simple class i think.
BUT,there is still has some problem that i don't know how to deal with. It is once i input the letter insteal of number or a long number. it will entry a dead loop... i hope someone could tell me why.
and I think there must be something wrong about my English grammar or spell. if i do, please tell me.
at last, if you have any suggest or other, please tell me also.
Subscribe to:
Posts (Atom)