Monday, 22 August 2016

INTRODUCTION TO C++

C++ was developed by Bjarne Stroustrup of AT&T Bell Laboratories in the early 1980's, and is based on the C language. The name is a pun - "++" is a syntactic construct used in C (to increment a variable), and C++ is intended as an incremental improvement of C. Most of C is a subset of C++, so that most C programs can be compiled (i.e. converted into a series of low-level instructions that the computer can execute directly) using a C++ compiler.

C is in many ways hard to categorise. Compared to assembly language it is high-level, but it nevertheless includes many low-level facilities to directly manipulate the computer's memory. It is therefore an excellent language for writing efficient "systems" programs. But for other types of programs, C code can be hard to understand, and C programs can therefore be particularly prone to certain types of error. The extra object-oriented facilities in C++ are partly included to overcome these shortcomings.

1.3 ANSI/ISO C++


The American National Standards Institution (ANSI) and the International Standards Organisation (ISO) provide "official" and generally accepted standard definitions of many programming languages, including C and C++. Such standards are important. A program written only in ANSI/ISO C++ is guaranteed to run on any computer whose supporting software conforms to the the standard. In other words, the standard guarantees that standard-compliant C++ programs are portable. In practice most versions of C++ include ANSI/ISO C++ as a core language, but also include extra machine-dependent features to allow smooth interaction with different computers' operating systems. These machine dependent features should be used sparingly. Moreover, when parts of a C++ program use non-compliant components of the language, these should be clearly marked, and as far a possible separated from the rest of the program, so as to make modification of the program for different machines and operating systems as easy as possible. The four most significant revisions of the C++ standard are C++98 (1998), C++03 (2003) and C++11 (2011) and C++14 (2014). The next iteration is currently expected in 2017. Of course, it can be a challenging task for software engineers, compiler writers and lecturers (!) to keep track of all the revisions that appear in each major version of the standard. You may wish to read further about the ongoing efforts that are being made to make the widely-used g++ compiler compliant with the C++11 standard and compliant with the C++14 standard.

1.5 An Example C++ Program

Here is an example of a complete C++ program:

// The C++ compiler ignores comments which start with
// double slashes like this, up to the end of the line.

/* Comments can also be written starting with a slash
followed by a star, and ending with a star followed by
a slash. As you can see, comments written in this way
can span more than one line. */

/* Programs should ALWAYS include plenty of comments! */

/* Author: Rob Miller and William Knottenbelt
Program last changed: 30th September 2001 */

/* This program prompts the user for the current year, the user's
current age, and another year. It then calculates the age
that the user was or will be in the second year entered. */

#include <iostream>

using namespace std;

int main()
{
int year_now, age_now, another_year, another_age;

cout << "Enter the current year then press RETURN.\n";
cin >> year_now;

cout << "Enter your current age in years.\n";
cin >> age_now;

cout << "Enter the year for which you wish to know your age.\n";
cin >> another_year;

another_age = another_year - (year_now - age_now);

if (another_age >= 0) {
cout << "Your age in " << another_year << ": ";
cout << another_age << "\n";
} else {
cout << "You weren't even born in ";
cout << another_year << "!\n";
}

return 0;
}
Share:

0 comments:

Post a Comment