Classes

Started by
7 comments, last by Ethan 22 years, 5 months ago
Hi, Can somebody tell me if i must have an Console Application or can i use Win32 Application for classes?
Advertisement
If i''m understanding your question right, any c++ application can take advantage of classes, it''s built in to the language, not the output platform.
when i write the source like other samples but it doesn''t will work
Well that all depends on what commands exactly you are using...some will only work in console mode, some in 16/32 bit windows mode, etc.

what are the exact commands you are using?
quote:Original post by Ethan
Hi,
Can somebody tell me if i must have an Console Application or can i use Win32 Application for classes?

Just checking: OOP/C++ classes have NOTHING to do with Window classes. Hope you know that.
class CMyObject  // OO class in C++{  CMyObject() : m_iData(0)  {  }  int GetData() { return m_iData; }private:  int m_iData;}// using in a console app:#include &ltiostream>int main(int argc, char *argv[]){  using namespace std;  CMyObject *pMyObj = new CMyObject();  cout << pMyObj->GetData() << endl;  return 0;}  


Window class:
#include &ltwindows.h>int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,                   LPCTSTR lpCmdLine, int nCmdShow){  // declare and define window class:  WNDCLASSEX wc;  wc.cbClsSize = sizeof(WNDCLASSEX);  wc.cbClsExtra = 0;  wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);  wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);  wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);  wc.hCursor = LoadCursor(NULL, IDC_ARROW);  wc.hInstance = hInstance;  wc.lpfnWndProc = WndProc;  wc.lpszClassName = szClassName;  wc.lpszMenuName = NULL;  // register window class with Windows:  if(!RegisterClassEx(&wc))    return -1;  // go on to create window, display etc here  ...  return 0;}  

As you can see, object-oriented classes have nothing to do with window classes. Window classes can only be used in full-fledged Win32 Applications (entry point WinMain).

[EDIT]: damn tags!


I wanna work for Microsoft!

Edited by - Oluseyi on November 7, 2001 12:53:13 PM
quote:Original post by Oluseyi
Window classes can only be used in full-fledged Win32 Applications (entry point WinMain).

No. Any Win32 application (whether windowed or console) can use window-classes and open windows etc. (and a windowed Win32 application can open a console-window if necessary).
quote:Original post by Dactylos
No. Any Win32 application (whether windowed or console) can use window-classes and open windows etc. (and a windowed Win32 application can open a console-window if necessary).

I took a wild shot with that one. I''ve written "windowed" Win32 applications with consoles, so I should have guessed the inverse would be possible. However, how do you obtain the HINSTANCE of the application (to supply to the window class)?


I wanna work for Microsoft!
GetModuleHandle(NULL) will get your HINSTANCE.
quote:Original post by Oluseyi
Original post by Ethan
Hi,
Can somebody tell me if i must have an Console Application or can i use Win32 Application for classes?

Just checking: OOP/C++ classes have NOTHING to do with Window classes. Hope you know that.
class CMyObject  // OO class in C++{  CMyObject() : m_iData(0)  {  }  int GetData() { return m_iData; }private:  int m_iData;} 



Just a minor point, you''ve missed a semi-colon at the end of your class…

class CMyObject
{

}; // semi-colon is vital

I would also recommend the following while we''re on the topic…

int GetData() const { return m_iData; }

Since you''re not modifying any of the members of the class, the function is logically const.

HTH,

Stuart.

This topic is closed to new replies.

Advertisement