Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 23

Features of OOPs are the following:

Encapsulation
Data abstraction

Inheritance

Polymorphism

Message passing

Extensibility

Persistence

Delegation

Genericity

Multiple Inheritance

Objects:

Objects are the basic run-time entities in an object-oriented system. Every object is
associated with data and functions which define meaningful operations on that object.
What is Encapsulation
It is a mechanism that associates the code and the data it manipulates into a single unit to
and keeps them safe from external interference and misuse. In C++ this is supported by
construct called class. An instance of an object is known as object which represents a real
world entity

constructor
A constructor takes the same name as the class name.
The programmer cannot declare a constructor as virtual or static, nor can the programmer
declare a constructor as const, volatile, or const volatile.
No return type is specified for a constructor.
The constructor must be defined in the public. The constructor must be a public member.

Overloading of constructors is possible

Basic concepts of OOPS

Objects
Classes

Inheritance

Data Abstraction

Data Encapsulation

Polymorphism

Overloading

Reusability

Objects:
Object is the basic unit of object-oriented programming. Objects are identified by its unique
name. An object represents a particular instance of a class. There can be more than one instance
of an object. Each instance of an object can hold its own relevant data.
An Object is a collection of data members and associated member functions also
known as methods.

Classes:
Classes are data types based on which objects are created. Objects with similar properties and
methods are grouped together to form a Class. Thus a Class represent a set of individual objects.
Characteristics of an object are represented in a class as Properties. The actions that can be
performed by objects becomes functions of the class and is referred to as Methods.
For example consider we have a Class of Cars under which Santro Xing, Alto and WaganR
represents individual Objects. In this context each Car Object will have its own, Model, Year of
Manufacture, Colour, Top Speed, Engine Power etc., which form Properties of the Car class and
the associated actions i.e., object functions like Start, Move, Stop form the Methods of Car
Class.

No memory is allocated when a class is created. Memory is allocated only when an object is
created, i.e., when an instance of a class is created.

Inheritance:
Inheritance is the process of forming a new class from an existing class or base class. The base
class is also known as parent class or super class, The new class that is formed is called derived
class. Derived class is also known as a child class or sub class. Inheritance helps in reducing the
overall code size of the program, which is an important concept in object-oriented programming.

Data Abstraction:
Data Abstraction increases the power of programming language by creating user defined data
types. Data Abstraction also represents the needed information in the program without presenting
the details.

Data Encapsulation:
Data Encapsulation combines data and functions into a single unit called Class. When using Data
Encapsulation, data is not accessed directly; it is only accessible through the functions present
inside the class. Data Encapsulation enables the important concept of data hiding possible.

Polymorphism:
Polymorphism allows routines to use variables of different types at different times. An operator
or function can be given different meanings or functions. Polymorphism refers to a single
function or multi-functioning operator performing in different ways.

Overloading:
Overloading is one type of Polymorphism. It allows an object to have different meanings,
depending on its context. When an exiting operator or function begins to operate on new data
type, or class, it is understood to be overloaded.

Reusability:
This term refers to the ability for multiple programmers to use the same written and debugged
existing class of data. This is a time saving device and adds code efficiency to the language.
Additionally, the programmer can incorporate new features to the existing class, further
developing the application and allowing users to achieve increased performance. This time
saving feature optimizes code, helps in gaining secured applications and facilitates easier
maintenance on the application.

The implementation of each of the above object-oriented programming features for C++ will be
highlighted in later sections.

Variable, Constants and Data types in C++


Variables
A variable is the storage location in memory that is stored by its value. A variable is identified or
denoted by a variable name. The variable name is a sequence of one or more letters, digits or
underscore, for example: character _

Constants
Constants have fixed value. Constants, like variables, contain data type. Integer constants are
represented as decimal notation, octal notation, and hexadecimal notation. Decimal notation is
represented with a number. Octal notation is represented with the number preceded by a zero
character. A hexadecimal number is preceded with the characters 0x.

C++ Objects and Classes

Access specifiers:
Access specifiers are used to identify access rights for the data and member functions of the
class. There are three main types of access specifiers in C++ programming language:

private
public

protected

A private member within a class denotes that only members of the same
class have accessibility. The private member is inaccessible from outside the
class.
.

Public members are accessible from outside the class.


.

A protected access specifier is a stage between private and public access. If


member functions defined in a class are protected, they cannot be accessed
from outside the class but can be accessed from the derived class.

Creation of Objects:
Once the class is created, one or more objects can be created from the class as objects are
instance of the class.

