Professional Documents
Culture Documents
History of Programming Languages Flowcharting Introduction to C++ Programming Fundamental Data Types O Operators for f Fundamental F d l Data D Types T The C++ Mathematical Functions Making Decisions Iteration Statements Formatting and Manipulators Arrays Pointers Functions User-Defined Data Types Classes
What Is a Programming Language? History of C and C++ FORTRAN and COBOL Pascal and Basic Windows Programming The .NET platform
The first computers cost millions of dollars. Programmers were forced to program in a language in which all the instructions were reduced to a series of numbers, called machine language. This language could be directly input into the computer. A typical machine-language machine language program looks like: 1010 1111 0011 0111 0111 0110 .. and d so on f for several l h hundred d d i instructions t ti
While machines think in ones and zeroes, people don't. To program these ancient machines, machines software engineers would write their programs using a simple language in which each word in the language stood for a single instruction. This language was called assembly language because the programmers had to hand translate, or assemble, each line into machine code. A typical program might look like: Program MOV A,47 ADD A, B HALT Translation 1010 1111 0011 0111 0111 0110
.. and so on for several hundred instructions Thi process i This is ill illustrated t t d in i the th following f ll i figure. fi
Assembling a program
Translation was a difficult, tedious, and exacting task. One software engineer decided that this was a perfect job for a computer, so he wrote a program called an assembler that would do the job automatically.
Todays computers, of course, are far more powerful than y computers. p However, , the language g g that computers p these early understand, the machine language, remains the same. A complex program may consist of thousands or even millions of step-by-step machine language instructions, which would require an inordinately long amount of time to write. Over the years, a series of higher-level languages have been devised. These languages attempt to let the programmer write it i in a medium di th that t is i easy for f him hi to t understand. d t d Programming languages are far more understandable to programmers than machine language or assembly assembl language because programming languages resemble the structure and syntax of human language. language Additionally, Additionally code can be written much faster with programming languages than assembly g g because one p programming g g language g g instruction language can cover many machine language instructions.
C++ is one of many programming languages. Other popular programming languages include Java Java, C# C#, and Visual Basic Basic. There are many others. Indeed, new languages are being created all the time. However, all programming languages have essentially the same purpose, which is to enable a programmer g to g give instructions to a computer. p human p Why learn C++ instead of another programming language? C++ is i the th most t widely id l used d programming i language l and di is an industry standard for programming applications of all kinds C++ is often the best language to use when writing kinds. games. In addition, C++ is a highly efficient programming language that can conserve resources more effectively than languages such as Visual Basic or Delphi.
Windows Programming
When Microsoft designed Windows, the company decided to provide a library of functions so that people could design Windows programs. This library is called the Windows API (Application Programming Interface). A subset of this library is called the Windows GUI (Graphic User Interface). Using the Windows GUI, you can make your programs more interesting by adding command buttons, scrolling lists, icons, and other Windows elements that users can employ to interact with your programs.
Using C and the native APIs is not the only way to write programs for f Windows. Wi d However, H this thi approach h offers ff you th the best performance, the most power, and the greatest versatility in exploiting the features of Windows Windows. Executables are relatively small and don't require external libraries to run. Most importantly, importantly becoming familiar with the API provides you with a deeper understanding of Windows internals, regardless of how you eventually write applications for Windows. Microsoft Visual C++ with the Microsoft Foundation Classes Library (MFC) has been a popular alternative in recent years. years MFC encapsulates many of the aspects of Windows programming in a collection of C++ classes.
Microsoft's Visual Basic language, introduced in the early 1990s to simplify the development of Microsoft Windows applications, has become one of the most popular programming languages in the world. world Many programmers enjoy the ease of development environments such as Microsoft Visual Basic or Borland Delphi (which incorporates an object-oriented dialect of Pascal) These environments allow a programmer to focus Pascal). on the user interface of an application and associate code with user interface objects.
Flowcharting
Flowcharting
A program, like lik a river, i fl flows f from b beginning i i t to end. d Programmers may find it helpful, both in writing code and in understanding someone elses code, code to visually depict the flow of the program. Programmers use a flowchart P fl h tt to visually i ll depict d i t th the flow fl of fa program. Flowcharts use symbols. These flowcharting symbols represent different aspects of a program program, such as the start or end of a program, user input, how it displays on a monitor, and so on. on These symbols are joined by arrows which show the connections between different parts of the program and the direction of the program flow.
The commonly used flowchart symbols are: Beginning and end of a program Input and output Assignment and computation Desission C Connector t
ax 2 + bx + c = 0
:
. : x 5 + 3 x 2 10 = 0 . : x = 5 10 3 x 2 x x x . x ) (.
x .
:
: x x
: x1 x : n : i
:
(... : ) ( ) (. 2 . . (1+2+3++t )1+2+3+ +t ) (t ) (t-1 ) (t-2 ) t ( .
:
-
:
: )(value )(current value : : ) ( )(time )(method : : )(year : )(factor )(depreciation : : )(sum : : 1+2+3++t = t*(t+1)/2
v c t m y f d s
Developing a C++ Program Building a C++ Project Our First C++ Program Whitespace The Manipulator endl The Newline Character (\n) Using Comments
The following three steps are required to create and translate a C++ program:
1.
First, a text editor is used to save the C++ program in a t t fil text file. The Th code d a programmer writes it is i referred f d to t as source code, which is saved in a file that usually has a .cpp cpp extension, extension standing for C Plus Plus. Plus The source file is put through a compiler for translation. If everything works as planned, planned an object file made up of machine code is created. The object file is also referred to as a module. Finally, the linker combines the object file with other modules to form an executable file (.exe). These further modules contain functions from standard libraries or parts of the p p program g that have been compiled p p previously. y
2.
3.
Prior to compilation, header files, which are also referred to as include i l d files, fil can be b copied i d to t the th source file. fil Header H d fil files are text files containing information needed by various source files, files for example, example type definitions or declarations of variables and functions. Header files can have the file extension .h, h but they may not have any file extension. extension
Start the compiler. Choose File New from the menus. Choose Win32 Console Application and enter a project name, such as HELLO, and click OK. Choose An Empty Project from the menu of choices and click li k Finish. A dialog di l box b i is displayed di l d with i h new project j information. Cli k OK. You Click Y are t taken k b back k to t the th main i editor dit window. i d Choose File New from the menus. Choose C++ Source File and give it a name, such as hello. You enter this name into the File Name text box. Click OK. You are taken back to the main editor window.
5. 6. 7. 8.
Write the code. Ch Choose B ild Build Build B ild h hello.exe ll f from the th menus. Check that you have no build errors. You can find this i f information ti near the th bottom b tt of f the th editor. dit Run the program by pressing Ctrl+F5 or by selecting Build Execute E t hello h ll from f the th menus. Press any key to end the program.
Start the compiler. compiler Choose File New Project from the menus. Choose Win32 Ch i Console Application i i and d enter t a project j t name, such as HELLO into the Name text box, and click OK. Click the Application Settings tab in the Application Wizard (or click next). ) Choose Empty Project from the menu of Additional Options and click Finish. You are taken back to the main editor window. Choose Project Add New Item from the menus. menus Choose C++ Source File and give it a name, such as hello. You enter this name into the Name text box. Click OK. You are taken back to the main editor window.
4. 5.
6 6. 7. 8.
Write the code. Ch Choose B ild Build Build B ild h hello ll from f the th menus. Check that you have no build errors. You can find this i f information ti near the th bottom b tt of f the th editor. dit Run the program by pressing Ctrl+F5 or by selecting D b Debug Start St t Without With t Debugging D b i f from the th menus. Press any key to end the program.
Output
Welcome to C++!
Note:
If you use Visual C++ 2003 and above, you should always write #include <iostream> using namespace std; instead of #include <iostream.h>
Analysis
On the O th first fi t line, li the th file fil iostream.h i t h is i i included l d d into i t the th current file. Heres how that works: The first character is the # symbol which is a signal to a program called the preprocessor. symbol, preprocessor Each time you start your compiler, the preprocessor is run first. The p preprocessor p reads through g y your source code, , looking for lines that begin with the pound symbol (#) and acts on those lines before the compiler runs. The command #include is a preprocessor instruction that says, What follows is a filename. Find that file, read it, and place it right here. The angle brackets around the filename tell the preprocessor to look in all the usual places for this file. file The file iostream.h iostream h (input-output-stream) (input output stream) is used by cout, which assists with writing to the console. The effect of this line is to include the file iostream.h iostream h into this program as if you had typed it in yourself.
The actual program starts with the function named main(). y C++ p program g requires q a function called main(), (), not Every Main() or MAIN() or mane(). When you run a C++ program, execution always begins at the beginning of the main() function. Therefore, if you dont have main(), you dont have a complete program, and the compiler points out th t you havent that h t defined d fi d a main() i () function. f ti A function is a block of code that performs one or more actions. ti main() i (), like lik all ll f functions, ti must t state t t what h t kind ki d of f value it returns. The return value type for main() in our program is int, which means that this function returns an integer to the operating system when it completes. The main() function ends with the closing brace (}). ) All functions begin with an opening brace ({) and end with a g brace (}). Everything y g between the opening p g and closing closing braces is considered a part of the function.
There are two statements in the program: cout << "Welcome Welcome to C++!"; C++! ; return 0; The first statement tells the computer p to display p y the q quoted phrase. Most statements tell the computer to do something. j cout (pronounced p C out) is used to p print a The object string to the screen. This object is used in C++ to handle output to the console. Heres how cout is used: Type the word cout, followed by the insertion operator (<<). The insertion operator directs the string on its right to cout, which sends it to the display. displ Wh te er follows Whatever follo s the insertion i sertio operator oper tor is written to the console. If you want a string of characters written be certain to enclose them in double quotes ("). written, ) A semicolon signals the end of the statement. This is a crucial part of the syntax p y but easy y to forget. g If y you leave out the semicolon, the compiler will signal an error.
The last statement in the function body is return 0;. This t ll main() to tells t return t the th value l 0 to t whoever h called ll d it it, in i this thi case the operating system. A value may be returned to the operating system to indicate success or failure, failure or using a failure code to describe a cause of failure. The previos C++ example have the following elements: