0% found this document useful (0 votes)
3 views

Module 1 (Lesson1)

This document serves as an introduction to C++, a versatile programming language developed by Bjarne Stroustrup, which supports various programming paradigms. It outlines the history of C++, its applications, and provides guidance on setting up a C++ development environment using Microsoft Visual Studio. Additionally, it includes a simple 'Hello World' program example to demonstrate basic C++ syntax and execution.

Uploaded by

Bangkal Ml
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Module 1 (Lesson1)

This document serves as an introduction to C++, a versatile programming language developed by Bjarne Stroustrup, which supports various programming paradigms. It outlines the history of C++, its applications, and provides guidance on setting up a C++ development environment using Microsoft Visual Studio. Additionally, it includes a simple 'Hello World' program example to demonstrate basic C++ syntax and execution.

Uploaded by

Bangkal Ml
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Lesson 1:

Introduction to C++

Welcome to the fascinating world of C++. C++ is a powerful general-purpose


programming language. It can be used to develop operating systems, browsers, games,
and so on. C++ supports different ways of programming like procedural, object-oriented,
functional, and so on. This makes C++ powerful as well as flexible.

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

At the end of this lesson, you shall be able to:

 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++?

 C++ is a cross-platform language that can be used to create high-


performance applications.

 C++ was developed by Bjarne Stroustrup, as an extension to the C


language.

 C++ gives programmers a high level of control over system


resources and memory.

 C++ is a influential general-purpose programming language. It can


be used to develop operating systems, browsers, games, and etc.

Why Use C++

 C++ is one of the world's most popular programming languages.

 C++ can be found in today's operating systems, Graphical User


Interfaces, and embedded systems.

 C++ is an object-oriented programming language which gives a


clear structure to programs and allows code to be reused, lowering
development costs.

 C++ is portable and can be used to develop applications that can


be adapted to multiple platforms.

 C++ is fun and easy to learn!

 As C++ is close to C# and Java, it makes it easy for programmers


to switch to C++ or vice versa
History Of 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.

Shortly thereafter, he began work on "C with Classes", which as the


name implies was meant to be a superset of the C language. His goal was
to add object-oriented programming into the C language. His language
included classes, basic inheritance, inlining, default function arguments,
and strong type checking in addition to all the features of the C
language.

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 1985, Stroustrup's reference to the language entitled The C++


Programming Language was published. The language was updated again
in 1989 to include protected and static members, as well as inheritance
from several classes.

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 1998, the C++ standards committee published the first international


standard for C++ ISO/IEC 14882:1998, which would be informally
known as C++98. The Annotated C++ Reference Manual was said to be a
large influence in the development of the standard. The Standard
Template Library, which began its conceptual development in 1979, was
also included. In 2003, the committee responded to multiple problems
that were reported with their 1998 standard, and revised it accordingly.
The changed language was dubbed C++03.

In 2005, the C++ standards committee released a technical report


(dubbed TR1) detailing various features they were planning to add to the
latest C++ standard. The new standard was informally dubbed C++0x as
it was expected to be released sometime before the end of the first
decade. Ironically, however, the new standard would not be released
until mid-2011. Several technical reports were released up until then,
and some compilers began adding experimental support for the new
features.

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

To start Microsoft Visual Studio, follow the steps below:


 Click on Start
 Choose: Programs
Microsoft Visual Studio 6.0
Microsoft Visual C++ 6.0

Once the Visual C++ Desktop appears you are ready to create the project.

Creating a New Visual C++ Project

Figure 1-1. The New Project window

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.

Creating A New Program In An Existing Visual C++ Project

We now want to code the C++ program. To do this we have to create a


new file. Click on File, select and click New.

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.

Figure 1-4. The file type 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

Figure 1-5. The program editor window.


Compiling and Running Your Visual C++ 6.0 Project

Compiling and linking your project:

Saving the project:


Click on File/Save All.

Closing the project:


Click on File/Close Workspace.

What if I have compiler errors?


Your First Program

The lessons in this tutorial will take you from being a beginner to being
able to write real programs in C++.

C++ is a compiled language. The C++ compiler is a program that reads


source code, which is the C++ code written by a programmer, and it
produces an executable or binary file that in a format that can be read
and executed (run) by a computer. The source file is a plain text file
containing your code. The executable file consists of machine code, 1's
and 0's that are not meant to be understood or read by people, but only
by computers.

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 1: #include <iostream>

As part of compilation, the C++ compiler runs a program called the


C++ preprocessor. The preprocessor is able to add and remove code from
your source file. In this case, the directive #include tells the preprocessor
to include code from the file iostream. This file contains declarations for
functions that the program needs to use, as well as available classes.

Line 2: using namespace std

C++ supports the concept of name spaces. Essentially, this allows


variables to localized to certain regions of code. The command using
namespace std allows all objects and functions from the standard input
and output library to be used within this program without explicit
qualifications. Namespaces are an advanced concept. For now, all you
need to know is that this line allows simple use of the standard library.

Line 3: int main()

This statement declares the main function. A C++ program can


contain many functions but must always have one main function. A
function is a self-contained module of code that can accomplish some
task. Functions are examined in a later tutorial. The "int" specifies the
return type of main to be an integer. An explicit value may be returned
using a return statement. In this case, 0 is returned be default.

Line 4: {

This opening bracket denotes the start of the program.


Line 5: cout << "Hello World\n";

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: }

This closing bracket denotes the end of the program.

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.

To run your program:

Click on Build/Execute HelloWorld.exe.

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

Read in advance the Flowcharting concept and be familiar with different


symbol and its uses.

You might also like