How to Access C++ Class Members


It is possible to access the class members after a class is defined and objects are created.
General syntax to access class member:
Object_name.function_name (arguments);

The dot (.) used above is called the dot operator or class member access operator. The dot
operator is used to connect the object and the member function

What is a Stream?
A stream is an object where a program can either insert or extract characters to or from it. The
standard input and output stream objects of C++ are declared in the header file iostream.

Standard Input Stream


Generally, the device used for input is the keyboard. For inputting, the keyword cin is used,
which is an object. The overloaded operator of extraction, >>, is used on the standard input
stream, in this case: cin stream. Syntax for using the standard input stream is cin followed by the
operator >> followed by the variable that stores the data extracted from the stream.

What is Type Conversion


It is the process of converting one type into another. In other words converting an expression of a
given type into another is called type casting.

How to achieve this


There are two ways of achieving the type conversion namely:
Automatic Conversion otherwise called as Implicit Conversion
Type casting otherwise called as Explicit Conversion

What is the use of Constructor


The main use of constructors is to initialize objects. The function of initialization is automatically
carried out by the use of a special member function called a constructor.

Copy constructor:
This constructor takes one argument. Also called one argument constructor. The main use of
copy constructor is to initialize the objects while in creation, also used to copy an object. The
copy constructor allows the programmer to create a new object from an existing one by
initialization.
For example to invoke a copy constructor the programmer writes:
Exforsys e3(e2);
or
Exforsys e3=e2;
Both the above formats can be sued to invoke a copy constructor.
For Example:
#include <iostream.h>
class Exforsys
{
private:
int a;
public:
Exforsys()
{ }
Exforsys(int w)
{
a=w;
}
Exforsys(Exforsys& e)
{
a=e.a;
cout<< Example of Copy Constructor;
}
void result()
{
cout<< a;
}
};

void main()

