You are on page 1of 22

Introduction to C++

C++ is a statically typed, free-form, multiparadigm, compiled, generalpurpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell Labs as an enhancement to the C language and originally named C with Classes. It was renamed C++ in 1983.

HISTORY

Bjarne Stroustrup began work on "C with Classes" in 1979. The idea of creating a new language originated from Stroustrup's experience in programming for his Ph.D. thesis. Stroustrup found that Simula had features that were very helpful for large software development, but the language was too slow for practical use, while BCPL was fast but too lowlevel to be suitable for large software development. When Stroustrup started working in AT&T Bell Labs, he had the problem of analyzing the UNIX kernel with respect to distributed computing. Remembering his Ph.D. experience, Stroustrup set out to enhance the C language with Simula-like features. C was chosen because it was general-purpose, fast, portable and widely used. Besides C and Simula, some other languages that inspired him were ALGOL 68, Ada, CLU and ML. At first, the class, derived class, strong type checking, in-lining, and default argument features were added to C via Stroustrup's C++ to C compiler, Cfront. The first commercial implementation of C++ was released on October 14, 1985.
y

Bjarne Stroustrup

In 1983, the name of the language was changed from C with Classes to C++ (++ being the increment operator in C). New features were added including virtual functions, function name and operator overloading, references, constants, user-controlled free-store memory control, improved type checking, and BCPL style singleline comments with two forward slashes (//). In 1985, the first edition of The C++ Programming Language was released, providing an important reference to the language, since there was not yet an official standard. Release 2.0 of C++ came in 1989 and the updated second edition of The C++ Programming Language was released in 1991. New features included multiple inheritance, abstract classes, static member functions, const member functions, and protected members. In 1990, The Annotated C++ Reference Manual was published. This work became the basis for the future standard. Late addition of features included templates, exceptions, namespaces, new casts, and a Boolean type.

As the C++ language evolved, the standard library evolved with it. The first addition to the C++ standard library was the stream I/O library which provided facilities to replace the traditional C functions such as printf and scanf. Later, among the most significant additions to the standard library, were large amounts of the Standard Template Library. C++ is sometimes called a hybrid language.

USES

C++ is one of the most popular programming languages and its application domains include systems software (such as Microsoft Windows), application software, device drivers, embedded software, high-performance server and client applications, and entertainment software such as video games. Several groups provide both free and proprietary C++ compiler software, including the GNU Project, Microsoft, Intel and Embarcadero Technologies. C++ has greatly influenced many other popular programming languages, most notably C# and Java. C++ is also used for hardware design, where the design is initially described in C++, then analyzed, architecturally constrained, and scheduled to create a register-transfer level hardware description language via high-level synthesis.

ADVANTAGES

1. ) The C++ language is a structured language which supports all necessary components of a structured language such as global and local variables, parameter passing by value and reference, and function and/or procedure return values.

2.) The C and C++ compiler also supports the concept of separate compilation of source code modules and the linking of independent object modules either from standalone files or from system or local libraries. This separate compilation feature allows the programmer to recompile only the parts of an application that have changed and re-link those modified parts with existing modules to produce a new executable program.

3.) The C and C++ compiler also allows an interface to assembly language. The compiler allows blocks of code can be written in assembly and linked with blocks written in C and C++. A block of code could be a function or simply several lines of code within a larger C and C++ function. The programmer only has to use the keyword asm followed by an opening {, then the assembly code for the processor being used and then a terminating}. The assembly code can use any variable declared within the C and C++ code that follows within the scope of the assembly code.

DISADVANTAGES

1.) The language lacks strong type checking, meaning that the compiler will allow a character variable to be stored into a floating point variable without complaining. The C++ component of a C and C++ compiler has much stronger type checking and will issue warnings for such behavior. So for the programmer to receive the maximum protection against mixed type errors, it would be better to use the C++ component of the C and C++ compiler.

2.) Neither C nor C++ has bounds checking on arrays. For example, if a programmer declares an array of 50 integers, but in the program code he or she by mistake stores a value into array element 52, the compiler will not complain. The integer value was indeed stored at the fifty-second integer sized offset from the base address of the array. This lack of bounds checking can cause severe problems in some programs.

3.) In the MS-DOC and PC-DOS operating system, neither C nor C++ has any memory protection from access by pointers. A programmer can load a memory address of any place in memory into a pointer and through that pointer retrieve or set the value at that memory address. This feature can cause the DOS operating system to re- boot, hang-up, or crash completely. In addition, a programmer that is unfamiliar with the use of pointers can, in MS/PC-DOS, because his or her hard disk drive to crash, be reformatted, or destroyed. The same also holds true for the video display on a DOS machine. Fortunately, mini-computer operating systems, such as UNIX, have built-in memory protection that prevents such dangerous happenings as stated above.

4.) Also, C and C++ do not have sophisticated string and record handling capabilities. Strings must be handled with a series of functions supplied in the standard C and C++ libraries. With C++, sophisticated String objects can be created as well as advanced record management schemes.

The majority of C++ programs are composed of functions. All functions have the following general form:
Function-Return-Type Function-Name(argument list) { local variable declarations; body of the function; }

C++ Sample Program

Problem:
The LeVan Car Rental company charges $0.25/mile if the total mileage does not exceed 100. If the total mileage is over 100, the company charges $0.25/mile for the first 100 miles, then it charges $0.15/mile for any additional mileage over 100.Write a program so that if the clerk enters the number of miles, the program would display the total price owed

#include <iostream> using namespace std; void main() { unsigned int Miles; const double LessThan100 = 0.25; const double MoreThan100 = 0.15; double PriceLessThan100, PriceMoreThan100, TotalPrice; cout << "Enter the number of miles: "; cin >> Miles; if(Miles <= 100) { PriceLessThan100 = Miles * LessThan100; PriceMoreThan100 = 0; } else { PriceLessThan100 = 100 * LessThan100; PriceMoreThan100 = (Miles - 100) * MoreThan100; } TotalPrice = PriceLessThan100 + PriceMoreThan100; cout << "\nTotal Price = $" << TotalPrice << "\n\n"; }

You might also like