CP II Unit II
CP II Unit II
Balagurusamy
Introduction of C++
- C++ is an object-oriented programming language.
- It was developed by Bjarne Stroustrup at AT&T Bell Laboratories in Murray Hill, New
Jersey, USA, in the early 1980’s.
- Stroustrup, an admirer of Simula67 and a strong supporter of C, wanted to combine the
best of both the languages and create a more powerful language that could support object-
oriented programming features and still retain the power and elegance of C. The result
was C++.
- C++ is an extension of C with a major addition of the class construct feature of Simula67.
Since the class was a major addition to the original C language, Stroustrup initially called
the new language ‘C with classes’. However, later in 1983, the name was changed to C+
+. The idea of C++ comes from the C increment operator ++, thereby suggesting that C++
is an augmented version of C.
- C+ + is a superset of C.
- Almost all C programs are also C++ programs.
- The most important facilities that C++ adds on to C care classes, inheritance, function
overloading and operator overloading. These features enable creating of abstract data
types, inherit properties from existing data types and support polymorphism, thereby
making C++ a truly object-oriented language.
Application of C++
- C++ is a versatile language for handling very large programs; it is suitable for virtually
any programming task including development of
o Editors
o compilers
o databases
o communication systems and
o Complex real life applications systems.
- Since C++ allow us to create hierarchy related objects, we can build special object-
oriented libraries which can be used later by many programmers.
- While C++ is able to map the real-world problem properly, the C part of C++ gives the
language the ability to get closed to the machine-level details.
- C++ programs are easily maintainable and expandable. When a new feature needs to be
implemented, it is very easy to add to the existing structure of an object.
#include<iostream.h>
int main( ) {
cout<< “C++ is better than C \n”;
return 0;
}
Program feature
- Like C, the C++ program is a collection of function.
- The above example contain only one function main( ). As usual execution begins at
main( ).
- Every C++ program must have a main( ).
- C++ is a free form language. With a few exception, the compiler ignore carriage return
and white spaces.
- Like C, the C++ statements terminate with semicolons.
Comments
- C++ introduces a new comment symbol // (double slash).
- Comment start with a double slash symbol and terminate at the end of the line.
- A comment may start anywhere in the line, and whatever follows till the end of the line is
ignored. Note that there is no closing symbol.
- The double slash comment is basically a single line comment.
- Multiline comments can be written as follows:
// This is an example of
// C++ program to illustrate
// some of its features
- The C comment symbols /*,*/ are still valid and are more suitable for multiline
comments.
- The following comment is allowed:
/* This is an example of
C++ program to illustrate
some of its features
*/
Output operator
- The statement in program 2.1
cout<< “C++ is better than C.\n”;
- Causes the string in quotation marks to be displayed on the screen.
- This statement introduces two new C++ features, cout and <<.
- The identifier cout(pronounced as C out) is a predefined object that represents the standard
output stream in C++.
-Here, the standard output stream represents the screen. It is also possible to redirect the output to
other output devices.
- The operator << is called the insertion or put to operator.
main( ){
……
Variables
- The program 2.2 uses four variables number1, number2, sum and average. They are
declared as type float by the statement.
float number1, number2, sum, average;
- All variable must be declared before they are used in the program.
Input Operator
The statement in program 2.2
cin >> number1;
- Is an input statement and causes the program to wait for the user to type in a number.
- The number keyed in is placed in the variable number1.
- The identifier cin (pronounced ‘C in’) is a predefined object in C++ that corresponds to
the standard input stream.
- Here, this stream represents the keyboard.
- The operator >> is known as extraction or get from operator.
- It extracts (or takes) the value from the keyboard and assigns it to the variable on its right
fig 2.1.
- This corresponds to a familiar scanf() operation.
- Like <<, the operator >> can also be overloaded.
- This approach is based on the concept of client-server model as shown in fig. 2.3.
- The class definition including the member functions constitute the server that provides
services to the main program known as client.
- The client uses the server through the public interface of the class.
#include<iostream.h>
main( ){
Compiler Linker Loader
cout<<“Wel-
Come”;