{
Exforsys e1(50);
Exforsys e3(e1);
cout<< ne3=;e3.result();

In the above the copy constructor takes one argument an object of type Exforsys which is passed
by reference. The output of the above program is

Example of Copy Constructor


e3=50

Some important points about constructors:

A constructor takes the same name as the class name.


The programmer cannot declare a constructor as virtual or static, nor can the
programmer declare a constructor as const, volatile, or const volatile.

No return type is specified for a constructor.

The constructor must be defined in the public. The constructor must be a


public member.

Overloading of constructors is possible. This will be explained in later sections


of this tutorial.

Destructors
What is the use of Destructors
Destructors are also special member functions used in C++ programming language. Destructors
have the opposite function of a constructor. The main use of destructors is to release dynamic

allocated memory. Destructors are used to free memory, release resources and to perform other
clean up. Destructors are automatically named when an object is destroyed. Like constructors,
destructors also take the same name as that of the class name.
Some important points about destructors:
Destructors take the same name as the class name.
Like the constructor, the destructor must also be defined in the public. The destructor
must be a public member.

The Destructor does not take any argument which means that destructors cannot be
overloaded.

No return type is specified for destructors.

What is an array?
An array is a group of elements of the same type that are placed in contiguous memory locations.

How to access an array element?


You can access an element of an array by adding an index to a unique identifier.

What is a Multidimensional Array?


A Multidimensional array is an array of arrays.

What is a Structure?
Structure is a collection of variables under a single name. Variables can be of any type: int, float,
char etc. The main difference between structure and array is that arrays are collections of the
same data type and structure is a collection of variables under a single name.

Operator Overloading

What is a function?
A function is a structure that has a number of program statements grouped as a unit with a name
given to the unit. Function can be invoked from any part of the C++ program.

C++ Function Passing Types


The arguments passed to a function can be performed in two ways:

Passed By Value
Passed By Reference

Passed By Reference:
Passed by reference indicates a contrast in that the variables are not passed by value. When a
variable is passed by reference, it passes the variable to the function definition and not the copies
of the value. Any changes to the variable in function definition would effect or reflect
corresponding changes in the variables passed as reference.
The symbol used for denoting the passed by reference is & (ampersand).

C++ Inline Functions


What is Inline Function?
Inline functions are functions where the call is made to inline functions. The actual code then
gets placed in the calling program.

What happens when an inline function is written?


The inline function takes the format as a normal function but when it is compiled it is compiled
as inline code. The function is placed separately as inline function, thus adding readability to the
source program. When the program is compiled, the code present in function body is replaced in
the place of function call.

Scope of Variables in Function


The scope of the variables can be broadly be classified as

Local Variables
Global Variables

Local Variables:
The variables defined local to the block of the function would be accessible only within the block
of the function and not outside the function. Such variables are called local variables

Global Variables:
Global variables are one which are visible in any part of the program code and can be used
within all functions and outside all functions used in the program. The method of declaring
global variables is to declare the variable outside the function or block.

C++ Inheritance

What is Inheritance?
Inheritance is the process by which new classes called derived classes are created from existing
classes called base classes. The derived classes have all the features of the base class and the
programmer can choose to add new features specific to the newly created derived class.
For example, a programmer can create a base class named fruit and define derived classes as
mango, orange, banana, etc. Each of these derived classes, (mango, orange, banana, etc.) has
all the features of the base class (fruit) with additional attributes or features specific to these
newly created derived classes. Mango would have its own defined features, orange would have
its own defined features, banana would have its own defined features, etc.
This concept of Inheritance leads to the concept of polymorphism.

Features or Advantages of Inheritance:


Reusability:
Inheritance helps the code to be reused in many situations. The base class is defined and once it
is compiled, it need not be reworked. Using the concept of inheritance, the programmer can
create as many derived classes from the base class as needed while adding specific features to
each derived class as needed.

Saves Time and Effort:


The above concept of reusability achieved by inheritance saves the programmer time and effort.
Since the main code written can be reused in various situations as needed.
Increases Program Structure which results in greater reliability.

eneral Format for implementing the concept of


Inheritance:
class derived_classname: access specifier baseclassname

For example, if the base class is exforsys and the derived class is sample it is specified as:
class sample: public exforsys

The above makes sample have access to both public and protected variables of base class
exforsys. Reminder about public, private and protected access specifiers:

If a member or variables defined in a class is private, then they are


accessible by members of the same class only and cannot be accessed from
outside the class.
.
Public members and variables are accessible from outside the class.
.
Protected access specifier is a stage between private and public. If a
member functions or variables defined in a class are protected, then
they cannot be accessed from outside the class but can be accessed from
the derived class.

Inheritance Example:
class exforsys
{
public:
exforsys(void) { x=0; }
void f(int n1)
{
x= n1*5;
}

void output(void) { cout<<x; }

private:
int x;
};

class sample: public exforsys


{
public:
sample(void) { s1=0; }

void f1(int n1)


{
s1=n1*10;
}

void output(void)
{
exforsys::output();
cout << s1;
}

private:
int s1;
};

int main(void)
{
sample s;
s.f(10);
s.output();
s.f1(20);
s.output();
}

The output of the above program is


50
200

In the above example, the derived class is sample and the base class is exforsys. The derived
class defined above has access to all public and private variables. Derived classes cannot have
access to base class constructors and destructors. The derived class would be able to add new
member functions, or variables, or new constructors or new destructors. In the above example,
the derived class sample has new member function f1( ) added in it. The line:
sample s;

creates a derived class object named as s. When this is created, space is allocated for the data
members inherited from the base class exforsys and space is additionally allocated for the data
members defined in the derived class sample.
The base class constructor exforsys is used to initialize the base class data members and the
derived class constructor sample is used to initialize the data members defined in derived class.

Static Functions - An Introduction


The static member functions have a class scope and they do not have access to the 'this' pointer
of the class. When a member is declared as static that is a static member of class have only one
such data for the entire class even though there are many objects created for the class. The main
usage of static function is when the programmer wants to have a function which is accessible
even when the class is not instantiated.

Accessing Static Function:


We have seen in our earlier sections about accessing a normal member function. To recollect a
normal member function gets accessed using the object and an operator called as the dot member
access operator. The function declared as static or static functions gets accessed using just the
class name and the operator called as scope resolution operator which is not possible in case of
normal member functions.
A non-static member functions can be declared as virtual but care must be taken not to
declare a static member function as virtual.

What is Abstraction
The concept of abstraction relates to the idea of hiding data that are not needed for presentation.
The main idea behind data abstraction is to give a clear separation between properties of data
type and the associated implementation details. This separation is achieved in order that the
properties of the abstract data type are visible to the user interface and the implementation details
are hidden. Thus, abstraction forms the basic platform for the creation of user-defined data types
called objects. Data abstraction is the process of refining data to its essential form. An Abstract
Data Type is defined as a data type that is defined in terms of the operations that it supports and
not in terms of its structure or implementation.
In object-oriented programming language C++, it is possible to create and provide an interface
that accesses only certain elements of data types. The programmer can decide which user to give
or grant access to and hide the other details. This concept is called data hiding which is similar in
concept to data abstraction.

How Types of Abstraction Differs:


There are two broad types of abstraction; functional abstraction and data abstraction. The
main difference between functional abstraction and data abstraction is that functional abstraction
refers to a function that can be used without taking into account how the function is
implemented. Data abstraction refers to the data that can be used without taking into account
how the data are stored. There is also a difference in the way the access takes place in functional
abstraction and data abstraction. In functional abstraction, access to the function is provided

through a specific interface defined to invoke the function. In contrast, in data abstraction, access
to the data is provided through a specific set of operations defined to examine and manipulate the
data. For instance, when a programmer is using C++ standard data types, this means that users
are using the concept of data abstraction. When using data types, the users are not concerned
with how the data is stored but they are concerned with what operations are provided and what
properties are supported.

Reasons for the need of Abstraction


Flexibility in approach:
By hiding data or abstracting details that are not needed for presentation, the programmer
achieves greater flexibility in approach.
Enhanced Security:
Abstraction gives access to data or details that are needed by users and hide the implementation
details, giving enhanced security to application.
Easier Replacement:
With the concept of abstraction in object-oriented programming language, it is possible to
replace code without recompilation. This makes the process easier and saves time for users.
Modular Approach:
In object-oriented programming language C++, the abstraction concept helps users to divide the
project application into modules and test each of them separately. Then all modules are
integrated and ultimately tested together. This approach makes the application development
easier.
There are various ways of achieving abstraction in object-oriented programming language C++.
One approach is to take modular based code that is broken apart into smaller segments, known as
functions. This functional or modular approach helps the code to be reused again and again when
needed. For example, a programmer might write a function for computing an average and
another programmer might write a function for computing salary. These functions can be reused
when needed, by anyone. The modular based approach helps to centralize all data of a similar
type, under the control of a type module. Defining module types allow the module to be an
abstract data type. In many other programming languages, there is a small drawback associated
with the approach to accessing module type.

C++ Encapsulation
Encapsulation is the process of combining data and functions into a single unit called class.
Using the method of encapsulation, the programmer cannot directly access the data. Data is only
accessible through the functions present inside the class. Data encapsulation led to the important
concept of data hiding. Data hiding is the implementation details of a class that are hidden from
the user. The concept of restricted access led programmers to write specialized functions or

methods for performing the operations on hidden members of the class. Attention must be paid to
ensure that the class is designed properly.
Neither too much access nor too much control must be placed on the operations in order to make
the class user friendly. Hiding the implementation details and providing restrictive access leads
to the concept of abstract data type. Encapsulation leads to the concept of data hiding, but the
concept of encapsulation must not be restricted to information hiding. Encapsulation clearly
represents the ability to bundle related data and functionality within a single, autonomous entity
called a class.
For instance:
class Exforsys
{
public:
int sample();
int example(char *se)
int endfunc();
.........
......... //Other member functions
private:
int x;
float sq;
..........
......... //Other data members
};

In the above example, the data members integer x, float sq and other data members and member
functions sample(),example(char* se),endfunc() and other member functions are bundled and put
inside a single autonomous entity called class Exforsys. This exemplifies the concept of
Encapsulation. This special feature is available in object-oriented language C++ but not available
in procedural language C. There are advantages of using this encapsulated approach in C++. One
advantage is that it reduces human errors. The data and functions bundled inside the class take
total control of maintenance and thus human errors are reduced. It is clear from the above
example that the encapsulated objects act as a black box for other parts of the program through
interaction. Although encapsulated objects provide functionality, the calling objects will not
know the implementation details. This enhances the security of the application.
The key strength behind Data Encapsulation in C++ is that the keywords or the access specifiers
can be placed in the class declaration as public, protected or private. A class placed after the
keyword public is accessible to all the users of the class. The elements placed after the keyword
private are accessible only to the methods of the class. In between the public and the private
access specifiers, there exists the protected access specifier. Elements placed after the keyword
protected are accessible only to the methods of the class or classes derived from that class.

The concept of encapsulation shows that a non-member function cannot access an object's
private or protected data. This adds security, but in some cases the programmer might require an
unrelated function to operate on an object of two different classes. The programmer is then able
to utilize the concept of friend functions. Encapsulation alone is a powerful feature that leads to
information hiding, abstract data type and friend functions.

Features and Advantages of the concept of Encapsulation:


* Makes Maintenance of Application Easier:
Complex and critical applications are difficult to maintain. The cost associated with maintaining
the application is higher than that of developing the application properly. To resolve this
maintenance difficulty, the object-oriented programming language C++ created the concept of
encapsulation which bundles data and related functions together as a unit called class. Thus,
making maintenance much easier on the class level.
* Improves the Understandability of the Application
* Enhanced Security:
There are numerous reasons for the enhancement of security using the concept of Encapsulation
in C++. The access specifier acts as the key strength behind the concept of security and provides
access to members of class as needed by users. This prevents unauthorized access. If an
application needs to be extended or customized in later stages of development, the task of adding
new functions becomes easier without breaking existing code or applications, there by giving an
additional security to existing application.

C++ Polymorphism
Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives
different meanings or functions to the operators or functions. Poly, referring to many, signifies
the many uses of these operators and functions. A single function usage or an operator
functioning in many ways can be called polymorphism. Polymorphism refers to codes,
operations or objects that behave differently in different contexts.

Types of Polymorphism:
C++ provides three different types of polymorphism.

Virtual functions
Function name overloading

Operator overloading

In addition to the above three types of polymorphism, there exist other kinds of polymorphism:

run-time
compile-time

ad-hoc polymorphism

parametric polymorphism

Other types of polymorphism defined:

run-time:
The run-time polymorphism is implemented with inheritance and virtual functions.

compile-time:
The compile-time polymorphism is implemented with templates.

ad-hoc polymorphism:
If the range of actual types that can be used is finite and the combinations must be individually
specified prior to use, this is called ad-hoc polymorphism.

parametric polymorphism:
If all code is written without mention of any specific type and thus can be used transparently
with any number of new types it is called parametric polymorphism.

C++ Virtual Functions


Virtual, as the name implies, is something that exists in effect but not in reality. The concept of
virtual function is the same as a function, but it does not really exist although it appears in
needed places in a program.
The functionality of virtual functions can be over-ridden in its derived classes.

Need for Virtual Function:


The vital reason for having a virtual function is to implement a different functionality in the
derived class.

For example: a Make function in a class Vehicle may have to make a Vehicle with red color. A
class called FourWheeler, derived or inherited from Vehicle, may have to use a blue background
and 4 tires as wheels. For this scenario, the Make function for FourWheeler should now have a
different functionality from the one at the class called Vehicle. This concept is called Virtual
Function.

Properties of Virtual Functions:

Dynamic Binding Property:

Virtual Functions are resolved during run-time or dynamic binding. Virtual functions are also
simple member functions. The main difference between a non-virtual C++ member function and
a virtual member function is in the way they are both resolved. A non-virtual C++ member
function is resolved during compile time or static binding. Virtual Functions are resolved during
run-time or dynamic binding

Virtual functions are member functions of a class.


Virtual functions are declared with the keyword virtual, detailed in an
example below.

Virtual function takes a different functionality in the derived class.

Declaration of Virtual Function:


Virtual functions are member functions declared with the keyword virtual.

C++ Pure Virtual Function and Virtual Base Class


Pure Virtual Function is a Virtual function with no body.

Declaration of Pure Virtual Function:


Since pure virtual function has no body, the programmer must add the notation =0 for declaration
of the pure virtual function in the base class.

What is a Friend Function?


A friend function is used for accessing the non-public members of a class. A class can allow nonmember functions and other classes to access its own private data, by making them friends. Thus,
a friend function is an ordinary function or a member of another class.

How to define and use Friend Function in C++:


The friend function is written as any other normal function, except the function declaration of
these functions is preceded with the keyword friend. The friend function must have the class to
which it is declared as friend passed to it in argument.

Some important points to note while using friend


functions in C++:

The keyword friend is placed only in the function declaration of the


friend function and not in the function definition.
.
It is possible to declare a function as friend in any number of classes.
.

When a class is declared as a friend, the friend class has access to the
private data of the class that made this a friend.
.

A friend function, even though it is not a member function, would have


the rights to access the private members of the class.
.

It is possible to declare the friend function as either private or


public.
.

The function can be invoked without the use of an object. The friend
function has its argument as objects, seen in example below.

C++ Static Functions


Static member functions have a class scope and they do not have access to the 'this' pointer of the
class. When a member is declared as static, a static member of class, it has only one data for the
entire class even though there are many objects created for the class. The main usage of static
function is when the programmer wants to have a function which is accessible even when the
class is not instantiated.

Defining Static Function:


Static function is defined by using the keyword static before the member function that is to be
declared as static function.

Accessing Static Function:


A normal member function is accessed using the object and an operator called the dot member
access operator. The functions declared static or static functions are accessed using only the class

name and the scope resolution operator, unlike in normal member functions where these are not
used.
The programmer must note the following while using static member functions:

A static member function can only access static member data, static member functions
and data and functions outside the class. The programmer must take note not to use static
member function in the same manner as non-static member function, as non-static
member function can access all of the above including the static data member.
.
A non-static member function can be declared as virtual but care must be taken not to
declare a static member function as virtual.
.

The programmer must first understand the concept of static data while learning the
context of static functions. It is possible to declare a data member of a class as static
irrespective of it being a public or a private type in class definition. If a data is declared
as static, then the static data is created and initialized only once. Non-static data members
are created again and again. For each separate object of the class, the static data is created
and initialized only once. As in the concept of static data, all objects of the class in static
functions share the variables. This applies to all objects of the class.
.

A non-static member function can be called only after instantiating the class as an object.
This is not the case with static member functions. A static member function can be called,
even when a class is not instantiated.
.

A static member function cannot have access to the 'this' pointer of the class.

C++ Pointers

Concept of Pointers:
Every storage location of memory has an associated address. Address is a number that grows
sequentially. For every program placed in memory, each variable or function in the program has
an associated address.

The address of operator:


The address of operator or Reference operator is denoted by the notation &. When the user wants
to get the address of a variable, then the reference operator & can be used. The operator & is
used to find the address associated with a variable.

The syntax of the reference operator is as follows:


&variablename

This means that the address of the variablename is achieved.

C++ Void Pointer and Null Pointer

Pointer to Void
General Syntax:
void* pointer_variable;

Void is used as a keyword.


Referring back to pointer definitions and usage, it is known that the data type the pointer variable
defines is the same as the data type the pointer points to. The address placed in a pointer must
have the same type as the pointer.
Pointer to void, or a void pointer, is a special type of pointer that has a great facility of pointing
to any data type. There are limitations in the usage of void pointers that are explained below.
The concept of dereferencing using the operator * has been explained in an earlier section of this
tutorial. The programmer must note that void pointers cannot be de-referenced in the same
manner. Direct dereferencing of void pointer is not permitted. The programmer must change the
pointer to void as any other pointer type that points to valid data types such as, int, char, float and
then dereference it. This conversion of pointer to some other valid data type is achieved by using
the concept of type-casting (refer to type-casting section of this tutorial).

NULL Pointer:
The concept of NULL pointer is different from the above concept of void pointer. NULL pointer
is a type of pointer of any data type and generally takes a value as zero. This is, however, not
mandatory. This denotes that NULL pointer does not point to any valid memory address.

The difference between void pointers and NULL pointers:


A Void pointer is a special type of pointer of void and denotes that it can point to any data type.
NULL pointers can take any pointer type, but do not point to any valid reference or memory
address. It is important to note that a NULL pointer is different from a pointer that is not
initialized.

C++ Memory Management operators

Need for Memory Management operators


The concept of arrays has a block of memory reserved. The disadvantage with the concept of
arrays is that the programmer must know, while programming, the size of memory to be
allocated in addition to the array size remaining constant.

What are memory management operators?


There are two types of memory management operators in C++:

new
delete

These two memory management operators are used for allocating and freeing memory blocks in
efficient and convenient ways.

New operator:
The new operator in C++ is used for dynamic storage allocation. This operator can be used to
create object of any type.

delete operator:
The delete operator in C++ is used for releasing memory space when the object is no longer
needed. Once a new operator is used, it is efficient to use the corresponding delete operator for
release of memory.
General syntax of delete operator in C++:
The general syntax of delete operator in C++ is as follows:
delete pointer variable;

In the above example, delete is a keyword and the pointer variable is the pointer that points to the
objects already created in the new operator. Some of the important points the programmer must
note while using memory management operators are described below:

The programmer must take care not to free or delete a pointer variable
that has already been deleted.
.

Overloading of new and delete operator is possible (to be discussed in


detail in later section on overloading).
.

We know that sizeof operator is used for computing the size of the
object. Using memory management operator, the size of the object is
automatically computed.
.

The programmer must take care not to free or delete pointer variables
that have not been allocated using a new operator.
.

Null pointer is returned by the new operator when there is insufficient


memory available for allocation.

You might also like