[go: up one dir, main page]

0% found this document useful (0 votes)
173 views5 pages

GENERALITY

C++ is an object-oriented extension of C that provides support for object-oriented programming concepts like encapsulation, inheritance, and polymorphism. Standard C++ is the standardized version accepted by major compilers to ensure portability. While C++ code can be written with both object-oriented and procedural styles, OOP concepts like classes are emphasized. C++ is mostly compatible with C but has some differences like mandatory function prototypes and return types.

Uploaded by

vinaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
173 views5 pages

GENERALITY

C++ is an object-oriented extension of C that provides support for object-oriented programming concepts like encapsulation, inheritance, and polymorphism. Standard C++ is the standardized version accepted by major compilers to ensure portability. While C++ code can be written with both object-oriented and procedural styles, OOP concepts like classes are emphasized. C++ is mostly compatible with C but has some differences like mandatory function prototypes and return types.

Uploaded by

vinaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

GENERALITY

An overview of C++
C++ is the object oriented extension of C. As for C there is an ANSI/ISO standard ( final draft 1998) for the C++ programming language. This will ensure that the C++ code is portable between
computers.
The C++ programming language teach here is the Standard C++. This is the version of C++ created by the ANSI/ISO 2 standardisation committee. The Standard C++ contains several enhancements not
found in the traditional C++. Thus, Standard C++ is a superset of traditional C++.
Standard C++ is the one that is currently accepted by all major compilers. Therefore, you can be confident that what you learn here will also apply in the future.
However, if you are using an older compiler it might not support one or more of the features that are specific to Standard C++. This is important because two recent additions to the C++ language affect
every program you will write. If you are using an older compiler that does not accept these knew features, don’t worry. There is an easy workaround, as you will in a later paragraph.
Since C++ was invented to support object-oriented programming. OOP concepts will be reminded. As you will see, many features of C++ are related to OOP in a way or another. In fact the theory of
OOP permeates C++. However, it is important to understand that C++ can be used to write programs that are and are not object oriented. How you use C++ is completely up to you.
A few comments about the nature and form of C++ are in order. For most part C++ programs look like C programs. Like a C program, a C++ program begins execution at main( ). To include
command-line arguments, C++ uses the same argc, argv convention that C uses. Although C++ defines its own, object-oriented library. It also supports all the functions in the C standard library. C++
uses the same control structures as C. C++ includes all the build-in data types defined by C programming.

Object Oriented Programming (OOP)


Although structured programming has yielded excellent results when applied to moderately complex programs, even it fails at some point, after a program reaches a certain size. To allow more complex
programs to be written, object-oriented programming has been invented. OOP takes the best of the ideas in structured programming and combines them with powerful new concepts that allow you to
organise your programme more efficiently.
Object oriented programming encourage you to decompose a problem into its constituent parts.
Each component becomes a self-contained object that contains its own instructions and data that relate to that object. In this way, complexity is reduced and the programmer can manage larger program.
All OOP languages, including C++, share three common defining traits.

