SlideShare a Scribd company logo
2
Most read
3
Most read
7
Most read
‘ Dynamic objects, pointer to function, array
and pointers, character string processing ’
Presented By:
Muskaan (MCA/25020/18)
Prashi Jain (MCA/25022/18)
DYNAMIC OBJECTS
C++ supports dynamic memory allocation and deallocation . C++
allocates memory and initializes the member variables.
An object can be created at run-time ; such an object is called a
dynamic object.
The new and delete operators are used to allocate and
deallocate memory to such objects.
NEW Operator
The new operator is used to allocate memory at runtime.
The memory is allocated in bytes.
Syntax:
DATA TYPE *ptr = new DATA TYPE;
// Pointer initialized with NULL
// Then request memory for the variable
int *p = NULL;
p = new int;
OR
// Combine declaration of pointer
// and their assignment
int *p = new int;
We can initialize a variable while dynamical allocation in the
following way:
int *ptr = new int (4);
Example:
#include<iostream.h>
#include<conio.h>
int main()
{
int *ptr = new int;
*ptr = 4;
cout << *ptr << endl ;
return 0;
}
OUTPUT: 4
DELETE Operator
A dynamic object can be distroyed by using the DELETE
operator
Syntax :
delete ptr;
Here ptr is the pointer to the dynamically allocated
variable
The delete operator simply returns the memory allocated
back to the operating system so that it can be used again.
Let’s look at an example:
int main()
{
int *ptr = new int;
*ptr = 4;
cout << *ptr << endl;
delete ptr;
return 0;
}
OUTPUT: 4
ADVANTAGES:
The main advantage of using dynamic memory allocation
is preventing the wastage of memory.
We can allocate (create) additional storage whenever we
need them.
We can de-allocate (free/delete) dynamic space whenever
we are done with them
POINTER TO FUNCTION
C++ allows you to pass a pointer to a function.
Simply declare the function parameter as a pointer type.
 Syntax:
data type fun_name(data type *arg)
Let’s look at an example:
#include <iostream.h>
void swap( int *a, int *b )
{
int t;
t = *a;
*a = *b;
*b = t;
}
int main()
{
int num1, num2;
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;
swap( &num1, &num2);
cout << "First number = " << num1 << endl;
cout << "Second number = " << num2 << endl;
return 0;
}
OUTPUT:
Enter first number
2
Enter second number
4
First number = 4
Second number = 2
Arrays and pointers
Array
An array is collection of items stored at
continues memory locations.
Syntax : Int arr[10]; //Array declaration by
specifying size
Pointers
A pointer is a variable whose value is the
address of another variable. Like any variable or
constant.
Syntax : type * var_name; //declaration of an
pointer
Example: int *p,a;
p=&a;
Arrays and pointers
Arrays and pointers are very closely related in c++.
For Example: An array declared as
Int a[10]
Can also be accessed using its pointers representation . The
name of the array is a constant pointer to the first element of the
array. So a can be considered as const int*.
Here arr points to the
first element of the array
For example:
In array and pointers ptr[i] and*(ptr+i) are considered as same.
Int a[4]={10,20,30,40};
Int *ptr = a;
for(int i =0 ; i<4; i++)
{
cout<< *(ptr+i) <<endl;
}
Output: 10
20
30
40
For the same array
For(int i=0 ; i<4;i++)
{
Cout<< ptr[i] <<endl;
}
Output:
10
20
30
40
Character string processing
String:
In C++, the one-dimensional array of characters
are called strings, which is terminated by a null
character 0.
Syntax: char greeting[6];
What is character string processing?
Character string processing basically means
accessing (insertion and extraction operators)
the characters from a string.
C++ provides following two types of string
representations −
• The C-style character string.
• The string class type introduced with Standard
C++.
Insertion operator : The result of inserting a char pointer to
an output stream is same as displaying a char array whose
first element is located at the address to which the char*
object points.
Example: Char text[9]= “world”;
For(char *ptr = text; *ptr!= ‘0’ ; ++ptr)
{ Cout<< ptr << endl;
}
Output : world
orld
rld
ld
d
Extraction operator
When the right operand of an extraction operator is a char*
object, the behaves same as extraction from char array- by
default leading white space is skipped and the next
nonwhitespace string of characters is extracted.
For example: char str[40];
Cout<<“enter a string:” ;
Cin>> str;
Cout<<“you entered :” <<str <<endl;
}
Output : enter a string : programming is fun
you entered: programming
String functions
1- strlen(): returns the length of the sytring.
Syntax: int strlen(s1);
2- strcpy(): copies one string to another string.
Syntax: char *strcpy(s1 , s2);
3- strcat(): this function concates two strings.
syntax: char *strcat(s1 , s2 );
4- strcmp(): compares two strings.
syntax: strcmp(s1 ,s2);
THANK YOU

