Object-Based Programming Is The Style of Programming That Primarily Supports
Object-Based Programming Is The Style of Programming That Primarily Supports
Object-Based Programming Is The Style of Programming That Primarily Supports
object-oriented. Depending upon the features they support, they can be classified into the
following two categories:
C+ + is a superset of C. Almost all c programs are also C++ programs. However, there
are a few minor differences that will prevent a c program to run under C++ complier. We
shall see these differences later as and when they are encountered.
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.
• 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.
• It is expected that C++ will replace C as a general-purpose language in the near
future.
Program 1.10.1
This simple program demonstrates several C++ features.
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.
1.10.2 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
*/
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.
#include <iostream>
The #include directive instructs the compiler to include the contents of the file enclosed
within angular brackets into the source file. The header file iostream.h should be
included at the beginning of all programs that use input/output statements.
1.10.5 Namespace
Namespace is a new concept introduced by the ANSI C++ standards committee. This
defines a scope for the identifiers that are used in a program. For using the identifier
defined in the namespace scope we must include the using directive, like
Here, std is the namespace where ANSI C++ standard class libraries are defined. All
ANSI C++ programs must include this directive. This will bring all the identifiers defined
in std to the current global scope. Using and namespace are the new keyword of C++.
In C++, main () returns an integer value to the operating system. Therefore, every main ()
in C++ should end with a return (0) statement; otherwise a warning an error might occur.
Since main () returns an integer type for main () is explicitly specified as int. Note that
the default return type for all function in C++ is int. The following main without type and
return will run with a warning:
main ()
{
…………..
………….
}
1.11 More C++ Statements
Let us consider a slightly more complex C++ program. Assume that we should like to
read two numbers from the keyboard and display their average on the screen. C++
statements to accomplish this is shown in program 1.11.1
Int main()
Return 0;
} //end of example
Program 1.11.1
1.11.1 Variables
The program uses four variables number1, number2, sum and average. They are declared
as type float by the statement.
All variable must be declared before they are used in the program.