2. Previous Lecture Outline
• Understanding the concept of flowcharts.
• Working with sequence, decision, repetition and case control
flowchart structures.
• Understanding algorithms.
3. Lecture Outline
• Introduction to C++ and other popular programming languages
• Historical development of C++
• Writing C++ program
• Structure of C++ program
4. Introduction to C++
• C++ is the advanced version of C language that is used for developing
computer programs.
• C++ is a powerful programming language that fully supports
• Procedural programming
• Object-oriented programming
• Data abstraction
• Generic programming
12. Historical Development of C++ Language
Programming Language Description
BCPL Language
Developed in 1967 by Martin Richards as a language for writing operating
systems and compilers for operating system.
B Language
Developed by Ken Thompson at Bell Laboratories. The early version of UNIX
operating system developed in 1970.
C Language
Developed by Dennish Ritche in 1972 by Bell Laboratories. It is available for
most computers and is hardware independent. Portable computer programs
can be created.
C++ Language
C++ is the extension of C developed by Bjarne Stroustrup in the early 1980s at
Bell Laboratories. It provides the capabilities of object-oriented programming.
Current versions of C++ such as Microsoft Visual C++ .NET and Borland C++ Builder.
We will follow the syntax and rules of turbo C++ version 3.0 and Borland C++ v5 for MS DOS environment.
13. Writing C++ Program
• C++ systems generally consist of three parts
• A program development environment
• The language
• C++ Standard Library
• C++ programs typically go through six phases
• Edit, Preprocess, Compile, Link, Load and Execute.
16. Writing C++ Program
• Phase 1 consists of editing a file with an editor program, normally
known simply as an editor.
• Type a C++ program (source code) using the editor.
• Make any necessary corrections.
• Save the program.
• C++ source code filenames often end with the .cpp, .cxx, .cc or .C extensions
(note that C is in uppercase) which indicate that a file contains C++ source
code.
18. Writing C++ Program
• Linux editors: vi and emacs.
• C++ software packages for Microsoft Windows such as Microsoft
Visual C++ (microsoft.com/express) have editors integrated into the
programming environment.
• You can also use a simple text editor, such as Notepad in Windows, to
write your C++ code.
• Integrated Development Environments (IDEs)
• Provide tools that support the software-development process, including
editors for writing and editing programs and debuggers for locating logic
errors—errors that cause programs to execute incorrectly.
19. Writing C++ Program
• Popular IDEs
• Microsoft® Visual Studio 2012 Express Edition
• Dev C++
• NetBeans
• Eclipse
• Apple’s Xcode
• CodeLite
20. Writing C++ Program
• In phase 2, you give the command to compile the program.
• A preprocessor program executes automatically before the compiler’s
translation phase begins (so we call preprocessing Phase 2 and compiling
Phase 3).
• The C++ preprocessor submits commands called preprocessing directives,
which indicate that certain manipulations are to be performed on the
program before compilation.
• These manipulations usually include other text files to be compiled, and
perform various text replacements.
22. Writing C++ Program
• In Phase 3, the compiler translates the C++ program into machine-
language code—also referred to as object code.
23. Writing C++ Program
• Phase 4 is called linking.
• The object code produced by the C++ compiler typically contains “holes” due
to some missing parts.
• A linker links the object code with the code for the missing functions to
produce an executable program.
• If the program compiles and links correctly, an executable image is produced.
25. Writing C++ Program
• Phase 5 is called loading.
• Before a program can be executed, it must first be placed in memory.
• This is done by the loader, which takes the executable image from disk and
transfers it to memory.
• Additional components from shared libraries that support the program are
also loaded.
27. Writing C++ Program
• Phase 6: Execution
• Finally, the computer, under the control of its CPU, executes the program
one instruction at a time.
• Some modern computer architectures can execute several instructions in
parallel.
29. Writing C++ Program
• Problems That May Occur at Execution Time
• Programs might not work on the first try.
• Each of the preceding phases can fail because of various errors that we’ll
discuss throughout this course.
• If this occurred, you’d have to return to the edit phase, make the necessary
corrections and proceed through the remaining phases again to determine
that the corrections fixed the problem(s).
• There is also a standard error stream referred to as cerr. The cerr stream is
used for displaying error messages.
33. Structure of C++ Program
• The instructions that are given to the compiler before the beginning
of the actual program are called preprocessor directives.
• Also known as compiler directives.
• The preprocessor directives consist of instructions for the compiler.
• The compiler adds special instructions or code from these directives into the
program at the time of compilation.
• It starts with a # (HASH) or (SHARP) and the keyword include or define.
• They are used to include header files.
34. Structure of C++ Program
• Header files in C++ are source files that
contains definitions of library
functions/objects.
• A header is added if function/object defined
in it is to be used in the program.
• For example, the header file iostream.h has
the definitions of different built-in
input/output functions. To include the
header file into the C++ source code we
normally write:
#include<iostream>
int main()
{
…………
…………
return 0;
}
36. Structure of C++ Program
• The C++ program is typically consist
of at least one function called
main() function.
• When C++ program starts execution,
main() is the first program that is
executed.
• The keyword int to the left of main()
indicates that main() “returns” an
integer (whole number) value.
#include<iostream>
int main()
{
…………
…………
return 0;
}
37. Structure of C++ Program
• Each function may or may not
return a value back.
• The function contains body in which
duties of the function is placed.
#include<iostream>
int main()
{
…………
…………
return 0;
}
38. Structure of C++ Program
• A statement instructs the computer to
perform an action.
• Together, the quotation marks and the
characters between them are called a
string, a character string or a string
literal.
• Most C++ statements end with a
semicolon (;), also known as the
statement terminator.
• C++ is a case-sensitive language.
#include<iostream>
int main()
{
…………
…………
return 0;
}
39. Structure of C++ Program
• You use blank lines, space characters and tab characters (i.e., “tabs”)
to make programs easier to read.
• Together, these characters are known as white space.
• White-space characters are normally ignored by the compiler.
42. Structure of C++ Program
• Comments can be used for
understanding the source
code.
• Single-line (//…………..)
comment vs multi-line
(/*……..*/) comment.
//sample program
#include<iostream>
int main()
{
…………
…………
return 0;
}
46. Summery
• Introduction to C++ and other popular programming languages
• Historical development of C++
• Writing C++ program
• Structure of C++ program
Data abstraction means determining the essential characteristics of an object. Abstraction is one of the basic principles of object-oriented design, which allows you for creating user-defined data types, known as objects.
Object oriented programming abbreviated as OOP is a programming approach that supports object technology. It allows pieces of software to be reused and interchanged between programs. Major concepts used in OOP are encapsulation, inheritance, and polymorphism.
Generic programming means that you are not writing source code that is compiled, but that you write templates of source codes that the compiler in the process of compilation transforms into source codes. Generic programming is about simplifying software components so that they can be easily reused in a wide variety of situations.
C++ programs consist of pieces called classes and functions.
Most C++ programmers take advantage of the rich collections of classes and functions in the C++ Standard Library.
Two parts to learning the C++ “world.”
The C++ language itself, and
How to use the classes and functions in the C++ Standard Library.
Many special-purpose class libraries are supplied by independent software vendors.
C++ programs typically contain references to functions and data defined elsewhere, such as in the standard libraries or in the private libraries of groups of
programmers working on a particular project.
Programs input and output data.
Input cin and output through cout.
logic errors—errors that cause programs to execute incorrectly.
A keyword is a word in code that is reserved by C++ for a specific use.
White-space characters in strings are not ignored by the compiler.
<< stream insertion or put to operator
A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries. Using namespace, you can define the context in which names are defined. In essence, a namespace defines a scope.