More Related Content

What's hot (20)

PDF
7BCEE2A - UNIT V - STACK ORGANIZATION.pdf
RajendranSheeba
 
PDF
Dbms interview questions s.pdf
Shivani139202
 
PPT
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Sowmya Jyothi
 
PPTX
CLIENT SERVER IN OS.ppt
suman yadav
 
PPT
History of c++
Ihsan Ali
 
PDF
Exception handling
Pranali Chaudhari
 
PPTX
Memory Hierarchy
chauhankapil
 
PPT
process creation OS
Kiran Kumar Thota
 
PDF
Computer architecture
Zuhaib Zaroon
 
PPTX
Inline function in C++
Jenish Patel
 
PDF
10. switch case
Way2itech
 
DOCX
Nonrecursive predictive parsing
alldesign
 
PPT
Memory allocation in c
Prabhu Govind
 
PPTX
Constructor and Destructor
Sunipa Bera
 
PPTX
Tokens in C++
Mahender Boda
 
PPTX
File system structure
sangrampatil81
 
PPTX
Decision Making and Looping
Munazza-Mah-Jabeen
 
PPT
Memory Addressing
chauhankapil
 
PPSX
Conditional statement
Maxie Santos
 
7BCEE2A - UNIT V - STACK ORGANIZATION.pdf
RajendranSheeba
 
Dbms interview questions s.pdf
Shivani139202
 
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Sowmya Jyothi
 
CLIENT SERVER IN OS.ppt
suman yadav
 
History of c++
Ihsan Ali
 
Exception handling
Pranali Chaudhari
 
Memory Hierarchy
chauhankapil
 
process creation OS
Kiran Kumar Thota
 
Computer architecture
Zuhaib Zaroon
 
Inline function in C++
Jenish Patel
 
10. switch case
Way2itech
 
Nonrecursive predictive parsing
alldesign
 
Memory allocation in c
Prabhu Govind
 
Constructor and Destructor
Sunipa Bera
 
Tokens in C++
Mahender Boda
 
File system structure
sangrampatil81
 
Decision Making and Looping
Munazza-Mah-Jabeen
 
Memory Addressing
chauhankapil
 
Conditional statement
Maxie Santos
 

Similar to Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing (20)

PPTX
arraytypes of array and pointer string in c++.pptx
harinipradeep15
 
PPTX
DSA-Lecture03-Pointers, Strings(outputs).pptx
mubashirahmed0296
 
PPT
Lecture2.ppt
Sabaunnisa3
 
PDF
C++ computer language chapter 4 pointers.pdf
birukh36
 
PPTX
Data Structures asdasdasdasdasdasdasdasdasdasd
abdulrahman39ab
 
PPT
8 Pointers
Praveen M Jigajinni
 
DOCX
Arrry structure Stacks in data structure
lodhran-hayat
 
PPTX
The string class
Syed Zaid Irshad
 
PPT
Pointer
manish840
 
PPTX
20.C++Pointer.pptx
AtharvPotdar2
 
PPTX
Arrays & Strings.pptx
AnkurRajSingh2
 
PPTX
Introduction to Arrays and Strings.pptx
DawitTekie1
 
PPTX
Chapter 1 Introduction to Arrays and Strings
DawitTekie1
 
PPTX
C++ - UNIT_-_IV.pptx which contains details about Pointers
ANUSUYA S
 
PDF
Chapter 3 - Characters and Strings - Student.pdf
mylinhbangus
 
