1 pointer a pointer is a variable that stores an address of another variable of a particular type. a...

Download 1 Pointer A pointer is a variable that stores an address of another variable of a particular type. A pointer has a variable name just like any other variable

Post on 02-Jan-2016

213 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • PointerA pointer is a variable that stores an address of another variable of a particular type. A pointer has a variable name just like any other variable and also has a type which designates what kind of variables its contents refer to.type* nametype *name

  • Example 26#include int main(){long *pointer=NULL;long x=10, y=20;pointer=&x;*pointer +=5;cout
  • PointerPointer is a variable that holds a memory address.

    Address of a variable is available by using the address - of operator (&).

  • Example 27#include int main(){unsigned short x=8;unsigned long y=56432;signed long z=-76543;cout
  • Example 28#include int main(){unsigned short int x=8;int y=16;unsigned short int *pointerx=0;pointerx=&x;cout
  • Indirection Operator The indirection operator (*) is called the dereference operator. The indirection operator mean the value stored at.When a pointer is dereferenced, the value at the address stored by pointer is retrieved.

  • Example 29#include int main(){unsigned short int x=8;int y=16, z;unsigned short int *pointerx=0;pointerx=&x;cout
  • DebuggingWhen you have written a program that doesnt work as it should, the debug facilities enable you to work through a program one step at a time to find out where and how its going wrong.

  • To Display Debug ToolbarSelect Tools from main menu.Select Customize from Tool menu.Select Toolbars tab.Check the box against Build.Uncheck Build MiniBar if it is checked.

  • Setting BreakpointPlace the cursor in the statement where you want execution to stop and click the Insert/Remove Breakpoint button or press F9.

  • Removing BreakpointPlace the cursor in the same line as an existing breakpoint and click the Insert/Remove Breakpoint button or press F9.

  • Starting DebuggingSelect Build from main menu.Select Start Debug from Build menu.Select one of the four way listed in Start Debug submenu.

  • GOThe Go option executes a program to the first breakpoint, where execution will halt.After you have examined all you need to at a breakpoint, selecting Go again will continue execution up to the next breakpoint.

  • Run to CursorIt executes the program up to the statement where you left the cursor in the text editor window.

  • Attach to ProcessEnables you to debug a program that is already running.This option will list the processes that are running on system.You can select the process you want to debug.

  • Step IntoExecute your program one statement at a time.It would also execute all the code for stream output (#include).

  • Step OverExecute the statements in function main( ) and jump over all the code used by the stream operations without stopping.

  • Variable WindowVariable window has three tabsAutoLocalsthis

  • Auto, Local and this TabsAuto tab shows the variables in use in the current and previous statements.Local tab shows the values of the variable local to the current function.this tab is useful as we progress into object-oriented programming.

  • Viewing Variable in the Edit WindowPosition the cursor over the variable for a second.A tooltip will pop up showing the current value of the variable.You can highlight a section and rest the cursor over the highlighted area and the tooltip will display the value.

  • Watching Variables ValuesPosition the cursor in the Text Editor window in the middle of the variable name.Select QuickWatch from the Debug menu.If you had any problem the whole name or expression and then select QuickWatch.

  • Watching Variables ValuesVariable or expressions can be added to Watch window in two other ways:type the name of the variable into the Name field in the Watch windowhighlight and drag a variable from the Text Editor or Variables window

  • Window ProgrammingMicrosoft Foundation Classes (MFC).Single Document Interface (SDI).Multiple Document Interface (MDI).AppWizard.

  • Window Programming With C++AppWizardfor creating the basic program code.ClassWizardfor extending and customizing the classes in programs.Resource Editorfor editing or creating such things as menus and toolbars.

  • ClassesA class is a collection of variables often of different types combined with a set of related function.A class enables you to bundle various parts and various functions into a single collection called object.

  • ClassesA class can consist of any combination of the variable types and other class types.The variables in the class are called the member variable or data member.Function in the class typically manipulate the member variables -- that is, what the objects of a class can do.

  • Declaring a ClassUse the class keyword followed by { and then list the data members and function members of that class.class car{int door ;int seats ;speed ( );};

  • Access Control in a ClassPublicmeans members of an object of the class can be accessed anywhere within the scope of the class object.Privatecan only be accessed by member functions of a class.

  • Access Control in a ClassIf you omit the access specification altogether, the members have the default attribute, private.

  • Example 30#include class box{public:int length,height, width;};int main(void){box box1, box2;int volume1=0, volume2=0;box1.length=10, box1.height=12, box1.width=8;box2.length=14, box2.height=13, box2.width=9;volume1=box1.height*box1.length*box1.width;cout
  • Example 31#include class box{public:int length,height, width;int volume(void){ return length*height*width;}};int main(void){box box1, box2;int volume1=0, volume2=0;box1.length=10, box1.height=12, box1.width=8;box2.length=14, box2.height=13, box2.width=9;volume1=box1.volume(), volume2=box2.volume();cout
  • Example 32#include class box{public:int length,height, width;int volume(void);};int box::volume(void){return length*height*width;}int main(void){box box1, box2;int volume1=0, volume2=0;box1.length=10, box1.height=12, box1.width=8;box2.length=14, box2.height=13, box2.width=9;volume1=box1.volume(), volume2=box2.volume();cout
  • Example 33#include class box{public:int length,height, width;int volume(void);};inline int box::volume(void){return length*height*width;}int main(void){box box1, box2;int volume1=0, volume2=0;box1.length=10, box1.height=12, box1.width=8;box2.length=14, box2.height=13, box2.width=9;volume1=box1.volume(), volume2=box2.volume();cout
  • Example 34#include class box{public:int length,height, width;box(int l, int h, int w){length=l, height=h, width=w;}int volume(void){return length*height*width;}};int main(void){box box1(10, 12, 8), box2(14, 13, 9);cout
  • Example 35#include class box{public:int length,height, width;box(int l=0, int h=0, int w=0){length=l, height=h, width=w;}int volume(void){return length*height*width;}};int main(void){box box1(10, 12, 8), box2(14, 13, 9);cout
  • Example 36#include class box{public:box(int l=0, int h=0, int w=0){length=l, height=h, width=w;}int volume(void){return length*height*width;}private:int length, height, width;};

  • Example 36 (continue)int main(void){box box1(10, 12, 8), box2(6,8,9);//box2.width=12;cout
  • friend Functionsfriend functions are selected functions which are not member of a class to, nonetheless, be able access all members of a class.friend functions are not members of the class, and therefore the access attributes do not apply to them.friend are just ordinary global functions with special privileges.

  • Example 37#include class box{public:box(int l=1, int h=1, int w=1){length=l, height=h, width=w;}int volume(void){return length*height*width;}private:int length, height, width;friend int boxsurfacearea(box area);};

  • Example 37 (continue)int boxsurfacearea(box area) { return 2*(area.length*area.width+area.length*area.height+area.height*area.width); }int main(void){box box1(10, 12, 8), box2(6,8,9);cout
  • Example 38#include class box{public:box(int l=1, int h=1, int w=1){length=l, height=h, width=w;}int volume(void){return length*height*width;}int compare(box xbox){return this-> volume()>xbox.volume();}private:int length, height, width;};

  • Example 38 (continue)int main(void){int x=0, y=0, z=0;cout x;cout
  • ExercisesWrite the code that declares a class called employee with these data members:agesexyears of servicesalary

  • ExercisesWrite a program with the employee class that creates ten employees; sets their age, sex, years of service, and salary; and prints their values.

  • Essential Pieces of a Window ProgramWinMain ( )which is called by windows at the start of execution of the program.WndProc ( ) or WindowProc ( )which will be called by the operating system whenever a message is to be passed to your applications window. (procedure for each window class youve defined)

  • WinMain ( )The function WinMain ( ) does any initialization and sets up the window or windows that will be the primary interface to the user.

  • WindowProc ( )The function WindowProc ( ) handles all the messages that arent queued, which will include those initiated in the message loop in WinMain ( ).

  • WindowProc ( )WindowProc ( ) is where you code your application-specific response to each window message which should handle all the communications with the user by processing the windows message generated by user action.

  • MFCMFC are a set of predefined classes upon which windows programming with Visual C++ is built.

    MFC represent an object - oriented approach to windows programming that encapsulates the windows API.

  • MFCAll the classes in MFC have names beginning with with C.CviewCdocumentData member of an MFC class are prefixed with m_ .m_lpCmdLine

Recommended

View more >