460 likes | 564 Views
Visual C++ Dr. K. Shahrabi. KEAN UNIVERSITY. Developer studio. Is a self-contain environment for creating, compiling, linking and testing windows program. Developer studio incorporate a range of fully integrated tools designed to make the whole process of writing windows programs easy.
E N D
Visual C++ Dr. K. Shahrabi KEAN UNIVERSITY
Developer studio • Is a self-contain environment for creating, compiling, linking and testing windows program. • Developer studio incorporate a range of fully integrated tools designed to make the whole process of writing windows programs easy.
System Components • Editor • Compiler • Linker • Libraries • AppWizard • ClassWizard
Editor • Provides an interactive environment for creating and editing C++ source code. • Cut • Paste • Color cues to differentiate between various language elements • The editor automatically recognize fundamental words in the C++ and assigns a color to them according to what they are.
Compiler • Converts the source code into machine language. • Detects and reports error in the compilation process. • Invalid program code. • Unrecognized program code. • Structural errors • part of program can never be executed
Compiler Output • Output from the compiler is known as object code. • Output is stored in a file called object file, which usually have name with extension .obj
Linker • Linker combines the various modules generated by the compiler from source code file. • Adds required code modules from program libraries supplied as part of C++, and welds everything into an executable whole. • It can detect and report errors. • Missing part of program • Non-existent libraries
Libraries • Support and extends the C++ language by providing routines to carry out operations which are not part of the language. • Calculating square root • Calculating sine
Types of Libraries • Standard library • Microsoft Foundation Class (MFC) library
Standard library • Contains routines that are not platform specific. • Common to all C++ compilers. • The extension to standard set are not universal.
MFC Library • A set of structured components that provides the basis for all the windows program. • MFC is referred to as an application framework
AppWizard • AppWizard automatically generates a basic framework for windows program.
ClassWizard • Provides an easy mean of extending the classes generated by AppWizard as part of your basic windows program. • It help to add new classes, based on classes in the MFC, to support the functionality you want to include in your program.
Project • A program of some kind. • A console program. • A program that have none of the baggage required for windows program (character-based DOS).
Project Workspace • A folder in which all the information relating to a project is stored. • When you create a project, a project workspace is generated automatically . • Developer Studio will maintain all of the source code and other files in the project workspace.
Project Workspace • Contain other folders to store the output from compiling and linking the project. • Further project can be added to the same project workspace (subprojects). • Any project can be a subproject of another (not recommended)
Defining a Project • Project name. • List of all the source files. • Definition of what sort of program is to be built from the source files. • .exe program • console program
Defining a Project • Option set for the editor, the compiler, the linker and other components of Visual C++. • Windows to be displayed in Developer Studio when the project is opened.
Defining a Project • Project are stored in a file with the extension .dsp or .mak.
Files Type • .dsp or .mak • .opt • .dsw • .exe • .obj • .ilk • .pch • .pdb
Files Type • .dsp or .mak • Contains information about how your program is to be created from the files in the project workspace and is produced when you create project workspace. • .opt • Contains setting for the project workspace.
Files Type • .dsw • Is used to store further information about the workspace, such as what project it contains. • .exe • Is the executable file for program. This file can be obtained if both compile and link steps are successful.
Files Type • .obj • Object file and it is created by compiler. This file containing machine code from your program source files. • These are used by linker, along with files from the libraries, to produce your .exe file
Files Type • .ilk • It enable the linker to incrementally link the object files produced from modified source code into the existing .exe file • .pch • pre-compiled header
Files Type • .pdb • This file contains debugging information that is used when you execute a program in debug mode
Step one • First step in writing a Visual C++ program is to create a project. • Select File from the main menu option • Select New from the File menu option • Select project • Define project type • File name
Step two • Select File from main menu option • Select New from File menu option • Select C++ source code • Start typing your program
Step Three • Select File from main menu option • Select save from File menu option • Type a file name
Step Four • Click right mouse key • Select Add to Project
Step Five • Click Build Button from main menu • Run your program
Program • Type the following program # include <iostream.h> int main(void) { cout <<“any text” <<endl <<“any text” <<endl <<endl; return 0; }
Project Folder • Look in project folder for subfolder called Debug. • This folder must contains the output of the build that you did.
Project Folder • Click Start • Click Programs • Click Windows Explorer • Select Program Files • Select DevStudio • Select MyProjects • Select Project Name • Select Debug
Project Folder • This folder must contain the following: *.exe *.ilk *.obj *.pch *.pdb vc50.idb vc50.pdb
Executing Program • There are different ways for executing a program. 1. From Explorer 2. From C++ development environment a. From main menu b. From toolbar
Executing Program • Double-click the .exe file from Explorer. OR • Select Build from main menu • Select Execute ‘file name’.exe from Build menu. OR • Click on toolbar button for this menu item (!).
Structure of a C++ Program • One or more functions. A function is a self-contained block of code with a unique name. main( )
Structure of a C++ Program void input_name ( ) { //… return ; } Execution start with main ( ) int main ( ) { input_name ( ); sort_name ( ); output_name ( ); return 0; } void sort_name ( ) { //… return ; } void output_name ( ) { //… return ; } Goes back to operating system
Structure of a C++ Program • // comment • # compiler look for first • include compiler will add to main • iostream.h header file • int integer • endl end line & start on new line • cout send to display
Example1 • Add to integer variables and display the result. //example1 #include <iostream.h> int main() { int A, B, C; //Declare integer variables A=10; B=20; //Set initial values C=A+B; //Add A and B cout <<endl; //Start a new line cout <<"A+B=" <<C; //Display cout <<endl<<endl; return 0; }
Example 2 //example2 #include <iostream.h> int Add (int x, int y) { return (x+y); } main() { int a, b, c; //Declare integer variables cout << "Enter two numbers:"<<endl; cout <<"a="; cin >> a; //Enter a value for a cout<<"b="; cin >> b; //Enter a value for b c=Add(a,b); //Call Add ( ) cout <<endl; //Start a new line cout <<"a+b=" <<c; //Display cout <<endl<<endl; return 0; }
Example3 //example3 #include <iostream.h> int Add (int x, int y) { return (x+y); } int Sub (int x, int y) { return (x-y); } main() { int a, b, c, d; //Declare integer variables cout << "Enter two numbers:"<<endl; cout <<"a="; cin >> a; //Enter a value for a cout<<"b="; cin >> b; //Enter a value for b c=Sub(a,b); //Call Sub ( ) d=Add(a,b); //Call Add ( ) cout <<endl; //Start a new line cout <<"a-b=" <<c; //Display cout <<endl; cout <<"a+b=" <<d; //Display cout <<endl<<endl; return 0; }
Variable Type • unsigned short int 2 bytes 0 to 65535 • short int 2 bytes -32768 to 32768 • unsigned long int 4 bytes 0 to 4294967295 • long int 4 bytes -2147483648 to 2147483647 • int 2 bytes -32768 to 32768 • unsigned int 2 bytes 0 to 65535
Variable Type • Char 1 byte 256 character • float 4 bytes 1.2 e-38 to 3.4 e38 • double 8 bytes 2.2 e-308 to 1.8 e308
Keywords • C++ reserved words (keywords). Keywords can not be used as name in program. asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof
Keywords static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, union, unsigned, using, virtual, void, volatile, wchar_t, while,