Documente Academic
Documente Profesional
Documente Cultură
Lab1
Lab1
Lab 1
Student ID: ---------------------------Date: -----------------------------
Activities
Completed
Inlab
Lab1
Outlines:
Lesson 1-1: Introduction to C++
Lesson 1-7: How to run a program with errors, read the errors and correct them.
Lab1
Introduction to C++
Lesson 1-1
Welcome to C++! We have worked hard to create what we hope be an appropriate text book to give you a guidance to program by using C++. C++, an extension of C, was developed by Bjarne Stroustrup starting in 1979 at Bell Labs as an enhancement to the C programming language and originally named C with Classes. It was renamed C++ in 1983. As one of the most popular programming languages ever created, C++ is widely used in the software industry. Some of its application domains include systems software, application software, device drivers, embedded software, high-performance server and client applications, and entertainment software such as video games. Several groups provide both free and proprietary C++ compiler software, including the GNU Project, Microsoft, Intel and Borland. C++ has greatly influenced many other popular programming languages, most notably Java. C++ is also used for hardware design, where design is initially described in C++, then analyzed, architecturally constrained, and scheduled to create a register transfer level hardware description language via high-level synthesis.
Introduction to Programming
Lesson 1-2
Programming is a process of problem solving. Computer programming is the process of designing, writing, testing, debugging / troubleshooting, and maintaining the source code of computer programs. This source code is written in a programming language. The purpose of programming is to create a program that exhibits a certain desired behaviour (customization). The process of writing source code often requires expertise in many different subjects, including knowledge of the application domain, specialized algorithms and formal logic.
Lab1
Lesson 1-3
Input: Before a program can operate on data, the data need to be entered by the user. Process: To make some calculations and manipulation for the inserted data. Output: To show the processed information or any notation on the screen or any proper output device. Algorithm: A step-by-step problem-solving process in which a solution is arrived at in a finite amount of time. Example: One of the simplest algorithms is to print succeed if the students mark is >=50, and print failed if the students mark is <50, the algorithm represented in the flowchart below:
Lab1
A typical C++ environment: 1- Editor: program is created and saved on the disk 2- Preprocessor: process the data 3- Compiler: Compiler creates object code and stores in a disk 4- Linker: Links the object code with the libraries 5- Loader: To load the program in memory 6- CPU: execute the instructions
Lesson 1-4
Microsoft Visual C++ (often abbreviated as MSVC or VC++) is a commercial integrated development environment (IDE) product engineered by Microsoft for the C, C++, and C++/CLI programming languages. It has tools for developing and debugging C++ code, especially code written for the Microsoft Windows API, the DirectX API, and the Microsoft .NET Framework. In this lab we are going to use Visual C++ version 2008. Any other versions such as 2005 or visual studio 6 are acceptable versions.
Lab1
Lesson 1-5
1. From Start go to All Programs then click on Visual C++2008 Express Edition. Then a screen of C++ software will be opened as Fig1-2.
Lab1
2. From File click on New then choose Project see Fig1-3. A New Project screen will be shown as Fig1-4.
Lab1
3. From the (New Project) window that appears, select Win32 Console Application. Then choose a name for your project (As start name it Pro1), then choose a location from Browse button to save it there (As start create a new folder with your id). Then click on Ok. After that a pop up window will appear click on Next. See Fig1-5, 1-6.
4. Another popup window will appear, choose Empty Project. Then, click on Finish.
Lab1
5. To create a new form, you can go to Project from menu bar, then choose new item. Another way, right click on Source Files, then a popup menu will appear choose Add then choose New Form. See Fig1-7.
6. Select C++ File and write the name of your form. (eg. start file name with your name). Fig1-8.
Choose C++File(.cpp)
10
Lab1
7. A new screen will be opened to write the code. This window is the editor window. See Fig1-9.
8. Write your code in the code area as below, this code is the general structure of any C++ program. (Its a program that does not do anything).
9. To check that your code is free of errors, go to Build from menu bar and click on Build Solution, to insure that the code does not have any syntax errors or warning. Fig1-10.
11
Lab1
12
Lab1
10. To execute the program, from Debug menu click on Start Without Debugging. See Fig112.
12. There is no output, because the program does not do anything. See Fig1-13.
13
Lab1
Lesson 1-6
Redo the previous steps to write the following C++ program then save, compile and run it.
#include <iostream> using namespace std; int main() { cout<<"Welcome to C++ "; return 0; }
#include <iostream>: Preprocessor directives. This tells the preprocessor to include in the program the contents of the input/output stream header file <iostream>. This file should be included for any C++ program. Using namespace std: To give the program the ability to use cin, cout, or endl. Int main() :main is a function that should be written in every C++ program. C++ program start executing at function main. The keyword int means that the main will return an integer value. You could replace it with void which means to return nothing. {: The left brace must begin the body of every function. }: right brace to end every function. Cout<<Welcome to C++<<endl; : a C++ statement, each statement should be end with semicolon (; ). Cout<< is an order to print on the screen the following Welcome to C++. Endl means to end the line on the screen. Return 0: end the main function.
14
Lab1
15
Lab1
Lesson 1-7
You need to read the errors from the output window. If this window was not opened, then go
#include <oistream> using namespace std int main { cout>>"Welcome to C++ ; return ; {
2. Write the errors that come out from the above program in the provided Error sheet, then
correct them and write the corrections.
16
Lab1
InLab Assignment
1. To write a new program: Write a program to print your name and your id on the paper then on visual C++ 2008
2. To Open and edit an existing file: Open the saved file from the previous question to print your name, your ID, your address, and your age each in a single line.
17
Lab1
Error sheet
Name: ---------------------------------Section: ------------------------------# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 # represents the line number in the program. OK Type of Error
Lab1
Student ID: ----------------------------Date: -------------------------------Corrections
18