PDF
Strinng Classes in c++
Vikash Dhal
 
PPTX
Pointers in C++ object oriented programming
Ahmad177077
 
PPTX
Introduction to pointers in c plus plus .
karimibaryal1996
 
PPTX
Chapter1.pptx
WondimuBantihun1
 
arraytypes of array and pointer string in c++.pptx
harinipradeep15
 
DSA-Lecture03-Pointers, Strings(outputs).pptx
mubashirahmed0296
 
Lecture2.ppt
Sabaunnisa3
 
C++ computer language chapter 4 pointers.pdf
birukh36
 
Data Structures asdasdasdasdasdasdasdasdasdasd
abdulrahman39ab
 
Arrry structure Stacks in data structure
lodhran-hayat
 
The string class
Syed Zaid Irshad
 
Pointer
manish840
 
20.C++Pointer.pptx
AtharvPotdar2
 
Arrays & Strings.pptx
AnkurRajSingh2
 
Introduction to Arrays and Strings.pptx
DawitTekie1
 
Chapter 1 Introduction to Arrays and Strings
DawitTekie1
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
ANUSUYA S
 
Chapter 3 - Characters and Strings - Student.pdf
mylinhbangus
 
Strinng Classes in c++
Vikash Dhal
 
Pointers in C++ object oriented programming
Ahmad177077
 
Introduction to pointers in c plus plus .
karimibaryal1996
 
Chapter1.pptx
WondimuBantihun1
 
Ad

More from Meghaj Mallick (20)

PPT
24 partial-orderings
Meghaj Mallick
 
PPTX
PORTFOLIO BY USING HTML & CSS
Meghaj Mallick
 
PPTX
Introduction to Software Testing
Meghaj Mallick
 
PPTX
Introduction to System Programming
Meghaj Mallick
 
PPTX
MACRO ASSEBLER
Meghaj Mallick
 
PPTX
Icons, Image & Multimedia
Meghaj Mallick
 
PPTX
Project Tracking & SPC
Meghaj Mallick
 
PPTX
Peephole Optimization
Meghaj Mallick
 
PPTX
Routing in MANET
Meghaj Mallick
 
PPTX
Macro assembler
Meghaj Mallick
 
PPTX
Architecture and security in Vanet PPT
Meghaj Mallick
 
PPTX
Design Model & User Interface Design in Software Engineering
Meghaj Mallick
 
PPTX
Text Mining of Twitter in Data Mining
Meghaj Mallick
 
PPTX
DFS & BFS in Computer Algorithm
Meghaj Mallick
 
PPTX
Software Development Method
Meghaj Mallick
 
PPTX
Secant method in Numerical & Statistical Method
Meghaj Mallick
 
PPTX
Motivation in Organization
Meghaj Mallick
 
PPTX
Communication Skill
Meghaj Mallick
 
PPT
Partial-Orderings in Discrete Mathematics
Meghaj Mallick
 
PPTX
Hashing In Data Structure
Meghaj Mallick
 
24 partial-orderings
Meghaj Mallick
 
PORTFOLIO BY USING HTML & CSS
Meghaj Mallick
 
Introduction to Software Testing
Meghaj Mallick
 
Introduction to System Programming
Meghaj Mallick
 
MACRO ASSEBLER
Meghaj Mallick
 
Icons, Image & Multimedia
Meghaj Mallick
 
Project Tracking & SPC
Meghaj Mallick
 
Peephole Optimization
Meghaj Mallick
 
Routing in MANET
Meghaj Mallick
 
Macro assembler
Meghaj Mallick
 
Architecture and security in Vanet PPT
Meghaj Mallick
 
Design Model & User Interface Design in Software Engineering
Meghaj Mallick
 
Text Mining of Twitter in Data Mining
Meghaj Mallick
 
DFS & BFS in Computer Algorithm
Meghaj Mallick
 
Software Development Method
Meghaj Mallick
 
Secant method in Numerical & Statistical Method
Meghaj Mallick
 
Motivation in Organization
Meghaj Mallick
 
Communication Skill
Meghaj Mallick
 
