Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

How to Create an Object Oriented Win32 Console Application

, 29 May 2010
Rate this:
Please Sign up or sign in to vote.
How to create an object oriented Win32 Console application

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.

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.

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.

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:

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.

// 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:

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.

// 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.

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.

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:

//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)

Share

About the Author

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 PinprofessionalManikandan1024-Jun-14 20:45 
GeneralParameterless constructor PinmemberTobiasP2-Dec-10 3:18 
GeneralMy vote of 1 PinmemberNiklas Lindquist7-Jun-10 7:12 
You set out to do something in the article title, and then the content is just something else.
 
You use C++, but that doesn't make it object oriented.
GeneralMy vote of 1 PinmemberMember 15069667-Jun-10 6:40 
GeneralMy vote of 1 Pinmembereds2-Jun-10 8:45 
GeneralMy vote of 2 Pinmemberwtwhite17-May-10 19:20 
GeneralMy vote of 2 PinmemberAescleal17-May-10 5:31 
GeneralArticle writing suggestion PinmemberDavid MacDermot14-May-10 8:40 
GeneralRe: Article writing suggestion [modified] Pinmemberchaplyn10814-Feb-11 10:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 30 May 2010
Article Copyright 2010 by Software_Developer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid