Module 1 (Lesson1)
Module 1 (Lesson1)
Introduction to C++
In this lesson, you will learn what is C++, who developed C++ programming languages,
and learn how to create a simple program in C++.
Learning Outcomes
Define C++;
Identify who developed C++;
Know how to start/instantiate the C++ editor;
Write a simple C++ program; and
Execute the C++ program.
INTRODUCTION TO C++
What is C++?
The C++ programming language has a history going back to 1979, when
Bjarne Stroustrup was doing work for his Ph.D. thesis. One of the
languages Stroustrup had the opportunity to work with was a language
called Simula, which as the name implies is a language primarily
designed for simulations. The Simula 67 language - which was the
variant that Stroustrup worked with - is regarded as the first language to
support the object-oriented programming paradigm. Stroustrup found
that this paradigm was very useful for software development, however
the Simula language was far too slow for practical use.
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. Cfront would later be abandoned in 1993 after it
became difficult to integrate new features into it, namely C++ exceptions.
In 1983, 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.
In 1990, The Annotated C++ Reference Manual was released. The same
year, Borland's Turbo C++ compiler would be released as a commercial
product. Turbo C++ added a plethora of additional libraries which would
have a considerable impact on C++'s development. Although Turbo C++'s
last stable release was in 2006, the compiler is still widely used.
In mid-2011, the new C++ standard (dubbed C++11) was finished. The
Boost library project made a considerable impact on the new standard,
and some of the new modules were derived directly from the
corresponding Boost libraries. Some of the new features included regular
expression support (details on regular expressions may be found here), a
comprehensive randomization library, a new C++ time library, atomics
support, a standard threading library (which up until 2011 both C and
C++ were lacking), a new for loop syntax providing functionality similar
to foreach loops in certain other languages, the auto keyword, new
container classes, better support for unions and array-initialization lists,
and variadic templates.
Starting Microsoft Visual Studio 6.0
Once the Visual C++ Desktop appears you are ready to create the project.
After clicking the OK button, a new window will pop-up to ask the type of
Console application you want to create. The screenshot below shows the
Win32 Console Application window.
Figure 1-2. The Win32 Console Application window
After clicking the Finish button, another window will pop-out which will
show the summary information of the new project. Just click the OK
button to exit this window.
Figure 1-3. The File-New window for creating a new C++ program.
After clicking the New option of the File menu, a window will pop-out
showing the different options you wish to create. Since you will be
creating a Win32 Console Application, then you will select the C++
Source File. The name of the C++ file shall also be typed in this window.
After clicking the OK button, the next window that will come out is the
editor for which your C++ codes will be encoded. Also, compiling,
building and execution of the program is done in this window.
Write your
source code here
The lessons in this tutorial will take you from being a beginner to being
able to write real programs in C++.
The best way to learn anything is to jump right in, so let's start by
writing a simple C++ hello world program.
First Program
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World\n";
return 0;
}
Line 4: {
cout is a object from a standard C++ library that has a method used
to print strings to the standard output, normally your screen. The
compiler links code from these standard libraries to the code you have
written to produce the final executable. The "\n" is a special format
modifier that tells the method to put a line feed at the end of the line. If
there were another "cout" in this program, its string would print on the
next line.
Line 6: }
That's it. To get the most of this series of tutorials, you should get access
to both a text editor and a C++ compiler. Some instructions for doing this
are in our tutorials on compiling.
The following output window should be displayed. When I press any key,
the window will be taken down and the program will stop.
Hello World!
Press any key to continue
ENHANCEMENT ACTIVITY