Pointer A pointer is a variable that stores
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 1
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; } 2
Pointer • Pointer is a variable that holds a memory address. • Address of a variable is available by using the address - of operator (&). 3
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; } 4
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; } 5
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. 6
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; return 0; } 7
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. 8
To Display Debug Toolbar • • • Select Tools from main menu. Select Customize from Tool menu. Select Toolbars tab. Check the box against Build. Uncheck Build Mini. Bar if it is checked. 9
Setting Breakpoint • Place the cursor in the statement where you want execution to stop and click the Insert/Remove Breakpoint button or press F 9. 10
Removing Breakpoint • Place the cursor in the same line as an existing breakpoint and click the Insert/Remove Breakpoint button or press F 9. 11
Starting Debugging • Select Build from main menu. • Select Start Debug from Build menu. • Select one of the four way listed in Start Debug submenu. 12
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. 13
Run to Cursor • It executes the program up to the statement where you left the cursor in the text editor window. 14
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. 15
Step Into • Execute your program one statement at a time. • It would also execute all the code for stream output (#include). 16
Step Over • Execute the statements in function main( ) and jump over all the code used by the stream operations without stopping. 17
Variable Window • Variable window has three tabs – Auto – Locals – this 18
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 objectoriented programming. 19
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. 20
Watching Variables’ Values • Position the cursor in the Text Editor window in the middle of the variable name. • Select Quick. Watch from the Debug menu. – If you had any problem the whole name or expression and then select Quick. Watch. 21
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 22
Window Programming • • Microsoft Foundation Classes (MFC). Single Document Interface (SDI). Multiple Document Interface (MDI). App. Wizard. 23
Window Programming With C++ • App. Wizard – for creating the basic program code. • Class. Wizard – for extending and customizing the classes in programs. • Resource Editor – for editing or creating such things as menus and toolbars. 24
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. 25
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. 26
Declaring a Class • Use the class keyword followed by { and then list the data members and function members of that class car { int door ; int seats ; speed ( ); }; 27
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. 28
Access Control in a Class • If you omit the access specification altogether, the members have the default attribute, private. 29
Example 30 #include <iostream. h> class box { public: int length, height, width; }; int main(void) { box 1, box 2; int volume 1=0, volume 2=0; box 1. length=10, box 1. height=12, box 1. width=8; box 2. length=14, box 2. height=13, box 2. width=9; volume 1=box 1. height*box 1. length*box 1. width; cout <<endl <<"Volume of Box # 1 is = " <<volume 1 <<endl; volume 2=box 2. height*box 2. length*box 2. width; cout <<endl <<"Volume of Box # 2 is = " <<volume 2 <<endl; return 0; } 30
Example 31 #include <iostream. h> class box { public: int length, height, width; int volume(void) { return length*height*width; } }; int main(void) { box 1, box 2; int volume 1=0, volume 2=0; box 1. length=10, box 1. height=12, box 1. width=8; box 2. length=14, box 2. height=13, box 2. width=9; volume 1=box 1. volume(), volume 2=box 2. volume(); cout <<endl <<"Volume of Box # 1 is = " <<volume 1 <<endl; cout <<endl <<"Volume of Box # 2 is = " <<volume 2 <<endl; return 0; } 31
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 1, box 2; int volume 1=0, volume 2=0; box 1. length=10, box 1. height=12, box 1. width=8; box 2. length=14, box 2. height=13, box 2. width=9; volume 1=box 1. volume(), volume 2=box 2. volume(); cout <<endl <<"Volume of Box # 1 is = " <<volume 1 <<endl; cout <<endl <<"Volume of Box # 2 is = " <<volume 2 <<endl; return 0; } 32
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 1, box 2; int volume 1=0, volume 2=0; box 1. length=10, box 1. height=12, box 1. width=8; box 2. length=14, box 2. height=13, box 2. width=9; volume 1=box 1. volume(), volume 2=box 2. volume(); cout <<endl <<"Volume of Box # 1 is = " <<volume 1 <<endl; cout <<endl <<"Volume of Box # 2 is = " <<volume 2 <<endl; return 0; } 33
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 1(10, 12, 8), box 2(14, 13, 9); cout <<endl <<"Volume of Box # 1 is = " <<box 1. volume() <<endl; cout <<endl <<"Volume of Box # 2 is = " <<box 2. volume() <<endl; return 0; } 34
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 1(10, 12, 8), box 2(14, 13, 9); cout <<endl <<"Volume of Box # 1 is = " <<box 1. volume() <<endl; cout <<endl <<"Volume of Box # 2 is = " <<box 2. volume() <<endl; return 0; } 35
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; }; 36
Example 36 (continue) int main(void) { box 1(10, 12, 8), box 2(6, 8, 9); //box 2. width=12; cout <<endl <<"Volume of Box # 1 is = " <<box 1. volume() <<endl; cout <<endl <<"Volume of Box # 2 is = " <<box 2. volume() <<endl; return 0; } If we uncomment the box 2. width=12 what will happen? Why? 37
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. 38
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); }; 39
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 1(10, 12, 8), box 2(6, 8, 9); cout <<endl <<"Volume of Box # 1 is = " <<box 1. volume() <<endl; cout <<endl <<"Surface area of Box # 1 is = " <<boxsurfacearea(box 1) <<endl; cout <<endl <<"Volume of Box # 2 is = " <<box 2. volume() <<endl; cout <<endl <<"Surface area of Box # 2 is = " <<boxsurfacearea(box 2) <<endl; return 0; } 40
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; }; 41
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 1(10, 12, 8), box 2(x, y, z); if(box 2. compare(box 1)) cout <<endl <<"Box # 1 is smaller than Box # 2"; else cout <<endl <<"Box # 1 is equal to or larger than Box # 2"; cout <<endl; return 0; } 42
Exercises • Write the code that declares a class called employee with these data members: – age – sex – years of service – salary 43
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. 44
Essential Pieces of a Window Program • Win. Main ( ) – which is called by windows at the start of execution of the program. • Wnd. Proc ( ) or Window. Proc ( ) – 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) 45
Win. Main ( ) • The function Win. Main ( ) does any initialization and sets up the window or windows that will be the primary interface to the user. 46
Window. Proc ( ) • The function Window. Proc ( ) handles all the messages that aren’t queued, which will include those initiated in the message loop in Win. Main ( ). 47
Window. Proc ( ) • Window. Proc ( ) 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. 48
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. 49
MFC • All the classes in MFC have names beginning with C. – Cview – Cdocument • Data member of an MFC class are prefixed with m_. – m_lp. Cmd. Line 50
MFC • MFC uses other prefix such as p for pointer, n for an int, l for long, h for a handle. – m_lp. Cmd. Line data member of class that is of type ‘long pointer’ 51
Other MFC Prefix • • b by c dw fn i s w a logical variable of type BOOL type unsigned char; a byte type char type DWORD a function int string WORD 52
Create MFC Application • Step one – Select File from main menu. – Select New from File menu. – Select Win 32 Application (under Project Tab). – Type project name. – Click the ok button. 53
Create MFC Application • Step two – Select Project from main menu. – Select Settings from Project menu. – Select General tab from Project Settings dialog. – Select MFC option (MFC in a Shared DLL). 54
Create MFC Application • Step three – Create a new source file. – Type your program. – Save. – Build. – Execute. 55
To begin programming • Include the header file afxwin. h – This contains the definitions for many MFC classes. • To produce the complete program, we’ll only need to drive two classes from MFC: – application class – window class 56
To begin programming • We won’t need to write a Win. Main ( ) function because this is automatically provided by the MFC library. 57
The Application Class • CWin. App is fundamental to any Windows program written using MFC. • An object of this class includes everything necessary for starting, initializing, running and closing the application. 58
The Application Class • m_n. Cmd. Show – specifies how the application window is to be shown when the application starts up. • m_psz. App. Name – stores a pointer to a string that defines the name of the application. 59
The Application Class (example) class our. App: public CWin. App { public: virtual BOOL Init. Instance ( ); }; 60
The Window Class • FMC application needs a window as the interface to the user, referred to as a frame window. CFrame. Wnd is designed for this purpose. • CFrame. Wnd provides everything for creating and managing a window for an application, all we need to add to our derived window class is a constructor. 61
The Window Class (example) class our. Wnd: public CFrame. Wnd { public: our. Wnd ( ) { Create (0, “My first window”); } }; 62
Example 39 # include<afxwin. h> class Our. App: public CWin. App { public: virtual BOOL Init. Instance(); }; class Our. Wnd: public CFrame. Wnd { public: Our. Wnd() { Create(0, "My first window"); } }; BOOL Our. App: : Init. Instance(void) { m_p. Main. Wnd = new Our. Wnd; m_p. Main. Wnd -> Show. Window(m_n. Cmd. Show); return TRUE; } Our. App An. Application; 63
Create an SDI Application With App. Wizard • Select File from main menu. • Select New from File menu. • Select MFC App. Wizard(exe) (under project tab). • Type project name. • Click on ok button. 64
Create an SDI Application With App. Wizard • Select the SDI option. • Select the language you like your resources in. • Click on Next> button once again. 65
Create an SDI Application With App. Wizard • This step gives you a range of choices relating to Object Linking and Embedding (OLE), under the label of compound document support. • OLE enables you to write programs that can import objects from other programs. • OLE enables you to import your program into another application. 66
Create an SDI Application With App. Wizard • The default setting at for this step is Active. X Controls. – This means that App. Wizard will include code that allows the possibility of using Active. X controls in our application. • Accept the default set choice. • Move to next step. 67
Create an SDI Application With App. Wizard • This step offers you a range of functions that can be included in your application by App. Wizard. – Docking toolbar – Initial status bar – Printing and print preview – Context-sensitive Help – 3 D control 68
Create an SDI Application With App. Wizard • Docking toolbar – provides a standard range of buttons that are alternatives to using menu option. • Initial status bar – status bar appears at the bottom of the application window. (Num Lock, Caps Lock, Scroll Lock) 69
Create an SDI Application With App. Wizard • Printing and print preview – This add the standard Page Setup, Print Preview and Print option to the File menu. • Context-sensitive Help – Enables a basic set of facilities to support context-sensitive help. • 3 D control – Gives a 3 D appearance. 70
Create an SDI Application With App. Wizard • The second group of choice concern Window Open Services Architecture (WOSA). • This is for implementing communication with other computer. This provide two options for communication: – MAPI – Windows Sockets 71
Create an SDI Application With App. Wizard • MAPI – This option allows you to send and receive messages. • Windows Sockets – This allows you to implement TCP/IP capability within your program. 72
Create an SDI Application With App. Wizard • Accepts all the default at this point. • Select the Advanced button. – This brings up a range of options for your application grouped under two tabs. • Document Template Strings • Window Styles 73
Create an SDI Application With App. Wizard • Document Template Strings – allows to choose the file extension which will identify files that are generated by your application. – Allows you to modify the File Type ID • File Type ID is used to label the file type in the system registry. – Doc type name: entry is a default name for a document. 74
Create an SDI Application With App. Wizard • Type file extension. • Click on Window Styles tab. – Main frame styles area enable you to tailor your application window. – Select Thick frame, Minimize box, Maximize box, and System menu. • Click on Close button to exit from Advanced option. 75
Create an SDI Application With App. Wizard • Click on Next >. • This step offers two options – whether or not comments are to be included in the source code generated by App. Wizard. – Relates how MFC library code is used in your program. • Default setting is shared DLL (Dynamic Link Library) means that your program will link to MFC library routines at run time. 76
Create an SDI Application With App. Wizard • Accept the default setting. • Click on Next >. – This presents you with a list of the classes that App. Wizard will be generating in your program code. • Click on Finish. • Click on ok 77
- Slides: 77