Partial-Orderings in Discrete Mathematics
Meghaj Mallick
 
Hashing In Data Structure
Meghaj Mallick
 
Ad

Recently uploaded (19)

PPTX
some leadership theories MBA management.pptx
rkseo19
 
PPTX
STURGEON BAY WI AG PPT JULY 6 2025.pptx
FamilyWorshipCenterD
 
PPTX
Pastor Bob Stewart Acts 21 07 09 2025.pptx
FamilyWorshipCenterD
 
PDF
Committee-Skills-Handbook---MUNprep.org.pdf
SatvikAgarwal9
 
PPTX
presentation on legal and regulatory action
raoharsh4122001
 
DOCX
How Digital Marketplaces are Empowering Emerging MedTech Brands
Ram Gopal Varma
 
PPTX
Inspired by VeinSense: Supercharge Your Hackathon with Agentic AI
ShubhamSharma2528
 
PDF
Model Project Report_36DR_G&P.pdf for investors understanding
MeetAgrawal23
 
PDF
From Draft to DSN - How to Get your Paper In [DSN 2025 Doctoral Forum Keynote]
vschiavoni
 
PPTX
Presentationexpressions You are student leader and have just come from a stud...
BENSTARBEATZ
 
PDF
Leveraging the Power of Jira Dashboard.pdf
siddharthshukla742740
 
PPTX
Great-Books. Powerpoint presentation. files
tamayocrisgie
 
PPTX
BARRIERS TO EFFECTIVE COMMUNICATION.pptx
shraddham25
 
PDF
Buy Verified Payoneer Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
PDF
The Family Secret (essence of loveliness)
Favour Biodun
 
PDF
The Origin - A Simple Presentation on any project
RishabhDwivedi43
 
PPTX
AI presentation for everyone in every fields
dodinhkhai1
 
PDF
Buy Verified Coinbase Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
PDF
The Impact of Game Live Streaming on In-Game Purchases of Chinese Young Game ...
Shibaura Institute of Technology
 
some leadership theories MBA management.pptx
rkseo19
 
STURGEON BAY WI AG PPT JULY 6 2025.pptx
FamilyWorshipCenterD
 
Pastor Bob Stewart Acts 21 07 09 2025.pptx
FamilyWorshipCenterD
 
Committee-Skills-Handbook---MUNprep.org.pdf
SatvikAgarwal9
 
presentation on legal and regulatory action
raoharsh4122001
 
How Digital Marketplaces are Empowering Emerging MedTech Brands
Ram Gopal Varma
 
Inspired by VeinSense: Supercharge Your Hackathon with Agentic AI
ShubhamSharma2528
 
Model Project Report_36DR_G&P.pdf for investors understanding
MeetAgrawal23
 
From Draft to DSN - How to Get your Paper In [DSN 2025 Doctoral Forum Keynote]
vschiavoni
 
Presentationexpressions You are student leader and have just come from a stud...
BENSTARBEATZ
 
Leveraging the Power of Jira Dashboard.pdf
siddharthshukla742740
 
Great-Books. Powerpoint presentation. files
tamayocrisgie
 
BARRIERS TO EFFECTIVE COMMUNICATION.pptx
shraddham25
 
Buy Verified Payoneer Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
The Family Secret (essence of loveliness)
Favour Biodun
 
The Origin - A Simple Presentation on any project
RishabhDwivedi43
 
AI presentation for everyone in every fields
dodinhkhai1
 
