230 likes | 235 Views
Learn how to edit, compile, and run programs under MS-Windows using Visual C++. Practice basic editing, cutting and pasting, and file saving. Includes an overview of the C++ program structure and the use of built-in functions.
E N D
Lab Session-I (A and B)CS-120 Fall 2000 • Using Windows • Using An Editor • Using Visual C++ • Using Compiler • Writing and Running Programs • Lab-1 DUE Fri September 8 • Lab-1 continues (Session I-B)
Lab Targets • We wish to learn how to program • We would like to edit a program, compile it and run it successfully • Therefore we need to learn editing, compiling and running programs • This can be done under MS-Windows operating system easily
Using Windows • MS-Windows is a user friendly system • Mouse clicks in GUI environment make it easy to use the system. • Switching from one task to another is done through various ways • Cutting and pasting from one application to another is convenient way to share data
Using an Editor • First, we should learn basic editing under Windows operating system • We need to know how to correct a mistake, cut and paste text and save a file • Let us do some practice sessions as prescribed in the manual. Please do experiment 1.1 in lab session 1
Suggested Editors • Under Ms-Windows, Wordpad and Notepad are available as editors that can handle text files • Wordpad is better than Notepad as it remembers previously edited files • Please save the file as “Text file--MS-DOS format” if you wish to give it an extension of .cpp (C++ source code file) or save as “all files” under Notepad
Using Visual C++ • Visual C++ is part of MS Visual Studio package • Visual C++ is easy to use because of its integrated development environment • It implements the C++ programming language and offers many choices for structured or object-oriented programming
Using A Compiler • We will be using Visual C++ for this course as our programming environment • Visual C++ can be started from the Start button • Go to “file==>new==>files==>C++Source File” to start editing a new source code file. Visual C++ provides its own editor • Perform Experiment 1.2 in session1 of lab manual
Using A Compiler • Please save your file once you are done editing it. The default name of your file would be Cpp1.cpp. Change it if you do not like this name. • Now go to menu options “Build==>Build” • It will ask you some funny question(s), say “Yes” to all • Once built, the project can be executed from “Build==>Execute” or “!” icon
Visual C++ Projects • Visual C++ likes to work with projects so that you can create many small source code files and compile these together as one coherent program • Visual C++ has already created for you a project with several files. • Let us look at all those files
How many files? • Use Explorer or winfile to check out all the files that Visual C++ creates for your project • .exe Executable program file • .obj Machine code for every source file • .ilk Linker uses this file to avoid re-linking code that was not modified
How many files? • .pch Pre-compiled header file • .pdb Debugging information • .idb Additional debug information
The Anatomy of a C++ Program • Let us explore the structure of the C++ program that we have compiled and executed. • The program began with comments line. All comments start with “//” or “/*”. Comments do not contribute anything in the program. Comments are for readers to understand the program later.
The Anatomy of a C++ Program • You are expected to put comments at all important places so anyone else can read and understand your source code • The line with • #include <iostream.h> • is a directive to include a “header” file. Header files contain names of the functions to be used by the program.
The Anatomy of a C++ Program • C++ provides a lot of built-in functions so that programmers do not have to re-invent the wheel • For example, displaying something on screen is a task. Everyone is not supposed to develop a program to do it • C++ developers have already written a “program” to do it. All you need to do is to call that program by its name
The Anatomy of a C++ Program • cout<< is the way to call the standard I/O function cout which displays your data on the screen • Next we see the line • void main(void) • This line gives the name of the function that you are developing. main() is the default name used for the main function of any program
The Anatomy of a C++ Program • Function is a block of code that performs a given task. A function carries a name and opening and closing braces. • If there is only one function in your program, it must be named main(). • If there are two or more functions in your program, one of the functions must be named main() • All function names are unique in a program
The Anatomy of a C++ Program • The function main is started with an opening brace { and terminated with a closing one } • These braces identify the start and end of a block of code. e.g. a function • We will identify some more uses of these braces in future
Lab-1 Due 9/8/2000 • Please submit your report of experiment 1.1 and experiment 1.2. • Perform one of the post lab problems as listed on page 11 of the lab manual and submit your report • Reports can be put in my mailbox in the department or given in class on Friday September 8th
Lab-1 Continues (Session I-B) • We have learned • How to use a text editor in Windows • How to use Visual C++ • How to compile, build and run programs in Visual C++ • We should strengthen our knowledge by doing some more examples
Q&A • Why is <iostream.h> included in the program source code? • Why do we name the only function in our program as main()? • What is the use of opening braces and closing braces in the program? • What is a header file? • What is cout<<? • Why do we put semicolons at the end of each statement?
Lab Experiments • We can introduce errors in our source code and then check the compiler error messages • Perform Experiment 1.3 on page 8 of lab manual session-1
Lab Experiments • The little character “\n” inserts a newline in the display. We can use it whenever we want the next item to begin on a new line • Perform Experiment 1.4 to learn of the effects of placing \n in the middle or end of displayed text strings • Perform Experiment 1.6 to learn of the rules about placing quotation marks in the text.
Practicing ASCII Art • Right now we have the expertise of using the output statement cout in displaying text • We can use this expertise to display several different things. • For example, let us see how to show a triangle with this statement • Can you write a program to show a triangle or a square, or an automobile with cout?