|
I write a program in C++ that asks the user for names of students and their cooresponding midterm scores ...
C++ Interview Questions and Answers
(Continued from previous question...)
I write a program in C++ that asks the user
for names of students and their cooresponding
midterm scores ...
I write a program in C++ that asks the user
for names of students and their cooresponding
midterm scores, at the end, it displays each
person, their average, gpa, class ave, total
a's, b's, etc. I make the exe and it works
fine, how do I make it run in something other
than the command prompt, I mean, make it so
that it is like a page where you can just
enter the info in and hit enter and it
display the calculates in say, a box
that was empty at the bottom of the page?
--------------------------------------------
I. Event Driven Programming, Part #1
-------------------------------------------
The most challenging hurdle that most new
programmers face when moving from the command
line to a graphical user interface is getting
their head around event-driven programming.
While there are some rare exceptions--I worked
on a couple of PlayStation2 games that had menu
systems that were only semi-event driven--
practically ALL graphical applications use
an event-driven architecture.
In a typical command line application,
you get some input from the user at startup,
immediately do some work based on the input
and then exit. If you need more input from
the user you display a prompt and wait there
until the user enters something. [KEY POINT:
The *program* controls the flow of execution.]
In an event driven application, the program
starts up and then waits there for "something"
to happen. "Something" might be a keystroke,
mouse click, tick from a timer or any number
of other things. Most often, it's related to
some sort of input from the user. When
"something" happens, the program then
responds and does some work.
This "something" is called an EVENT.
Events are the core of event-driven programming.
Events tell the program when the user does
something that the program needs to respond to.
[KEY POINT: The *user*
controls the flow of execution.]
Here is some pseudo-code for a typical
command line program:
void main()
{
getKeystroke();
doSomeWork();
exit();
}
Here is the same pseudo-code converted into
a home-brew event-driven
program:
void main()
{
//do forever (this loop is also called the "pump")
while(true) {
// every time "something" happens, call
// the function that goes along with it
if ( getKeystroke() ) {
onKeyPress();
}
if ( exitKeyPressed) {
exit();
}
}
}
void onKeyPress() {
doSomeWork();
}
Cheesy as that sounds, that is exactly how
most event-driven applications work...
including large Windows applications. I am
over-simplifying a bit, but not by much.
In addition to events, Windows adds an extra
layer called MESSAGES to make its job easier.
Messages are just bits of data
(a UINT to be exact) with some
parameters attached that get passed around
in order to tell a program what event just
happened. Each MESSAGE passes a number, which
represents a specific EVENT. Then the program's
EVENT HANDLER is called with that message number.
An event handler is a function in your program
that handles an event; or in other words,
a function that does "something"
when "something" happens.
In our pseudo-code listing, this concept
could be written like so:
void main()
{
// do forever ("MESSAGE PUMP")
while(true) {
// "translate" the events... every time
//"something" happens,
// set the variable "message"
//to the number that corresponds
int message;
if ( getKeystroke() ) {
message = 1;
}
if ( exitKeyPressed) {
message = 2;
}
// "dispatch" the events... call a main
// event handler to
// figure out which sub-event handler to call
doEvent(message);
}
}
// MAIN EVENT HANDLER
void doEvent(int message)
{
switch(message) {
case 1:
onKeyPress();
break;
case 2:
exit();
break;
}
}
// KEYPRESS EVENT HANDLER
void onKeyPress() {
doSomeWork();
}
This may seem like a round-about way to
do things, but messages make it easier for
Windows to pass important events between
different programs. If the program doesn't
know what to do with an event, it can also
pass it to the Windows default event handler.
The default event handler is just another
function that figures out what to do with the
event when you aren't sure or simply don't
want to deal with it.
Following is code for a real Windows program
to show you what I mean. This code will compile
and run with Microsoft Visual C\C++6.0; it should
also run with Borland or any other C\C++
compiler that supports the Windows API. Just in
case you aren't sure, the Windows API is a
set of functions, libraries, etc., provided by
the operating system to do the basic things that
are required by a graphical program. This
saves you from re-inventing the wheel every time
you write a program; the code for things that
all windows programs need, like the basic
functionality of a button, is already done
for you. Other platforms, such as the Mac or
X Windows\Motif, have similar APIs. From my
personal experience, I can say that the Windows
API is more full-featured and does a lot more
things for you than many of the other platforms,
at the expensive of being enormous, sometimes
over-featured and often overwhelming.
The following code will create a basic window.
It also handles the PAINT event, and provides
a sample function that draws a circle on the
window. (I apologize in advance if some of
the code wraps funny. You may need to copy and
paste this into a mono-spaced editor to read it
more clearly.)
**********BEGIN LISTING****************
#define STRICT // strict type checking
#define WIN32_LEAN_AND_MEAN//exclude seldom-used code
#include <windows.h>//needed to write windows code
/*-----------------------------------------------
Paints the Window Client Area
This is your event handler for the PAINT event.
For each thing you want to do, such as handle
a keystroke or a button press, you'll have
an event handler function just like this.
Parameters: HWND hWnd - handle to current window.
Windows identifies each form and control in an
application by assigning it a handle.
Most API functions need to know the handle of a window
in order to do anything interesting to it.
Returns: <nothing>
---------------------------------------------*/
void onPaint(HWND hWnd)
{
PAINTSTRUCT ps;
// structure for paint info
HDC hDC;
// handle to graphics device context,
//i.e. where to paint
// create device context, prepare for painting
hDC = BeginPaint(hWnd, &ps);
// draw a circle
Ellipse(hDC, 0, 0, 50, 50);
// update the window client area with
//whatever else we want
// delete device context
DeleteDC(hDC);
// end painting
EndPaint(hWnd, &ps);
}
/*----------------------------------------------
Windows Message Pump Callback Routine
This is your MAIN EVENT HANDLER. This function
takes the events that Windows passes in,
figures out what they are, and then calls your
additional
sub-event handler functions to handle the event.
Any events that aren't handled get passed on
to the default event handler supplied by Windows
(DefWindowProc).
This saves you from having to write code
for events you don't care about, like, say,
a notice that the window was resized.
Parameters: HWND hWnd - our window's handle
UINT wMsg - message number
WPARAM wParam - word parameter, contents
differ for each message
LPARAM lParam - long parameter, contents differ for
each message
Returns: 0 if we handled the message,
result code otherwise
-------------------------------------------*/
LRESULT CALLBACK WndProc(HWND hWnd,
UINT wMsg, WPARAM wParam, LPARAM lParam)
{
// select and handle possible window messages
switch(wMsg){
// window created, initialize application
case WM_CREATE:
// do whatever you need to do on startup here
break;
// window close suggested
case WM_CLOSE:
// do whatever you need to do on shutdown here
DestroyWindow(hWnd);
break;
// paint window requested
case WM_PAINT:
onPaint(hWnd);
break;
// application terminated
case WM_DESTROY:
PostQuitMessage(0);
break;
//pass on other unhandled messages to default handler
default:
return(DefWindowProc(hWnd, wMsg, wParam, lParam));
}
// return 0 if we handled the message
return 0L;
}
/*------------------------------------------------
Main Entry Point
-- this is the equivalent of "void main()"
for a windows program
Parameters:
HINSTANCE hInstance - current program instance
HINSTANCE hPrevInst -handle to the previous instance
LPSTR lpszCmdLine - command-line arguments
int nCmdShow - window size/mode
Returns: the value of PostQuitMessage
---------------------------------------------------*/
int PASCAL WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInst, LPSTR
lpszCmdLine, int nCmdShow)
{
HWND hWnd; // window handle from
create window
MSG wMsg; // message from
GetMessage
WNDCLASS wndclass; // window class
structure
char *strProgramName = "My Program Name";
// the name of your
program
// if this is the first instance of the
application, register the window structure...
// back in the day, system resources were very tight,
and programs that used several similar looking
windows could save loads of memory by storing
only one "design" for a window in memory. new
copies of the application in memory,
or "instances", could point to the window design
that was already in RAM instead of loading it
again. even though it's less important today now
that computers have gobs of memory, windows
still works this way; it has a number of misc
advantages.
if( ! hPrevInst ) {
wndclass.style = CS_HREDRAW | CS_VREDRAW;
//window style
wndclass.lpfnWndProc = WndProc;
//address to event handling procedure
wndclass.cbClsExtra = 0;
// no extra class data
wndclass.cbWndExtra = 0;
// no extra window data
wndclass.hInstance = hInstance;
//handle to the current instance
wndclass.hCursor =LoadCursor(NULL, IDC_ARROW);
//stock arrow cursor
wndclass.hIcon =LoadIcon(NULL, IDI_APPLICATION);
//stock icon
wndclass.lpszMenuName = NULL;
// menu name
wndclass.hbrBackground =(HBRUSH)(COLOR_APPWORKSPACE + 1);
//background color brush
wndclass.lpszClassName = strProgramName;
//window class name
RegisterClass(&wndclass);
}
// create the main window. you can
change the parameters to this function
to make different kinds of windows
hWnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
// extended window style
strProgramName, // program name
strProgramName, // window titlebar caption
WS_OVERLAPPEDWINDOW | WS_SYSMENU,// basewindow style
CW_USEDEFAULT, // default x position
CW_USEDEFAULT, // default y position
CW_USEDEFAULT, // default width
CW_USEDEFAULT, // default height
NULL, //parent's handle
NULL, // menu handle
hInstance, // handle to the instance
NULL); //initialization data
// display the main window; unless you
make the window visible as part of the window
// style you specify, the window is invisible by default
ShowWindow(hWnd, nCmdShow);
// windows "message pump"... remember
that "while(true)" loop that sat around
// waiting for events in the pseudocode?
well, here it is! through this loop, messages
// are parsed and eventually sent by
windows to your main event handler, WndProc
while(GetMessage(&wMsg,0,0,0)){
TranslateMessage(&wMsg);
DispatchMessage(&wMsg);
}
// return exit code from program,
obtained from PostQuitMessage
return wMsg.wParam;
}
**********END LISTING****************
--------------------------------------------
II. Event Driven Programming, Part #2
---------------------------------------------
If you actually read the previous listing,
you might be reeling a little by now and
wondering if there isn't an easier way.
Thankfully, the answer is "yes".
Most programmers don't want to deal
with the whole window setup routine (WinMain)
or frankly the main event handler (WndProc) either.
Most programmers just want to cut to the chase
and write the application-specific code for
the events they are interested in (i.e. onPaint).
Fortunately, the people who write development
tools are not insensitive to this need. Many
tools exist that will help you do exactly that.
MFC, for example, is a window framework that
does the dirty work for you. MFC handles the
window setup and teardown that is basically the
same for all apps, and allows you to write
only the onWhatever event handler functions
that are meaningful to you. Deep, deep down
it still has a message pump but you'll never
see it... and for most people, that's a good thing.
Many other window frameworks other than MFC
are available and range from basic application
skeletons to libraries that--like MFC--contain
a lot of extra code for other things that GUI
programs like to do: such as send data over
a network or write to a database. ViewKit,
for example, is a basic window framework
for CDE and OSF/Motif developers
on Unix platforms.
----------------------------------------------
III. Widgets, Windows and Gadgets, Part #1
------------------------------------------------
The word WIDGET is a short for "window gadget".
A widget is any control you place on your main window
(or FORM) such as a button, textbox or scrollbar.
Once you understand the basics of how to
get an event-driven window up and running, you'll
want to start adding widgets to make your app,
well, do something interesting. If you wanted
to add a button using the Windows-API, you might
add the following code to your WinMain
function right before the call to ShowWindow:
CreateWindow(
"BUTTON", // button class control name
"Click Me", // default text
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
// window style
30, // horizontal position
30, // vertical position
50, // width
50, // height
hWnd, // handle to parent or owner window
(HMENU)IDC_MYBUTTONID,
// button id, anything you like
hInstance,
// handle to application instance
NULL // pointer to window-creation data
);
Be sure to make assign a unique ID to you button.
Do this by pasting the following line at the
top of the example:
#define IDC_MYBUTTONID 100
Notice that the CreateWindow function is
used to make a button. Buttons, and other widgets,
are really just windows themselves much
like your main application window,
but with special functionality
written into them to make them
operate as buttons.
To make the button actually do anything,
you'll need to attach an event to it. When a button
is clicked, Windows fires the WM_COMMAND
event. It also sets the low-order word of
the wParam message parameter to the ID of the button,
so that you know what button was clicked. To
process this event, you'll need to add the following
code to your WndProc function:
// button clicked
case WM_COMMAND:
if (LOWORD(wParam) == IDC_MYBUTTONID) {
onClickMyButton(hWnd);
}
else {
// some other button got clicked
}
break;
Then you would add a function to serve as
the event handler for your button:
void onClickMyButton(HWND hWnd)
{
MessageBox(hWnd, "Yeah, you clicked me!!",
"I was Clicked", MB_OK);
}
Congratulations! You've just made a real,
working fully graphical pure-Windows API program
that actually does something. It's not much,
but it fully demonstrates the core of what
is involved in producing a simple
application with a GUI.
-----------------------------------------------
IV. Widgets, Windows and Gadgets, Part #2
------------------------------------------------
Just like manually writing the code for
the event pump, most programmers have no
tolerance for manually coding every button on
their form. Not only is it a pain to do,
it's difficult to maintain when you want
to make design changes. While there's
no reason you can't do it that way, the truth
is that most major applications do not
create all of their widgets by calling CreateWindow.
Most windows compilers include what is know
as a RESOURCE COMPILER, which generally also includes
a graphical resource editor. A RESOURCE
EDITOR lets you design your form by dragging
and dropping buttons, scrollbars and other widgets
onto it. When you are done designing, your form is
saved as a RESOURCE SCRIPT and the positions of your
buttons and other widgets are stored inside.
When you compile your application, this script is
compiled into your application by the resource compiler.
Various API calls allow you to simply load even
complete window dialogs from a resource file,
which saves you the trouble of writing code to
position all of the controls.
Tools such as MFC can help you simplify
connecting your code to the resources you create.
However, you can use a resource editor to help
you design your user interfaces whether you use
MFC (or any other framework) or not.
-------------------------------------------
V. Toolkits
-------------------------------------------
Sometimes even a resource editor and window
framework are not enough. Many people like to shortcut
designing their applications even further by using one
of many different toolkits. For example, Tcl and the Tk
Toolkit is one popular means for designing simple GUIs.
Many people develop just the graphical portion of
their application in Tcl\Tk, and then write the remainder
of the application that does the actual work in C++.
This is often a very quick way for people to add a GUI to a
command-line application without investing a large
amount of time programming the interface in C++. Mktclapp
is a tool that can help you do that a bit more easily,
and can be found here:
http://www.hwaci.com/sw/mktclapp/
Some other window toolkits and frameworks
were already mentioned in
the comments.
----------------------------------------------
VI. What Tools to Use
--------------------------------------------
Which development tool(s) to use is a very
personal decision based on a large number of
different factors. Different tools vary greatly in
both performance and ease of use. Some tools
are also better suited to different types of
applications. For example, since we've been
discussing MFC, MFC would be a great choice
for writing a spreadsheet application for
Windows because it is easy to use and creates many
standard kinds of window appearances with less
effort. However, MFC would be a terrible choice
for developing a trendy screen hack because
it makes it far too difficult to get under
the hood. MFC would also be a poor choice for
developing a spreadsheet application if you intended
to port the application to several different platforms,
say Windows and Linux. If you wanted to do something
like that, you might be better off using a different
compiler entirely, perhaps such as GCC,
along with some sort of cross-platform window toolkit.
My best advice is just to shop around and see
what feels right for you. Think about what
kind of application you want to write, and see
if you can find something similar that someone
else has done and find out what they used to do it.
The other thing to consider is how much you are willing
to spend. Some tools--like many of the Linux
tools--are free, but some of the other tools--
like the Microsoft compilers--can be kind of pricey.
On the flip side, if you take a college course,
you might be able to get the student edition of any of
them for fairly cheap.
----------------------------------------------------
VII. Where to Learn More
--------------------------------------------------
Obviously, there is a lot more to learn about
developing applications with graphical user interfaces.
You might consider a course at your local community
college, or at the very least, a trip to your local
library. Many of the "For Dummies" books are
actually very good as are most of the
books published by O'Reilly.
Don't forget to read the compiler documentation,
either. Although I had a programming background to
begin with--both my parents are programmers,
so they taught me ever since I was little--
first taught myself C years ago strictly from the
Borland help files. The MSDN has a wealth of
information and lots of useful examples.
Many of the programming tools available come with
some basic docs or sample files
to get you started.
The following sites have good links to
information (and lots of code
you can swipe :-) ) as well as answers
to common questions related to
programming graphical applications
for various platforms:
Experts Exchange
http://www.experts-exchange.com/
Code Guru
http://www.codeguru.com/
Code Project
http://www.codeproject.com/
MSDN
http://msdn.microsoft.com/
Linoleum: Linux Programming Resources
http://leapster.org/linoleum/
Mac OS X Programming: Getting Started
http://developer.apple.com/macosx/gettingstarted/
(Continued on next question...)
Other Interview Questions
- What is C++?
- What is a modifier?
- What is an accessor?
- Differentiate between a template class and class template.
- When does a name clash occur?
- Define namespace.
- What is the use of ‘using’ declaration.
- What is an Iterator class?
- What is an incomplete type?
- What is a dangling pointer?
- Differentiate between the message and method.
- What is an adaptor class or Wrapper class?
- What is a Null object?
- What is class invariant?
- What do you mean by Stack unwinding?
- Define precondition and post-condition to a member function.
- What are the conditions that have to be met for a condition to be an invariant of the class?
- What are proxy objects?
- Name some pure object oriented languages.
- What is a node class?
- What is an orthogonal base class?
- What is a container class? What are the types of container classes?
- How do you write a function that can reverse a linked-list?
- What is polymorphism?
- How do you find out if a linked-list has an end? (i.e. the list is not a cycle)
- How can you tell what shell you are running on UNIX system?
- What is Boyce Codd Normal form?
- What is pure virtual function?
- Write a Struct Time where integer m, h, s are its members
- How do you traverse a Btree in Backward in-order?
- What is the two main roles of Operating System?
- In the derived class, which data member of the base class are visible?
- How do you find out if a linked-list has an end? (i.e. the list is not a cycle)
- What is the difference between realloc() and free()?
- What is function overloading and operator overloading?
- What is the difference between declaration and definition?
- What are the advantages of inheritance?
- How do you write a function that can reverse a linked-list?
- What do you mean by inline function?
- Write a program that ask for user input from 5 to 9 then calculate the average
- Write a short code using C++ to print out all odd number from 1 to 100 using a for loop
- What is public, protected, private?
- Write a function that swaps the values of two integers, using int* as the argument type.
- Tell how to check whether a linked list is circular.
- What is polymorphism?
- What is virtual constructors/destructors?
- What is the difference between an ARRAY and a LIST?
- Does c++ support multilevel and multiple inheritance?
- What is a template?
- Define a constructor - What it is and how it might be called (2 methods).
- You have two pairs: new() and delete() and another pair : alloc() and free(). Explain differences between eg. new() and malloc()
- What is the difference between class and structure?
- What is RTTI?
- What is encapsulation?
- Explain term POLIMORPHISM and give an example using eg. SHAPE object: If I have a base class SHAPE, how would I define DRAW methods for two objects CIRCLE and SQUARE
- What is an object?
- How can you tell what shell you are running on UNIX system?
- Describe PRIVATE, PROTECTED and PUBLIC – the differences and give examples.
- What is namespace?
- What do you mean by inheritance?
- What is a COPY CONSTRUCTOR and when is it called?
- What is Boyce Codd Normal form?
- What is virtual class and friend class?
- What is the word you will use when defining a function in base class to allow this function to be a polimorphic function?
- What do you mean by binding of data and functions?
- What are 2 ways of exporting a function from a DLL?
- What is the difference between an object and a class?
- Suppose that data is an array of 1000 integers. Write a single function call that will sort the 100 elements data [222] through data [321].
- What is a class?
- What is friend function?
- Which recursive sorting technique always makes recursive calls to sort subarrays that are about half size of the original array?
- What is abstraction?
- What are virtual functions?
- What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator.
- What is a scope resolution operator?
- What do you mean by pure virtual functions?
- What is polymorphism? Explain with an example?
- How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Why is it difficult to store linked list in an array? How can you find the nodes with repetetive data in a linked list?
- What’s the output of the following program? Why?
- Why are arrays usually processed with for loop?
- What is an HTML tag?
- Explain which of the following declarations will compile and what will be constant ...
- You’re given a simple code for the class BankCustomer. Write the following functions ...
- What problems might the following macro bring to the application?
- Anything wrong with this code? T *p = new T[10]; delete p;
- Anything wrong with this code? T *p = 0; delete p;
- How do you decide which integer type to use?
- What’s the best way to declare and define global variables?
- What does extern mean in a function declaration?
- What can I safely assume about the initial values of variables which are not explicitly initialized?
- What is the difference between char a[] = “string”; and char *p = “string”;?
- What’s the auto keyword good for?
- What is the difference between char a[] = “string”; and char *p = “string”; ?
- How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
- What does extern mean in a function declaration?
- How do I initialize a pointer to a function?
- How do you link a C++ program to C functions?
- Explain the scope resolution operator.
- What are the differences between a C++ struct and C++ class?
- How many ways are there to initialize an int with a constant?
- I write a program in C++ that asks the user for names of students and their cooresponding midterm scores ...
- How does throwing and catching exceptions differ from using setjmp and longjmp?
- What is a default constructor?
- What is a conversion constructor?
- What is the difference between a copy constructor and an overloaded assignment operator?
- When should you use multiple inheritance?
- Explain the ISA and HASA class relationships. How would you implement each in a class design?
- When is a template a better solution than a base class?
- What is a mutable member?
- What is an explicit constructor?
- What is the Standard Template Library (STL)?
- Describe run-time type identification.
- What problem does the namespace feature solve?
- Are there any new intrinsic (built-in) data types?
- Will the following program execute?
- Will it execute or not?
- What is the difference between Mutex and Binary semaphore?
- In C++, what is the difference between method overloading and method overriding?
- What methods can be overridden in Java?
- What are the defining traits of an object-oriented language?
- Write a program that ask for user input from 5 to 9 then calculate the average
- Can you be bale to identify between Straight- through and Cross- over cable wiring? and in what case do you use Straight- through and Cross-over?
- If you hear the CPU fan is running and the monitor power is still on, but you did not see any thing show up in the monitor screen. What would you do to find out what is going wrong?
- Assignment Operator - What is the diffrence between a "assignment operator" and a "copy constructor"?
- "mutable" Keyword - What is "mutable"?
- RTTI - What is RTTI?
- STL Containers - What are the types of STL containers?
- Virtual Destructor - What is the need for “Virtual Destructor”?
- Differences of C and C++ Could you write a small program that will compile in “C” but not in “C++”?
- What is output equal to in Bitwise Operations - ...
|