Encapsulation
Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps them both safe from outside. In an object-oriented language, code and data can be combined in such a
way that a self-contained ‘black box’ is created. When code and data are link together in this fashion , an object is created:
Within an object, code, data, or both may be private to that object or public.
Private code or data is known to and accessible only by another part of the object (i.e. cannot be accessed by a piece of the program that exists outside the object.
Public code or data can be accessed by other parts of the program even though it is defined within an object.
Public parts of an object are used to provide a controlled interface to the private elements of the object.

Data 2 ANSI: American National Standards Institute ISO: International Standard Organisation
Methods: code OBJECT Standard C++ programming 4
object, you are creating a new data type. Each specific instance of this data type is a compound
variable.

Polymorphism
Polymorphism is the quality that allows one name to be used for two or more related but technically different
purposes.

Polymorphism allows one name to specify a general class of actions. Within a general class of actions, the
specific action to be applied is determined by the type of data. For example, in C, the absolute value action
requires three distinct function names: abs( ) for integer, labs( ) for long integer, and fabs( ) for floating-point
value. However in C++, each function can be called by the same name, such as abs( ). The type of data used to
call the function determines which specific version of the function is actually executed.

In C++ it is possible to use one function name for many different purposes. This type of polymorphism is called
function overloading.

Polymorphism can also be applied to operators. In that case it is called operator overloading.

More generally the concept of polymorphism is characterised by the idea ‘one interface, multiple methods’.
The key point to remember about polymorphism is that it allows you to handle greater complexity by allowing
the creation of standard interfaces to related activities.

Inheritance
Inheritance is the process by which one object can acquire the properties of another. An object can inherit a
general set of properties to which it can add those features that are specific only to itself.

Inheritance is important because it allows an object to support the concept of hierarchical classification. Most
information is made manageable by hierarchical classification.

The child class inherits all those qualities associated with the parent and adds to them its own defining
characteristics.

Differences between C and C++


Although C++ is a subset of C, there are some small differences between the two, and few are worth knowing
from the start.

First, in C, when a function takes no parameters, its prototype has the word void inside its function parameter
list. For example if a function f1( ) takes no parameters (and returns a char), its prototype will look like this:
char f1(void); /* C version */
In C++, the void is optional. Therefore the prototype for f1( ) is usually written as:
char f1( ); //C++ version
this means that the function has no parameters. The use of void in C++ is not illegal; it is just redundant.
Remember these two declarations are equivalent.

Another difference between C and C++ is that in a C++ program, all functions must be prototyped. Remember
in C prototypes are recommended but technically optional. As an example from the previous section show, a
member function’s prototype contained in a class also serves as its general prototype, and no other separate
prototype is required.

A third difference between C and C++ is that in C++, if a function is declared as returning a value, it must return
a value. That is, if a function has a return type other than void, any return statement within the function must
contain a value. In C, a non void function is not required to actually return a value. If it doesn’t, a garbage value
is ‘returned’.

In C++, you must explicitly declare the return type of all functions.
Another difference is that in C, local variables can be declared only at the start of a block, prior to any ‘action’
statement. In C++, local variables can be declared anywhere. Thus, local variables can be declared close to
where they are first use to prevent unwanted side effects.

C++ defines the bool date type, which is used to store Boolean values. C++ also defines the keywords true and
false, which are the only values that a value of type bool can have.

In C, a character constant is automatically elevated to an integer, whereas in C++ it is not.

In C, it is not an error to declare a global variable several times, even though it is bad programming practice. In
C++, this is an error.

In C an identifier will have at least 31 significant characters. In C++, all characters are considered significant.
However, from practical point of view, extremely long identifiers are unwieldy and seldom needed.

In C, you can call main( ) from within the program. In C++, this is not allowed..

In C, you cannot take the address of a register variable. In C++, you can.

In C, the type wchar_t is defined with a typedef. In C++, wchar_t is a keyword.

Differences between C++ and Standard C++


The traditional C++ and the Standard C++ are very similar. The differences between the old-style and the
modern style codes involve two new features: new-style headers and the namespace statement. Here an
example of a do-nothing program that uses the old style,
/* A traditional-style C++ program */
#include < iostream.h >
int main( ) {
/* program code */
return 0;
}

New headers
Since C++ is build on C, the skeleton should be familiar, but pay attention to the #include statement. This
statement includes the file iostream.h, which provides support for C++’s I/O system. It is to C++ what stdio.h is
to C.

Here the second version that uses the modern style,


/*
A modern-style C++ program that uses
the new-style headers and namespace
*/
#include < iostream>
using namespace std;
int main( ) {
/* program code */
return 0;
}
First in the #include statement, there is no .h after the name iostream. And second, the next line, specifying a
namespace is new.

The only difference is that in C or traditional C++, the #include statement includes a file (file-name.h). While
the Standard C++ do not specify filenames. Instead the new style headers simply specify standard identifiers
that might be map to files by the compiler, but they need not be. New headers are abstractions that simply
guaranty that the appropriate prototypes and definitions required by the C++ library have been declared.

Since the new-style header is not a filename, it does not have a .h extension. Such header consists only of the
header name between angle brackets:
< iostream >
< fstream >
< vector >
< string >
Standard C++ supports the entire C function library, it still supports the C-style header files associated with the
library. That is, header files such as stdio.h and ctype.h are still available. However Standard C++ also defines
new-style headers that you can use in place of these header files. For example,

Old style header files Standard C++ headers


< math.h > < cmath >
< string.h > < cstring >
Remember, while still common in existing C++ code, old-style headers are obsolete.

Namespace
When you include a new-style header in your program, the contents of that header are contained in the std
namespace. The namespace is simply a declarative region. The purpose of a namespace is to localise the
names of identifiers to avoid name collision. Traditionally, the names of library functions and other such items
were simply placed into the global namespace (as they are in C). However, the contents of new-style headers
are place in the std namespace. Using the statement,
using namespace std;
brings the std namespace into visibility. After this statement has been compiled, there is no difference working
with an old-style header and a new-style one.

Working with an old compiler


If you work with an old compiler that does not support new standards: simply use the old-style header and
delete the namespace statement, i.e.

replace: by:
#include < iostream> #include < iostream.h >

You might also like