Lesson1 Fundamentals of C- Prog
Lesson1 Fundamentals of C- Prog
UNIVERSITY
Cabambangan, Villa de Bacolor 2001, Pampanga,
Objectives
After completing the module, the students are expected to:
Appreciate the history of the C++ programming language
Familiarize with the terms used in C++ programming language
Able to construct the basic structure of C++
Understand the C++ standard library/libraries and headers
Understand the files and standard functions
Instructions
What are the main parts of the basic structure of C++ programming
language?
What is the standard library of C++ programming language?
How does Bjarne Stroustroup make the C++ programming language?
What is the difference of <iostream> and <iomanip> header libraries?
DON HONORIO VENTURA STATE
UNIVERSITY
Cabambangan, Villa de Bacolor 2001, Pampanga,
Computers are some of the most versatile tools that we have available. They are
capable of performing stunning feats of computation, they allow information to be
exchanged easily regardless of their physical location, they simplify many every-day
tasks, and they allow us to automate many processes that would be tedious or boring to
perform otherwise. However, computers are not "intelligent" as we are. They have to be
told in no uncertain terms exactly what they're supposed to do, and their native languages
are quite unlike anything we speak. Thus, there's a formidable language barrier between a
person who wishes a computer to do something, and the computer that typically requires
instructions in its native language, machine code, to do anything. So far, computers
cannot figure out what they are supposed to do on their own, and thus they rely on
programs which we create, which are sets of instructions that the computer can
understand and follow.
Compiled Languages
are translated to the target machine’s native language by a program called a
compiler. This can result in very fast code, especially if the compiler is effective at
optimizing, however the resulting code may not port well across operating systems and
the compilation process may take a while.
Interpreted Languages
are read by a program called an interpreter and are executed by that program.
While they are as portable as their interpreter and have no long compile times, interpreted
languages are usually much slower than an equivalent compiled program.
Low-level language
is generally quite similar to machine code, and thus is more suitable for programs
like device drivers or very high-performance programs that really need access to the
hardware.
High-level language
focuses more on concepts that are easy to understand by the human mind, such as
objects or mathematical functions.
The first C with Classes compiler was called Cfront, which was derived from a C compiler
called Cpre. It was a program designed to translate C with Classes code to ordinary C. A
rather interesting point worth noting is that Cfront was written mostly in C with Classes,
making it a self-hosting compiler (a compiler that can compile itself). Cfront would later be
abandoned in 1993 after it became difficult to integrate new features into it, namely C++
exceptions. Nonetheless, Cfront made a huge impact on the implementations of future
compilers and on the Unix operating system.
Timeline Event
1993 The name of the language was changed from C with
Classes to C++. The ++ operator in the C language
is an operator for incrementing a variable, which
gives some insight into how Stroustrup regarded the
language.
Example explained
Line 1: #include <iostream> is a header file library that lets us work with input and
output objects, such as cout (used in line 5). Header files add functionality to C++
programs.
Line 2: using namespace std means that we can use names for objects and variables
from the standard library.
Don't worry if you don't understand how #include <iostream> and using namespace
std works. Just think of it as something that (almost) always appears in your program.
Line 3: A blank line. C++ ignores white space. But we use it to make the code more
readable.
Line 4: Another thing that always appear in a C++ program, is int main(). This is called
a function. Any code inside its curly brackets {} will be executed.
Line 5: cout (pronounced "see-out") is an object used together with the insertion
operator (<<) to output/print text. In our example it will output "Hello World!".
Note: The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }
Remember: The compiler ignores white spaces. However, multiple lines makes the
code more readable.
Line 7: Do not forget to add the closing curly bracket } to actually end the main
function.
DON HONORIO VENTURA STATE
UNIVERSITY
Cabambangan, Villa de Bacolor 2001, Pampanga,
Omitting Namespace
You might see some C++ programs that runs without the standard namespace library.
The using namespace std line can be omitted and replaced with the std keyword,
followed by the :: operator for some objects:
Comments
Comments can be used to explain the C++ code, and to make it more readable. It can also
be used to prevent execution when testing alternative code.
Single-line comments start with two forward slashes (//)
Multi-line comments start with (/*) and ends with (*/). Any text between the
comment symbols will be ignored by the compilers.
DON HONORIO VENTURA STATE
UNIVERSITY
Cabambangan, Villa de Bacolor 2001, Pampanga,
Name Descriptions
cerr The object cerr controls output to a stream buffer associated
with the object stderr, declared in <cstdio>.
cin Specifies the cin global stream.
clog Specifies the clog global stream.
cout Specifies the cout global stream.
wcerr Specifies the wcerr global stream.
wistream The object controls extractions from the standard input as a
wide
stream. Once the object is constructed, the call wcin.tie
returns &wcout.
wclog Specifies the wclog global stream.
wostream The object controls insertions to the standard output
as a wide stream.
DON HONORIO VENTURA STATE
UNIVERSITY
Cabambangan, Villa de Bacolor 2001, Pampanga,
<iomanip>
The header <iomanip> is part of the Input/output library of the C++
Standard Library. It defines the manipulator functions resetiosflags(), setiosflags(),
setbase(), setfill(), setprecision(), and setw(). These functions may be conveniently
used by C++ programs to affect the state of iostream objects. It contains the
functions that we can use to format the output of the C++ program. These
functions can be used one at a time or together to make the output of our program
more presentable.
Setw
Function Prototype: setw (int n).
Parameter(s): n=> value of the field
width (number of characters) to be
used.
Return Value: unspecified
Description: Function setw sets the field
width or the number of characters to be
used for outputting numbers.
Setfill
Function Prototype: setfill (char_type c).
Parameter(s): n=> new fill character
for the stream; char_type: type of
characters used by stream.
Return Value: unspecified
Description: setfill sets c as the new fill
character for the stream.
DON HONORIO VENTURA STATE
UNIVERSITY
Cabambangan, Villa de Bacolor 2001, Pampanga,
References:
Goodrich, Michael et.al (2011). Data Structures and Algorithms in C++. John
Wiley & Sons Inc, United States of America
Educational Technology Journals.
http://www.educational-software-directory.net/publications/journals
CPlusPlusNotesForProfessionals. http://www.GoalKicker.com
C++ Tutorials. http://www.programiz.com
C++ Tutorials. http://www.w3schools.in