C++ Language
C++ Language for
creating large-scale applications. C++ is a superset of the C language. A
related programming language, Java, is based on C++ but optimized for the
distribution of program objects in a network such as the Internet. Java is
somewhat simpler and easier to learn than C++ and has characteristics that give
it other advantages over C++. However, both languages require a considerable
amount of study.
C++, pronounced C plus plus is a programming language that was built off the C language. The syntax of C++ is nearly identical to C, but it has object-oriented features, which allow the programmer to create objects within the code. This makes programming easier, more efficient, and some would even say, more fun. Because of the power and flexibility of the language, most software programs today are written in C++.
C++ pronounced see plus plus is a
general purpose programming language that is free-form and compiled. It is regarded as an intermediate-level language, as
it comprises both high-level and low-level language features. It provides imperative, object-oriented and generic programming
features.
C++ is one of the
most popular programming languages
and is implemented on a wide
variety of hardware and operating system platforms. As an efficient performance
driven programming language it is used in systems software, application software, and
device drivers, embedded software, high-performance server and client
applications, and entertainment software such as video
games.
Various entities provide both open source and proprietary C++ compiler software, including
the FSF,
LLVM,
Microsoft and Intel.
C++ has influenced many other programming languages, for example, C# and Java.
It was developed by Bjarne Stroustrup starting in
1979 at Bell Labs, C++ was
originally named C with Classes, adding object-oriented features, such as classes, and other enhancements to
the C programming language. The language was renamed C++ in 1983, as a pun
involving the increment operator. It began as enhancements to C, first adding classes, then virtual
functions, operator
overloading, multiple inheritance, templates and exception handling,
alongside changes to the type system and other features.
C++ is standardized
by the International
Organization for Standardization ,
which the latest and current having being ratified and published by ISO in. The C++
programming language was initially standardized in 1998 as, which was amended
by the 2003 technical corrigendum,. The current standard C++11 supersedes these, with new
features and an enlarged standard library.
Bjarne Stroustrup,
a Danish and British trained computer scientist, began his work on C++'s
predecessor "C with Classes"
in 1979. The motivation for creating a new language originated from Stroustrup
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 low-level
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's influences, other languages also influenced C++, CLU and ML. At
first, the class, derived class, strong typing, inlining, and default argument
features were added to C via Stroustrup C with Classes to C compiler.
C++ is a statically
typed, compiled, general-purpose, case-sensitive, free-form programming
language that supports procedural, object-oriented, and generic programming. C++
is regarded as a middle-level language, as it comprises a combination of both
high-level and low-level language features. C++ was developed by Bjarne Stroustrup
starting in 1979 at Bell Labs in Murray Hill, New Jersey, as an enhancement to
the C language and originally named C with Classes but later it was renamed C++
in 1983. C++ is a super set of C, and that virtually any legal C program is a
legal C++ program.
Use of C++
C++ is used by hundreds of thousands of programmers in essentially every application domain. C++ is being highly used to write device drivers and other software that rely on direct manipulation of hardware under real time constraints++ is widely used for teaching and research because it is clean enough for successful teaching of basic concepts. Anyone who has used either an Apple Macintosh or a PC running Windows has indirectly used C++ because the primary user interfaces of these systems are written in C++.
Operators and operator
overloading
Operator
|
Symbol
|
Scope resolution operator
|
::
|
Conditional operator
|
?:
|
dot operator
|
.
|
Member selection operator
|
.*
|
"sizeof" operator
|
sizeof
|
"typeid" operator
|
typeid
|
C++
provides more than 35 operators, covering basic arithmetic, bit manipulation,
indirection, comparisons, logical operations and others. Almost all operators
can be overloaded for user-defined types, with a few notable exceptions such as
member access .and.* as well as the conditional operator. The rich set of over
loadable operators is central to using user created types in C++ as well and as
easily as built in types. The over loadable operators are also an essential
part of many advanced C++ programming techniques, such as smart pointers.
Overloading an operator does not change the precedence of calculations
involving the operator, nor does it change the number of operands that the
operator uses any operand may however be ignored by the operator, though it
will be evaluated prior to execution. Overloaded "&&" and
"||" operators lose their short-circuit evaluation property.
Memory management
C++ supports four types of memory
management:
- Static memory allocation. A static variable is assigned a value at compile-time, and allocated storage in a fixed location along with the executable code. These are declared with the "static" keyword in the sense of static storage, not in the sense of declaring a class variable.
- Automatic memory allocation. An automatic variable is simply declared with its class name, and storage is allocated on the stack when the value is assigned. The constructor is called when the declaration is executed, the destructor is called when the variable goes out of scope, and after the destructor the allocated memory is automatically freed.
- Dynamic memory allocation. Storage can be dynamically allocated on the heap using manual memory management - normally calls to new and delete though old-style C calls such as malloc () and free() are still supported.
- With the use of a library, garbage collection is possible. The Boehm garbage collector is commonly used for this purpose.
The fine control over memory management
is similar to C, but in contrast with languages that intend to hide such
details from the programmer, such as Java, Perl, PHP, and Ruby.
Objects
C++
introduces object-oriented programming OOP features to C. It offers classes,
which provide the four features commonly present in OOP and some non-OOP
languages, abstraction, encapsulation,
inheritance, and polymorphism. One distinguishing feature of C++ classes
compared to classes in other programming languages is support for deterministic
destructors,
Inheritance
Inheritance
allows one data type to acquire properties of other data types. Inheritance
from a base class may be declared as public, protected, or private. This access
specified determines whether unrelated and derived classes can access the inherited
public and protected members of the base class. Only public inheritance
corresponds to what is usually meant by inheritance.
The other two forms are much less frequently used. If the access specified is
omitted, a "class" inherits privately, while a struct inherits publicly. Base classes may be declared as virtual;
this is called virtual inheritance. Virtual inheritance ensures that only one
instance of a base class exists in the inheritance graph, avoiding some of the
ambiguity problems of multiple inheritance.
Multiple
inheritances is a C++ feature not found in most other languages, allowing a
class to be derived from more than one base class, this allows for more
elaborate inheritance relationships. A Flying
Cat class can inherit from both "Cat" and Flying Mammal. Some other languages, such as C# or Java, accomplish
something similar although more limited by allowing inheritance of multiple
interfaces while restricting the number of base classes to one interfaces,
unlike classes, provide only declarations of member functions, no implementation
or member data. An interface as in C# and Java can be defined in C++ as a class
containing only pure virtual functions, often known as an abstract base class
or ABC. The member functions of such
an abstract base class are normally explicitly defined in the derived class not
inherited implicitly. C++ virtual inheritance exhibits an ambiguity resolution
feature called dominance.
What is C++:
C++ is a programming language. It literally means increased
C, reflecting its nature as an evolution of the C language. A high-level
programming language developed by Bjarne
Stroustrup at Bell Labs. C++ adds object-oriented features to its
predecessor, C. C++ is one of the most popular programming language for
graphical applications, such as those that run in Windows and Macintosh environments.
What is OOP: Object-oriented programming:
It is a programming model that treats programming from a
perspective where each component is considered an object, with its own
properties and methods, replacing or complementing structured programming
paradigm, where the focus was on procedures and parameters. C++ fully
supports object-oriented programming, including the four pillars of
object-oriented development:
1.
Encapsulation
2.
Data hiding
3.
Inheritance
4.
Polymorphism
What is ANSI-C++:
ANSI-C++ is the name by which the international ANSI/ISO
standard for the C++ language is known. But before this standard was published,
C++ was already widely used and therefore there is a lot of code out there written
in pre-standard C++. Referring to ANSI-C++ explicitly differentiates it from
pre-standard C++ code, which is incompatible in some ways.
Definition - What does
C++ Programming Language mean:
C++ is an
object oriented programming OOP language, developed by Bjarne Stroustrup, and
is an extension of C language. It is therefore possible to code C++ in a C
style or object-oriented style. In certain scenarios, it can be coded in
either way and is thus an effective example of a hybrid language.
C++ is a
general purpose object oriented programming language. It is considered to be an
intermediate level language, as it encapsulates both high and low level
language features. Initially, the language was called C with classes as it had all properties of C language with an
additional concept of 'classes’. However, it was renamed to C++ in 1983. It is
pronounced C-Plus-Plus.
Explains C++ Programming Language
C++ is one of the most
popular languages primarily utilized with system/application software, drivers,
client-server applications and embedded firmware.
The main highlight of C++ is a collection of pre-defined classes, which are data types that can be instantiated multiple times. The language also facilitates declaration of user defined classes. Classes can further accommodate member functions to implement specific functionality. Multiple objects of a particular class can be defined to implement the functions within the class. Objects can be defined as instances created at run time. These classes can also be inherited by other new classes which take in the public and protected functionalists by default.
C++ includes several operators such as comparison, arithmetic, bit manipulation, logical operators etc. One of the most attractive features of C++ is that it enables the overloading of certain operators such as addition. A few of the essential concepts within C++ programming language include polymorphism, virtual and friend functions, templates, namespaces and pointers.
The main highlight of C++ is a collection of pre-defined classes, which are data types that can be instantiated multiple times. The language also facilitates declaration of user defined classes. Classes can further accommodate member functions to implement specific functionality. Multiple objects of a particular class can be defined to implement the functions within the class. Objects can be defined as instances created at run time. These classes can also be inherited by other new classes which take in the public and protected functionalists by default.
C++ includes several operators such as comparison, arithmetic, bit manipulation, logical operators etc. One of the most attractive features of C++ is that it enables the overloading of certain operators such as addition. A few of the essential concepts within C++ programming language include polymorphism, virtual and friend functions, templates, namespaces and pointers.
C++ Classes and Objects
The main purpose of
C++ programming is to add object orientation to the C programming language and
classes are the central feature of C++ that supports object-oriented
programming and are often called user-defined types.
A class is used to
specify the form of an object and it combines data representation and methods
for manipulating that data into one neat package. The data and functions within
a class are called members of the class.
C++ Class Definitions:
When you define a
class, you define a blueprint for a data type. This doesn't actually define any
data, but it does define what the class name means, that is, what an object of
the class will consist of and what operations can be performed on such an
object.
A class definition
starts with the keyword class followed by the class name; and the class
body, enclosed by a pair of curly braces. A class definition must be followed
either by a semicolon or a list of declarations. For example, we defined the
Box data type using the keyword class as follows,
class Box
{
public:
double length;
double breadth;
double height;
};
The keyword “public”
determines the access attributes of the members of the class that follow it. A
public member can be accessed from outside the class anywhere within the scope
of the class object. You can also specify the members of a class as “private”
or “protected” which we will discuss in a sub-section.
Define C++ Objects:
A class provides the
blueprints for objects, so basically an object is created from a class. We
declare objects of a class with exactly the same sort of declaration that we
declare variables of basic types. Following statements declare two objects of
class Box:
Box Box1;
Box Box2;Both of the objects Box1 and Box2 will have their own copy of data members.
Classes & Objects in Detail:
So far, you have got
very basic idea about C++ Classes and Objects. There are further interesting
concepts related to C++ Classes and Objects which we will discuss in various
sub-sections listed below,
Concept
|
Description
|
Class member functions
|
A member function of a class is a function that
has its definition or its prototype within the class definition like any
other variable.
|
Class access modifiers
|
A class member can be defined as public, private
or protected. By default members would be assumed as private.
|
Constructor & destructor
|
A class constructor is a special function in a
class that is called when a new object of the class is created. A destructor
is also a special function which is called when created object is deleted.
|
C++ copy constructor
|
The copy constructor is a constructor which
creates an object by initializing it with an object of the same class, which
has been created previously.
|
C++ friend functions
|
A friend function is permitted full access
to private and protected members of a class.
|
C++ inline functions
|
With an inline function, the compiler tries to
expand the code in the body of the function in place of a call to the
function.
|
The this pointer in C++
|
Every object has a special pointer this
which points to the object itself.
|
Pointer to C++ classes
|
A pointer to a class is done exactly the same way
a pointer to a structure is. In fact a class is really just a structure with
functions in it.
|
Static members of a class
|
Both data members and function members of a class
can be declared as static.
|
C++ Definitions
A definition is a
unique specification of an object or variable, function, class, or enumerator.
Because definitions must be unique, a program can contain only one definition
for a given program element. There can be a many-to-one correspondence between
declarations and definitions. There are two cases in which a program element
can be declared and not defined,
1.
A function is
declared but never referenced with a function call or with an expression that
takes the function's address.
2.
A class is used
only in a way that does not require its definition be known. However, the class
must be declared.
The C++ programming
language allows programmers to separate program-specific data types through the
use of classes. Classes define types of data structures and the functions that
operate on those data structures. Instances of these data types are known as
objects and can contain member variables, constants, member functions, and
overloaded operators defined by the programmer.
#include
#include
using namespace std;
class person
{
public:
string name;
int number;
};
void main()
{
person
obj;
cout<<"Enter the Name :";
cin>>obj.name;
cout<<"Enter the Number
:";
cin>>obj.number;
cout << obj.name << ":
" << obj.number << endl;
getch();
return 0;
}
Output:
Enter the Name :Byron
Enter the Number :100
Byron: 100
Definition of Program
A computer program is a
set of instructions for a computer to perform a specific task. Programs
generally fall into these categories applications, utilities or services.
Programs are written in a programming language
See What is a Programming Language then translated into machine
code by a compiler and linker so that the computer can execute it directly or run
it line by line by an interpreter program. Popular scripting languages like Visual Basic in Microsoft Office are
interpreted.
What does C++ mean:
C++ is a general-purpose programming language with high-level and low-level
capabilities. It is a statically typed, free-form, multi-paradigm, usually
compiled language supporting procedural programming, data abstraction,
object-oriented programming, and generic programming. C++ is regarded as a mid-level language. This indicates
that C++ comprises a combination of both
high-level and low-level language features. As the C++ language evolved, a standard library
also 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, was the
Standard Template Library. It can get complicated
C++ Basic
When we consider a
C++ program, it can be defined as a collection of objects that communicate via
invoking each others methods. Let us now briefly look into what do class,
object, methods and instant variables mean.
·
Object - Objects
have states and behaviors. Example: A dog has states - color, name, breed as
well as behaviors - wagging, barking, and eating. An object is an instance of a
class.
·
Class - A class
can be defined as a template/blueprint that describes the behaviors/states that
object of its type support.
·
Methods - A method
is basically a behavior. A class can contain many methods. It is in methods
where the logics are written, data is manipulated and all the actions are
executed.
·
Instant Variables - Each
object has its unique set of instant variables. An object's state is created by
the values assigned to these instant variables.
C++ Keywords:
asm
|
else
|
new
|
this
|
auto
|
enum
|
operator
|
throw
|
bool
|
explicit
|
private
|
true
|
break
|
export
|
protected
|
try
|
case
|
extern
|
public
|
typedef
|
catch
|
false
|
register
|
typeid
|
char
|
float
|
reinterpret_cast
|
typename
|
class
|
for
|
return
|
union
|
const
|
friend
|
short
|
unsigned
|
const_cast
|
goto
|
signed
|
using
|
continue
|
if
|
sizeof
|
virtual
|
default
|
inline
|
static
|
void
|
delete
|
int
|
static_cast
|
volatile
|
do
|
long
|
struct
|
wchar_t
|
double
|
mutable
|
switch
|
while
|
dynamic_cast
|
namespace
|
template
|
A simple C++ program
Let's use the code
below to find what makes up a very simple C++ program - one that simply prints
"Hello World!" and stops. Adjust your browser so you can
see the text window below the code. Then point your mouse at different
statements in the program.
Output:
Hello World
C++ Program in Perfect Number
#include
#include
void main()
{
clrscr();
int i=1, u=1,
sum=0;
while(i<=500)
{
while(u<=500)
{
if(u
{
if(i%u==0
)
sum=sum+u;
}
u++;
}
if(sum==i)
{
cout<
}
i++;
u=1; sum=0;
}
getch();
}
Output:
6 is a
perfect number.
28 is a
perfect number.
496 is a
perfect number.
No comments:
Post a Comment