Académique Documents
Professionnel Documents
Culture Documents
What is C Programming?
C
Program Code
C
Compiler
Executable Program
human-readable form
translator
machine-readable form
Evolutions of C
BCPL B C 1967 1970 1972 (Martin Richards) (Ken Thompson) (Dennis Ritchie)
ANSI C 1989
C++
1990
(Bjourne Stroustrup)
What is C++?
Stroustrup states that the purpose of C++ is to make writing good programs easier and more pleasant for the individual programmer.
Stroustrup added OOP (Object Oriented Programming) features to C without significantly changing the C component. Thus C++ is a "relative" (called a superset) of C, meaning that any valid C program is also a valid C++ program.
Etc.
All of these software packages enable you to create computer programs with C++, but they all implement the C++ language in a slightly different manner. In an attempt to maintain portability of both the C and C++ languages, the American National Standards Institute (ANSI) developed a standard of consistency for C and C++ programming.
Advantages of C++
Available on most machines Can get good performance Can get small size Can manage memory effectively Can control everything
Suitable for almost any type of program (from systems programs to applications)
Disadvantages of C++
Complicated!!!
40 operators, intricate precedence, pointers, etc. can control everything
Aspects above can result in high cost, maintenance and reliability problems
C++ Trivia
Due to their power and ease of use, C and C++ were used in the programming of the special effects for Star Wars.
THE MECHANICS OF CREATING A C++ PROGRAM Editor: accepts the typing of the
source code (or header file). Source file: the file that contains the program you prepared in the editor after you save it. (.cpp) Header file: header files such as iostream.h Preprocessor: performs preliminary operations on files before they are passed to the compiler. Compiler: translates the source code to machine language. Object code: the file containing the translated source code. (.obj) Linker: links the object file with additional code, such as the library codes. Executable code: the file containing the final product. (.exe)
/* Sample C program that outputs the word Hello */ #include <stdio.h> void main(void) { printf(Hello\n); }
The program shown in the example above is what is referred to as a program source code. The source code is made up of a combination of letters, numbers and other symbols. The source code can be created by the programmer/user using any text editor.
// Sample C++ program that outputs the word Hello World #include <iostream.h> int main () { cout << "Hello World!"; return 0; }
// Comments at the beginning: // your name // a description of the program // information pertinent to the program #include <iostream.h> #include <stdlib.h> int main(void) { system ("CLS"); cout<< "This is a literal print\n"; cout<< "Yea!"<<endl; return 0; }
The CODE:
Information about the code: COMPONENTS This is AcommentPROGRAM beginning OF a C++ line. All the lines with two slash signs (//) are considered // Comments at the comments and do not have any effect on the beginning: behavior of the program. They can be used by // your name the programmer to include short explanations // a description of the or observations within the source itself. In this program // information pertinent to case, the line is a brief description of what our the program program does. Preprocessor directives begin with # and tell the computer to find the filename that follows and read it in. The file iostream.h (Input/Output stream) is used by cout. The stdlib.h is used to clear the screen. Note: <iostream.h> should always be your first "header" file.
int main(void)
EVERY C++ program has a main( ) function. A function is a block of code that performs one or more actions. While functions are usually called within a program, main( ) is called automatically. You may see various adaptations of this line of code. While other adaptations are allowed, be sure to maintain "int" as the return type because every ANSI-compliant C++ program will return zero to the IDE. Between the French curly braces is the body of function main( ). It is indented and terminates by returning 0 to the IDE. This body: * clears the screen, * prints Yea! to the screen. The semicolon character (;) signifies the end of the instruction and must be included after every instruction in any C++ program.
4. Project Workspace Window displays the two kind of views: - the Class View and File View 5. Editor Window that will enable the programmer to write and edit codes. 6. Output Window will display the different kind of errors while the program is executed
1.
2. Select NEW 3. Select the FILE tab 4. Select C++ Source File 5. Enter a file name Your project name and your file name can be the same if you wish. Your source code files will have the extension .cpp 6. Click OK
CLOSING FILE
1. Pull down the FILE menu 2. Select CLOSE WORKSPACE to close the entire screen. 3. Answer YES to the dialog box. 4. Pull down the FILE menu and select EXIT to leave the software package. Choosing CLOSE only, will close only the program and not the project.
Hands-on # 1:
Create a simple C++ program that will display the following information: 1. Full Name 2. Level & Section 3. Birthdate 4. Address