Introduction to Programming and C++

Contents Previous Topic Next Topic

When C++ first came out, including a standard header was done using the syntax

#include <iostream.h>

With the C++ Standard, two changes were made:

  • Everything within the standard libraries was wrapped in a std namespace, and
  • The syntax for including standard libraries was changed.

Both these changes were made to ensure that old code continued to run:

  1. Old code would continue to include iostream.h which does not have the name space std, while
  2. Newer code could use the C++ Standard iostream.

The most significant problem with including iostream.h is that it is no longer being updated. New features will be added to the standard iostream and may only be migrated to iostream.h through the generosity of some hacker in his or her spare time.

Program 1 shows how the non-standard old iostream.h may still be used today. Program 2 shows how you can use the C++-standard iostream and still avoid explicitly using the std name space. Program 3 demonstrates how you can avoid loading all the functions in iostream through the explicit use of the std name space.

Program 1. The pre-C++ standard inclusion of the iostream header.

#include <iostream.h>

int main() {
	cout << "Hello World!" << endl;

	return 0;
}

Program 2. The C++ standard inclusion of the iostream header with the using keyword.

#include <iostream>

using namespace std;

int main() {
	cout << "Hello World!" << endl;

	return 0;
}

Program 3. The C++ standard inclusion of the iostream header specifying the std name space.

#include <iostream>

int main() {
	std::cout << "Hello World!" << std::endl;

	return 0;
}

Questions

1. Do a google search for iostream versus iostream.h. What are some of the differences between the standard and non-standard versions of iostream?


Contents Previous Topic Top Next Topic