Sie sind auf Seite 1von 28

Writing my First

Program
Daniel M. Ahiatrogah
Regent University College
September 2013
Learning Outcomes
By the end of the class students shall be
able to
Write a simple C++ Program
Describe the parts of a C++ program.

Regent University College of


Science and Technology 2
Steps to writing a Program
Type the program into your editor
Save the Program/File
Compile the Program
Link the Program
Run the Program
The output of the Program will then be printed on
your screen
Remember to pay attention to punctuation when
writing/typing the program.
Regent University College of
Science and Technology 3
Text Editor
It produces file with plain text in them.
There are no formatting commands or special
symbols required by a particular word
processor.
It also does not have any automatic word
wrap, bold print, italic, underline among others.

Regent University College of


Science and Technology 4
Types of Errors
Compile-time Error
Error during the time you run the compiler
Link-time Error
Error during the time you run the linker
Run-time Error
Error during the time you run the program

Regent University College of


Science and Technology 5
Functions of the Compiler &
Linker
The Compiler translates the source code into
an object file. This phase of program
implementation is called Compiling
The Compiler again invokes a linker, which
turns the object file into an executable program.
Compilers produces a program that is very fast
each time it is run.
Linkers are however, preferred by
programmers to Compilers.
Regent University College of
Science and Technology 6
Advantages of Compiled
Languages
Executable Program can be distributed to
those who do not have the compiler installed
on their system.
The compiled code is efficient and can be run
many times.
Overhead for the translation is incurred only
once.
Compiled code only needed to be loaded and
executed.
Regent University College of
Science and Technology 7
Regent University College of
Science and Technology 8
Compiled and Interpretive
Languages.
Cost of running the interpretive language is
higher than that of compiled language.
Compiled language is suited for part of an
application that demands heavy resource usage
while the interpretive language should be used
normally for interface portion of the application.
Greater Dependency on computer resource
when using interpretive language compared to
compiled language.
Regent University College of
Science and Technology 9
Compiling the Source
Code
Source code, which is a text listing of
commands to be compiled or
assembled into an executable
computer program , is not a program.
Source code is human-readable.
It needs to be translated into a program using
a compiler.
Do use a simple text editor to create the
source code.
Regent University College of
Science and Technology 10
Compiling Source Code (2)
by Margaret Rouse - in 778 Google+ circles
Source code is the programming language
statements in a program before they are
compiled into object code, the code that is
actually processed in a computer.

Regent University College of


Science and Technology 11
Compiling from the OS
cmd line
For the Borland C++ compiler :
bcc <filename>
For the Borland C++ for Windows compiler
bcc <filename>
For the Borland Turbo C++ compiler
tc <filename>
Microsoft compilers:
cl <filename>

Regent University College of


Science and Technology 12
Creating an Executable File with
the Linker
Create a source code, with a .cpp extension
Compile the source code into a file with an
.obj extension
Link the OBJ file with any needed libraries to
produce an executable program.
NB: C++ programs are typically created by
linking together one or more OBJ files with one
or more libraries. A library is a collection of
linkable files that came with the compiler.
Regent University College of
Science and Technology 13
Parts of a C++
Program

Regent University College of


Science and Technology 14
The Pound (#) Symbol
The first character in the program is the #
symbol.
It is a signal to the preprocessor.
The preprocessor is the first to read through
the source code anytime the compiler is
started.
It looks for lines that begins with the pound
symbol (#), and acts on those lines before the
compiler runs.
Regent University College of
Science and Technology 15
What is a Preprocessor?
It is a program that processes its input data to
produce output that is used as input to another
program . http://en.wikipedia.org/wiki/Preprocessor

The preprocessor is a program that is invoked by


the compiler to process code before compilation.
The preprocessor is executed before the actual
compilation of code begins.
The output of a preprocessor is used as input
data for other program such as the compiler.
Regent University College of
Science and Technology 16
Preprocessor Directives
Preprocessor directives are not program statements
but directives for the preprocessor
Lines of the source file beginning with the character #
It extend only across a single line of code
No semicolon (;) is expected at the end of a
preprocessor directive
The effect of each preprocessor directive is a change
to the text of the source code, and the result is a new
source code file, which does not contain the directives

Regent University College of


Science and Technology 17
More on Preprocessor Directives
The reserved word include informs the
preprocessor that what follows is a filename.
# include <iostream.h>
It also tells it to find the particular file and read it in
right here in the current source code.
The angle bracket tells the preprocessor to look at
directory that holds all the H files for your compiler.
The preprocessor will include the file iostream.h
into the source code as if you type it into the source
code yourself.
Regent University College of
Science and Technology 18
Namespace
Namespaces allows to group entities like
classes, objects and functions under a name.
This way the global scope can be divided in
"sub-scopes", each one with its own name.

Namespace
Namespace Animal
Animal Namespace
Namespace Birds
Birds
{
{ {
{
string
string domestic;
domestic; string
string domestic;
domestic;
string
string wild_ani;
wild_ani; string
string wild_ani;
wild_ani;
}
} }
}
Regent University College of
Science and Technology 19
Calling from a Namespace
Animal :: domestic
Animal :: wild_ani
Bird :: domestic
Bird :: wild_ani

Regent University College of


Science and Technology 20
Namespace Std
Type using followed by the namespace
Ends the statement with the name of the
particular namespace.
Terminate the statement with a semicolon.
The program will know to look in the std
library to find the object. Namespace std
contains all the classes, objects and functions
of the standard C++ library.

Regent University College of


Science and Technology 21
Without using
namespace
#include <iostream>
int main ()
{
std::cout << "Hello
world!\n";
return 0;
}
Regent University College of
Science and Technology 22
With namespace
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello world!\n";
return 0;
}

Regent University College of


Science and Technology 23
The Main() Function
A function is a block of code that perform one or
more action.
Functions are invoked or called by other functions.
All functions begins with an opening brace ({ ) and
ends with a closing brace ( } )
Main() function starts automatically anytime the
program is executed.
The return type for the main() function is an int
which means it will return an integer value.

Regent University College of


Science and Technology 24
The Main() Function
Every statement between both braces is part
of the function.

Regent University College of


Science and Technology 25
The cout
It is an object (instance of a class)
It is used to print a message to the screen.
It uses the iostream.h file to perform its function.
Using cout:
Type word cout
Type the output redirection operator ( << )
Type the statement to display on the screen. Enclosed
strings in double quotes ( );
NB: variables names should not be included in double
quotes
Regent University College of
Science and Technology 26
The cout
A text string can be defined as a set of
characters.
It can also be defined as a series of printable
characters.
The \n ( c programming ) tell cout to put a new
line after the word. endl performs the same
task in C++.
Each statement should be terminated using
semicolon ( ; )
Regent University College of
Science and Technology 27
Reference
https://www.google.com.gh/?
gws_rd=cr&ei=I5tBUtGTE-
ew7AbKnYDQAw#q=what+is+a+source+code
http://www.cplusplus.com/doc/tutorial/preprocess
or/
http://publib.boulder.ibm.com/infocenter/comphe
lp/v8v101/index.jsp?topic=
%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref
%2Fpcl.htm
http://www.cplusplus.com/doc/tutorial/namespac
es/
Sams Teach Yourself C++ in 21 days , Second
Edition
Regent University College of
Science and Technology 28

Das könnte Ihnen auch gefallen