Buy Verified Coinbase Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
The Impact of Game Live Streaming on In-Game Purchases of Chinese Young Game ...
Shibaura Institute of Technology
 

Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing

  • 1. ‘ Dynamic objects, pointer to function, array and pointers, character string processing ’ Presented By: Muskaan (MCA/25020/18) Prashi Jain (MCA/25022/18)
  • 2. DYNAMIC OBJECTS C++ supports dynamic memory allocation and deallocation . C++ allocates memory and initializes the member variables. An object can be created at run-time ; such an object is called a dynamic object. The new and delete operators are used to allocate and deallocate memory to such objects.
  • 3. NEW Operator The new operator is used to allocate memory at runtime. The memory is allocated in bytes. Syntax: DATA TYPE *ptr = new DATA TYPE; // Pointer initialized with NULL // Then request memory for the variable int *p = NULL; p = new int; OR // Combine declaration of pointer // and their assignment int *p = new int;
  • 4. We can initialize a variable while dynamical allocation in the following way: int *ptr = new int (4); Example: #include<iostream.h> #include<conio.h> int main() { int *ptr = new int; *ptr = 4; cout << *ptr << endl ; return 0; } OUTPUT: 4
  • 5. DELETE Operator A dynamic object can be distroyed by using the DELETE operator Syntax : delete ptr; Here ptr is the pointer to the dynamically allocated variable The delete operator simply returns the memory allocated back to the operating system so that it can be used again.
  • 6. Let’s look at an example: int main() { int *ptr = new int; *ptr = 4; cout << *ptr << endl; delete ptr; return 0; } OUTPUT: 4
  • 7. ADVANTAGES: The main advantage of using dynamic memory allocation is preventing the wastage of memory. We can allocate (create) additional storage whenever we need them. We can de-allocate (free/delete) dynamic space whenever we are done with them
  • 8. POINTER TO FUNCTION C++ allows you to pass a pointer to a function. Simply declare the function parameter as a pointer type.  Syntax: data type fun_name(data type *arg)
  • 9. Let’s look at an example: #include <iostream.h> void swap( int *a, int *b ) { int t; t = *a; *a = *b; *b = t; } int main() { int num1, num2; cout << "Enter first number" << endl; cin >> num1; cout << "Enter second number" << endl; cin >> num2; swap( &num1, &num2); cout << "First number = " << num1 << endl; cout << "Second number = " << num2 << endl; return 0; }
  • 10. OUTPUT: Enter first number 2 Enter second number 4 First number = 4 Second number = 2
  • 12. Array An array is collection of items stored at continues memory locations. Syntax : Int arr[10]; //Array declaration by specifying size
  • 13. Pointers A pointer is a variable whose value is the address of another variable. Like any variable or constant. Syntax : type * var_name; //declaration of an pointer Example: int *p,a; p=&a;
  • 14. Arrays and pointers Arrays and pointers are very closely related in c++. For Example: An array declared as Int a[10] Can also be accessed using its pointers representation . The name of the array is a constant pointer to the first element of the array. So a can be considered as const int*. Here arr points to the first element of the array
  • 15. For example: In array and pointers ptr[i] and*(ptr+i) are considered as same. Int a[4]={10,20,30,40}; Int *ptr = a; for(int i =0 ; i<4; i++) { cout<< *(ptr+i) <<endl; } Output: 10 20 30 40
  • 16. For the same array For(int i=0 ; i<4;i++) { Cout<< ptr[i] <<endl; } Output: 10 20 30 40
  • 17. Character string processing String: In C++, the one-dimensional array of characters are called strings, which is terminated by a null character 0. Syntax: char greeting[6];
  • 18. What is character string processing? Character string processing basically means accessing (insertion and extraction operators) the characters from a string. C++ provides following two types of string representations − • The C-style character string. • The string class type introduced with Standard C++.
  • 19. Insertion operator : The result of inserting a char pointer to an output stream is same as displaying a char array whose first element is located at the address to which the char* object points. Example: Char text[9]= “world”; For(char *ptr = text; *ptr!= ‘0’ ; ++ptr) { Cout<< ptr << endl; } Output : world orld rld ld d
  • 20. Extraction operator When the right operand of an extraction operator is a char* object, the behaves same as extraction from char array- by default leading white space is skipped and the next nonwhitespace string of characters is extracted. For example: char str[40]; Cout<<“enter a string:” ; Cin>> str; Cout<<“you entered :” <<str <<endl; } Output : enter a string : programming is fun you entered: programming
  • 21. String functions 1- strlen(): returns the length of the sytring. Syntax: int strlen(s1); 2- strcpy(): copies one string to another string. Syntax: char *strcpy(s1 , s2); 3- strcat(): this function concates two strings. syntax: char *strcat(s1 , s2 ); 4- strcmp(): compares two strings. syntax: strcmp(s1 ,s2);