c++ lesson # 1 - intro
TRANSCRIPT
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 1/25
What is C?
• a general purpose programminglanguage developed by Dennis
Ritchie
• can be used to create lists of
instructions for a computer to follow
• a compiled language
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 2/25
CProgram Code
C
Compiler
ExecutableProgram
human-readableform
machine-readableform
What is C Programming?
translator
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 3/25
What about C Compiler?
• If you are using a UNIX machine, the Ccompiler is available for free. (It is called either
"cc" or "gcc" and is available on the commandline.)
• If you are a student, then the school will likelyprovide you with a commercial compiler.
• If you are working at home on a Windowsmachine, you may download a free C compileror purchase a commercial compiler.
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 4/25
Evolutions of C
BCPL 1967 (Martin Richards)
B 1970 (Ken Thompson)
C 1972 (Dennis Ritchie)
ANSI C 1989
C++ 1990 (Bjourne Stroustrup)
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 5/25
What is C++?
Stroustrup states that the purpose of C++ is tomake writing good programs easier and more
pleasant for the individual programmer.
Stroustrup added OOP (Object OrientedProgramming) features to C without
significantly changing the C component. ThusC++ is a "relative" (called a superset) of C,meaning that any valid C program is also avalid C++ program.
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 6/25
Versions of the C++ Language
ANSI C++
Borland C++
Code Warrior (Mac)
Turbo C++
Visual C++ (Windows) Etc.
All of these software packagesenable you to create computerprograms with C++, but they
all implement the C++language in a slightly differentmanner. In an attempt tomaintain portability of both the
C and C++ languages, theAmerican National StandardsInstitute (ANSI) developed astandard of consistency for Cand C++ programming.
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 7/25
Advantages of C++
Available on most machines
Can get good performance
Can get small size
Can manage memory effectively
Can control everythingSuitable for almost any type of program
(from systems programs to applications)
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 8/25
Disadvantages of C++
Complicated!!!
40 operators, intricate precedence, pointers, etc.
can control everythingmany exceptions and special cases
tremendous libraries both standard, vendor specific,
and available for purchase, but all are intricate Aspects above can result in high cost,
maintenance and reliability problems
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 9/25
Steps in Developing a C++
Program
Plan
Edit (uses editor)
Compile * (converts to binary)
Link * (resolves external references)
Load (places into memory)
Run * (transfers control, executes)
* IDE reports errors at this step; must fix them
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 10/25
C++ Trivia
Due to their power and ease of
use, C and C++ were used in theprogramming of the special effects
for Star Wars.
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 11/25
APPLICATIONS THAT CAN BE
WRITTEN WITH C LANGUAGE
1. Operating systems. MS Windows, Linux, Unix, VxWorks,Nucleus Plus,VMS, MS-DOS, Vrtx, etc.
2. Controllers such as Tape drives, disk drives, laser printers,
ink jet printers, vending machines, microwave ovens,industrial mills, mailing machines, VCRs, DVD players,airplane controllers, car controllers, etc.
3. Desktop applications (yes, another wide field) Videogames, databases, database front-ends, spreadsheets,word processors, editors, image editors, architecturetools, schematic routers, assemblers, compilers, web & internet applications.
4. Scientific applications
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 12/25
THE MECHANICS OF CREATING
A C++ PROGRAM Editor: accepts the typing of thesource code (or header file).
Source file: the file that containsthe program you prepared in theeditor after you save it. (.cpp)Header file: header files such asiostream.h
Preprocessor: performspreliminary operations on files beforethey are passed to the compiler.Compiler: translates the sourcecode to machine language.Object code: the file containing
the translated source code. (.obj)Linker: links the object file withadditional code, such as the librarycodes.Executable code: the file
containing the final product. (.exe)
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 13/25
How does a typical C program looks like?
First, a very simple example of a C program:Example 1:
/* 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 aprogram source code. The source code is made up of a combination ofletters, numbers and other symbols. The source code can be created by
the programmer/user using any text editor.
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 14/25
How does a typical C++ program looks like?
First, a very simple example of a C++ program:
Example 1:
// Sample C++ program that outputs the word “Hello World” #include <iostream.h>int main (){
cout << "Hello World!";return 0;
}
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 15/25
How does a typical C++ program looks like?
Example 2:
// 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;
}
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 16/25
COMPONENTS OF A C++ PROGRAMThe CODE: Information about the code:
// Comments at thebeginning: // your name // a description of theprogram
// information pertinent tothe program
This is a comment line. All the lines beginning
with two slash signs (//) are consideredcomments and do not have any effect on thebehavior of the program. They can be used bythe programmer to include short explanationsor observations within the source itself. In this
case, the line is a brief description of what ourprogram does.
#include <iostream.h>#include <stdlib.h>
Preprocessor directives begin with # and tellthe computer to find the filename that followsand read it in. The file iostream.h(Input/Output stream) is used by cout. Thestdlib.h is used to clear the screen.Note: <iostream.h> should always be yourfirst "header" file.
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 17/25
COMPONENTS OF A C++ PROGRAM
int main(void)
This code begins the actual program. EVERY C++program has a main( ) function. A function is a
block of code that performs one or moreactions. While functions are usually called withina program, main( ) is called automatically. Youmay see various adaptations of this line of code. While other adaptations are allowed, be
sure to maintain "int" as the return type becauseevery ANSI-compliant C++ program will returnzero to the IDE.
{
system ("CLS");cout<< "This is aliteral print.\n";
cout<<"Yea!“ return 0;
}
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.
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 18/25
INITIAL STEPS IN LOADING
MICROSOFT VISUAL C++
Open the Software ( MS Visual C++)
If you are working in a PC environment:
1. Click Start Menu2. Select Programs 3. Select Visual Studio
4. Select MS Visual C++ 6.0
When the Tip of the Day dialog box appears, close it byclicking on the Close button. If you do not want to seethe Tip of the Day every time you open Visual C++, click the Show tips at Startup check box to remove the check
mark and deselect this feature.
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 19/25
INTEGRATED DEVELOPMENT
ENVIRONMENT (IDE)
Microsoft VisualC++ offers an easy-to-use GUI,graphical userinterface. Itcontains thefollowing:1.Titlebar displaysthe projectfilename2.Menubar containsthe menu of C++commands
3.Toolbars displaysicons for thecommands in themenu bar 4. Project Workspace Window displays the two kind of views:
- the Class View and File View5. 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
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 20/25
WRITING C++ PROGRAM
Once you have created your project, it is time tostart typing the source code for your program.
1. Pull down the FILE menu
2. Select NEW3. 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 sourcecode files will have the extension .cpp
6. Click OK
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 21/25
7. You will now see thethree main windows of MS Visual C++. The Project
Workspace (on the left) hastwo tabs named Class View and File View at thebottom. We will be usingthe File View tab to
determine if all of theneeded files are containedwithin a project.
8. You may start typing now.
WRITING C++ PROGRAM
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 22/25
// my first program in C++
#include <iostream.h>
int main ()
{
cout << "Hello World!";return 0;
}
WRITING C++ PROGRAM
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 23/25
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 theprogram and not the project.
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 24/25
RUNNING A C++ PROGRAM
Once you have typed your source code, it is time to save your work and
see if your program works properly.1. Pull down FILE menu, select SAVE - or use the save icon
2. Pull down BUILD menu, select BUILD project.exe - or press F7, orchoose the build icon
3. If an error message occurs, double click on the error message to getan arrow pointing to the spot in the program source code that causedthe error.
4. Pull down the BUILD menu and select EXECUTE project.exe - or useexecute icon
5. Press ALT and ENTER to see your display in a full black screen.
6. Press any key to close the black screen. Do NOT click the mouse onthe underlying screen to close the black screen.
8/4/2019 C++ Lesson # 1 - Intro
http://slidepdf.com/reader/full/c-lesson-1-intro 25/25
Hands-on # 1:
Create a simple C++ program that willdisplay the following information:
1. Full Name2. Level & Section
3. Birthdate
4. Address