Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / Win32

How to Create an Object Oriented Win32 Console Application

Rate me:
Please Sign up or sign in to vote.
2.67/5 (10 votes)
29 May 2010CPOL2 min read 43.6K   905   19   9
How to create an object oriented Win32 Console application
Image 1

Introduction

Designing apps in Win32 Console Mode is pretty boring. There exist no User interface libraries. But this simple app demonstrates how to create an object oriented application in a Win32 console. But before we begin, let's talk about a header file called cExampleTextMode.h.

The C Header File cExampleTextMode.h contains the functions that are of outmost importance for the game to be able to position the cursor and text characters in the correct x and y location. Also included in the file textmode.h are the functions to draw simple dialogs, draw lines, plot points and of course set a text character's color.

Functions in cExampleTextMode.h:

delay()         delays x milliseconds
gotoxy()        Goes to position on 80x25 console screen
setcolor()      Changes text and background color
clrbox()        draws colored box without frames
box()           draws framed box
gotoxy() only works with printf(), cprintf() and not cout<

The gotoxy(int x, int y) Method

The gotoxy(int x, int y) event is called upon to set the cursor at a specific x and y location.

C++
void gotoxy(int x, int y)
{
    COORD coord;
    coord.X = x; coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
	return;
}

The setcolor(WORD Color) Method

The setcolor(WORD color) event is called upon to set the color of text characters.

C++
void setcolor(WORD color)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
	return;
}

The clrscr() Method

The clrscr() event is called upon to clear the screen or to fill the screen with specific color.

C++
void clrscr()
{
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '), 
		dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, 
		dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
	return;
}  

The Class Header File cExample.h

And now the application itself. We begin by declaring a class header file and then we continue with defining the class member functions in the file cExample.h.
Our class cExample will inherit some function methods from the class cExampleTextMode which is done by using this syntax:

C++
class cExample: public cExampleTextMode 

We choose not to define and embody our member functions in the header file, we will just put the prototypes in cExample.h. We will define the function and method bodies in Example.cpp.

C++
// cExample.h

#ifndef CExample_H
#define CExample_H

#include "cExampleTextMode.h"

class cExample: public  cExampleTextMode  	// cExample inherits methods 
					// from cExampleTextMode
{
   public:
	cExample();       // default constructor

	cExample( int i);	//  constructor, always have a parameter 
			// in the constructor header to avoid link errors
	~cExample();     	// destructor


	// class member functions
	void ExampleInfo(void);

	void ExampleMethod(void);

};

#endif CExample_H

The Class Definition File cExample.cpp

In cExample.cpp, we will define and embody our member functions with function calls, syntaxes and statements. Our class cExample inherits function methods from the class cExampleTextMode so we need to include this syntax:

C++
cExample::cExample( int i) : cExampleTextMode()

Now the function methods in cExampleTextMode.h have become "one" with the function methods in our class cExample. We have also placed function calls we want to be initialized at start up in the constructor so that when our class is being instantiated, those function methods get called and executed.

C++
// cExample.cpp

#include "cExample.h"

cExample::cExample( int i) :  cExampleTextMode()	// cExample inherits methods 
						// from cExampleTextMode
{
	ExampleInfo();
	ExampleMethod();
}

void cExample::ExampleInfo(void)
{
	setcolor(15);
	clrscr();

	clrbox(2,2,78,6,224);
	gotoxy(3,2);printf(" %s ",ver);
	 gotoxy(3,3);printf(" %s ",prgdate);
	gotoxy(3,4);printf(" %s ",author);
	gotoxy(3,5);printf(" %s ",website);

	setcolor(15);gotoxy(1,23);
}

void cExample::ExampleMethod(void)
{
	clrbox(20,10,60,16,31);
	   box(20,10,60,16,15,15,"ExampleMethod Title");
	gotoxy(22,12);printf("ExampleMethod Text out ");
	setcolor(15);gotoxy(1,23);
}

The Constructor

We have placed function calls we want to be initialized at start up in the constructor so that when our class is being instantiated, those function methods get called and executed.

C++
cExample::cExample( int i) :  cExampleTextMode()	// cExample inherits methods 
						// from  cExampleTextMode
{
	ExampleInfo();
	ExampleMethod();
}

The cExample::ExampleMethod(void) Method

The method cExample::ExampleMethod(void) is executed when an instance for our class is created and since we placed a call to it in the constructor, it will proceed to draw a dialog box in text mode titled "ExampleMethod Title" and print the text "ExampleMethod Text out " in the center of the dialog box.

C++
void cExample::ExampleMethod(void)
{
	clrbox(20,10,60,16,31);
	   box(20,10,60,16,15,15,"ExampleMethod Title");
	gotoxy(22,12);printf("ExampleMethod Text out ");
	setcolor(15);gotoxy(1,23);
}

The Main File

The file cExample_main.cpp is where we create an instance for our class and run our application:

C++
//cExample_main.cpp

#include "cExample.h"

int main()
{
  cExample *Example = new cExample(1);

  return 0;
}

And that is how easy it is to create a simple object oriented Win32 console application.

Thanks for reading.

History

  • 2010-05-13 Updates made

License

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


Written By
Sweden Sweden
About me:
I attended programming college and I have a degree in three most famous and successful programming languages. C/C++, Visual Basic and Java. So i know i can code. And there is a diploma hanging on my wall to prove it.
.
I am a professional, I am paid tons of cash to teach or do software development. I am roughly 30 years old .

I hold lectures in programming. I have also coached students in C++, Java and Visual basic.

In my spare time i do enjoy developing computer games, and i am developing a rather simple flight simulator game
in the c++ programming language using the openGL graphics libray.

I've written hundreds of thousands of code syntax lines for small simple applications and games.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Manikandan1024-Jun-14 20:45
professionalManikandan1024-Jun-14 20:45 
GeneralParameterless constructor Pin
TobiasP2-Dec-10 3:18
TobiasP2-Dec-10 3:18 
Since the article states // constructor, always have a parameter in the constructor header to avoid link errors, I guess it should be noted in case someone tries to learn from this article that that is incorrect information and that there is no problem having parameterless constructors.
GeneralMy vote of 1 Pin
Niklas L7-Jun-10 7:12
Niklas L7-Jun-10 7:12 
GeneralMy vote of 1 Pin
Member 15069667-Jun-10 6:40
Member 15069667-Jun-10 6:40 
GeneralMy vote of 1 Pin
eds2-Jun-10 8:45
eds2-Jun-10 8:45 
GeneralMy vote of 2 Pin
wtwhite17-May-10 19:20
wtwhite17-May-10 19:20 
GeneralMy vote of 2 Pin
Aescleal17-May-10 5:31
Aescleal17-May-10 5:31 
GeneralArticle writing suggestion Pin
David MacDermot14-May-10 8:40
David MacDermot14-May-10 8:40 
GeneralRe: Article writing suggestion [modified] Pin
chaplyn10814-Feb-11 10:07
chaplyn10814-Feb-11 10:07 

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.