html5-img
1 / 77

Pointer

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 and also has a type which designates what kind of variables its contents refer to. type* name type *name. Example 26.

vern
Download Presentation

Pointer

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 and also has a type which designates what kind of variables its contents refer to. type* name type *name

  2. Example 26 #include <iostream.h> int main() { long *pointer=NULL; long x=10, y=20; pointer=&x; *pointer +=5; cout <<"x = " <<x <<"\t &x= " <<hex <<pointer; cout <<endl; pointer = &y; x=*pointer*10; cout <<endl <<"x= "<<dec <<x <<"\t pointer = " <<hex <<pointer <<"\t *pointer = " <<dec <<*pointer; cout <<endl; return 0; }

  3. Pointer • Pointer is a variable that holds a memory address. • Address of a variable is available by using the address - of operator (&).

  4. Example 27 #include <iostream.h> int main() { unsigned short x=8; unsigned long y=56432; signed long z=-76543; cout <<"x:\t" <<x <<"\t Address of x: \t" <<&x <<"\n"; cout <<"y:\t" <<y <<"\t Address of y: \t" <<&y <<"\n"; cout <<"z:\t" <<z <<"\t Address of z: \t" <<&z <<"\n"; return 0; }

  5. Example 28 #include <iostream.h> int main() { unsigned short int x=8; int y=16; unsigned short int *pointerx=0; pointerx=&x; cout <<"x=" <<x <<"\t pointerx=" <<pointerx; cout <<endl; int *pointery=&y; cout <<"y=" <<y <<"\t pointery=" <<pointery; cout <<endl; return 0; }

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

  7. Example 29 #include <iostream.h> int main() { unsigned short int x=8; int y=16, z; unsigned short int *pointerx=0; pointerx=&x; cout <<"x = " <<x <<"\t pointerx = " <<pointerx; cout <<endl; int *pointery=&y; cout <<"y = " <<y <<"\t pointery = " <<pointery; cout <<endl; z=*pointerx + *pointery; cout <<"z = x + y = " <<z ; cout <<endl <<endl; return 0; }

  8. Debugging • When you have written a program that doesn’t 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 it’s going wrong.

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

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

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

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

  13. GO • The 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.

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

  15. Attach to Process • Enables 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.

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

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

  18. Variable Window • Variable window has three tabs • Auto • Locals • this

  19. Auto, Local and this Tabs • Auto 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.

  20. Viewing Variable in the Edit Window • Position 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.

  21. Watching Variables’ Values • Position 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.

  22. Watching Variables’ Values • Variable 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 window • highlight and drag a variable from the Text Editor or Variables window

  23. Window Programming • Microsoft Foundation Classes (MFC). • Single Document Interface (SDI). • Multiple Document Interface (MDI). • AppWizard.

  24. Window Programming With C++ • AppWizard • for creating the basic program code. • ClassWizard • for extending and customizing the classes in programs. • Resource Editor • for editing or creating such things as menus and toolbars.

  25. Classes • A 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.

  26. Classes • A 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.

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

  28. Access Control in a Class • Public • means members of an object of the class can be accessed anywhere within the scope of the class object. • Private • can only be accessed by member functions of a class.

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

  30. Example 30 #include <iostream.h> 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 <<endl <<"Volume of Box # 1 is = " <<volume1 <<endl; volume2=box2.height*box2.length*box2.width; cout <<endl <<"Volume of Box # 2 is = " <<volume2 <<endl <<endl; return 0; }

  31. Example 31 #include <iostream.h> 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 <<endl <<"Volume of Box # 1 is = " <<volume1 <<endl; cout <<endl <<"Volume of Box # 2 is = " <<volume2 <<endl <<endl; return 0; }

  32. Example 32 #include <iostream.h> 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 <<endl <<"Volume of Box # 1 is = " <<volume1 <<endl; cout <<endl <<"Volume of Box # 2 is = " <<volume2 <<endl <<endl; return 0; }

  33. Example 33 #include <iostream.h> 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 <<endl <<"Volume of Box # 1 is = " <<volume1 <<endl; cout <<endl <<"Volume of Box # 2 is = " <<volume2 <<endl <<endl; return 0; }

  34. Example 34 #include <iostream.h> 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 <<endl <<"Volume of Box # 1 is = " <<box1.volume() <<endl; cout <<endl <<"Volume of Box # 2 is = " <<box2.volume() <<endl <<endl; return 0; }

  35. Example 35 #include <iostream.h> 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 <<endl <<"Volume of Box # 1 is = " <<box1.volume() <<endl; cout <<endl <<"Volume of Box # 2 is = " <<box2.volume() <<endl <<endl; return 0; }

  36. Example 36 #include <iostream.h> 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; };

  37. Example 36 (continue) int main(void) { box box1(10, 12, 8), box2(6,8,9); //box2.width=12; cout <<endl <<"Volume of Box # 1 is = " <<box1.volume() <<endl; cout <<endl <<"Volume of Box # 2 is = " <<box2.volume() <<endl <<endl; return 0; } If we uncomment the box2.width=12 what will happen? Why?

  38. friend Functions • friend 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.

  39. Example 37 #include <iostream.h> 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); };

  40. 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 <<endl <<"Volume of Box # 1 is = " <<box1.volume() <<endl; cout <<endl <<"Surface area of Box # 1 is = " <<boxsurfacearea(box1) <<endl; cout <<endl <<"Volume of Box # 2 is = " <<box2.volume() <<endl; cout <<endl <<"Surface area of Box # 2 is = " <<boxsurfacearea(box2) <<endl <<endl; return 0; }

  41. Example 38 #include <iostream.h> 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; };

  42. Example 38 (continue) int main(void) { int x=0, y=0, z=0; cout <<"length of box # 2 is = "; cin >>x; cout <<endl <<"height of box # 2 is = "; cin >>y; cout <<endl <<"width of box # 2 is = "; cin >>z; box box1(10, 12, 8), box2(x,y,z); if(box2.compare(box1)) cout <<endl <<"Box # 1 is smaller than Box # 2"; else cout <<endl <<"Box # 1 is equal to or larger than Box # 2"; cout <<endl <<endl; return 0; }

  43. Exercises • Write the code that declares a class called employee with these data members: • age • sex • years of service • salary

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

  45. Essential Pieces of a Window Program • WinMain ( ) • 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 application’s window. (procedure for each window class you’ve defined)

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

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

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

  49. MFC • MFC 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.

  50. MFC • All the classes in MFC have names beginning with with C. • Cview • Cdocument • Data member of an MFC class are prefixed with m_ . • m_lpCmdLine

More Related