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

PPTX
Dynamic memory allocation
Burhanuddin Kapadia
 
PPTX
C pointer
University of Potsdam
 
PPTX
Function C programming
Appili Vamsi Krishna
 
PPT
Pointers (Pp Tminimizer)
tech4us
 
PDF
SPL 9 | Scope of Variables in C
Mohammad Imam Hossain
 
PPTX
C programming - String
Achyut Devkota
 
PPTX
Input and Output In C Language
Adnan Khan
 
PPTX
Strings in c++
Neeru Mittal
 
Dynamic memory allocation
Burhanuddin Kapadia
 
Function C programming
Appili Vamsi Krishna
 
Pointers (Pp Tminimizer)
tech4us
 
SPL 9 | Scope of Variables in C
Mohammad Imam Hossain
 
C programming - String
Achyut Devkota
 
Input and Output In C Language
Adnan Khan
 
Strings in c++
Neeru Mittal
 

What's hot (20)

PPTX
arrays in c
vidhi mehta
 
PPT
Preprocessor in C
Prabhu Govind
 
PDF
Function in C
Dr. Abhineet Anand
 
PPT
pointers
teach4uin
 
PPTX
functions of C++
tarandeep_kaur
 
PPT
String c
thirumalaikumar3
 
PPTX
Pointer to function 1
Abu Bakr Ramadan
 
PDF
Strings IN C
yndaravind
 
PPT
Strings
Nilesh Dalvi
 
PPT
User defined functions in C programmig
Appili Vamsi Krishna
 
PPT
Enumerated data types in C
Arpana shree
 
PPTX
Functions in c language
tanmaymodi4
 
PPTX
Structure in C language
CGC Technical campus,Mohali
 
PPTX
Typedef
vaseemkhn
 
PPTX
Java string , string buffer and wrapper class
SimoniShah6
 
PPTX
Strings in C
Kamal Acharya
 
PPT
Exception handling and templates
farhan amjad
 
PPTX
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
PDF
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
PPTX
16 dynamic-memory-allocation
Rohit Shrivastava
 
arrays in c
vidhi mehta
 
Preprocessor in C
Prabhu Govind
 
Function in C
Dr. Abhineet Anand
 
pointers
teach4uin
 
functions of C++
tarandeep_kaur
 
Pointer to function 1
Abu Bakr Ramadan
 
Strings IN C
yndaravind
 
Strings
Nilesh Dalvi
 
User defined functions in C programmig
Appili Vamsi Krishna
 
Enumerated data types in C
Arpana shree
 
Functions in c language
tanmaymodi4
 
Structure in C language
CGC Technical campus,Mohali
 
Typedef
vaseemkhn
 
Java string , string buffer and wrapper class
SimoniShah6
 
Strings in C
Kamal Acharya
 
Exception handling and templates
farhan amjad
 
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
16 dynamic-memory-allocation
Rohit Shrivastava
 
Ad

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

PPTX
Pointers in C++ object oriented programming
Ahmad177077
 
PPTX
Introduction to pointers in c plus plus .
karimibaryal1996
 
PPTX
C++ FUNCTIONS-1.pptx
ShashiShash2
 
PPT
Unit 6 pointers
George Erfesoglou
 
PPT
C++ Functions.ppt
WaheedAnwar20
 
PPT
CPP Language Basics - Reference
Mohammed Sikander
 
PDF
C Programming - Refresher - Part III
Emertxe Information Technologies Pvt Ltd
 
DOCX
Arrry structure Stacks in data structure
lodhran-hayat
 
PPT
Data structure and problem solving ch05.ppt
Ping261512
 
PPTX
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
ab11167
 
PPT
Lecture2.ppt
Sabaunnisa3
 
ODP
C++ Secure Programming
Marian Marinov
 
PPT
8 Pointers
Praveen M Jigajinni
 
PPTX
Data Structures asdasdasdasdasdasdasdasdasdasd
abdulrahman39ab
 
PPT
Pointer
saeeb12
 
PDF
C++ Nested loops, matrix and fuctions.pdf
yamew16788
 
PPTX
COM1407: Working with Pointers
Hemantha Kulathilake
 
PDF
c programming
Arun Umrao
 
PPTX
Pointer in C++
Mauryasuraj98
 
PDF
chapter-11-pointers.pdf
study material
 
Pointers in C++ object oriented programming
Ahmad177077
 
Introduction to pointers in c plus plus .
karimibaryal1996
 
C++ FUNCTIONS-1.pptx
ShashiShash2
 
Unit 6 pointers
George Erfesoglou
 
C++ Functions.ppt
WaheedAnwar20
 
CPP Language Basics - Reference
Mohammed Sikander
 
C Programming - Refresher - Part III
Emertxe Information Technologies Pvt Ltd
 
Arrry structure Stacks in data structure
lodhran-hayat
 
Data structure and problem solving ch05.ppt
Ping261512
 
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
ab11167
 
Lecture2.ppt
Sabaunnisa3
 
C++ Secure Programming
Marian Marinov
 
Data Structures asdasdasdasdasdasdasdasdasdasd
abdulrahman39ab
 
Pointer
saeeb12
 
C++ Nested loops, matrix and fuctions.pdf
yamew16788
 
COM1407: Working with Pointers
Hemantha Kulathilake
 
c programming
Arun Umrao
 
Pointer in C++
Mauryasuraj98
 
chapter-11-pointers.pdf
study material
 
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
 

Recently uploaded (20)

PPTX
Ocean_and_Freshwater_Awareness_Presentation.pptx
Suhaira9
 
PPTX
Assam' Vibrant Bihu Festival Bihu presentation.pptx
rpmsbarman
 
DOCX
Policies & Procedures of Internal Audit Department of Shelter Holding LLC.docx
AlamGir100
 
PPTX
Information Security and Risk Management.pptx
prembasnet12
 
PPTX
AMFI - Investor Awareness Presentation.pptx
ssuser89d308
 
PPTX
Public Speakingbjdsbkjfdkjdasnlkdasnlknadslnbsjknsakjscbnkjbncs.pptx
ranazunairriaz1
 
PDF
Enhancing Bambara Groundnut Production Through Improved Agronomic Practices
Francois Stepman
 
PPTX
garment-industry in bangladesh. how bangladeshi industry is doing
tanvirhossain1570
 
PPTX
2025-08-03 Joseph 01 (shared slides).pptx
Dale Wells
 
PPTX
2025-07-27 Abraham 09 (shared slides).pptx
Dale Wells
 
DOCX
Ss Peter & Paul Choir Formation Training
kiambutownshipsecond
 
PDF
Green Natural Green House Presentation (2).pdf
SaeedOsman6
 
PDF
Advanced-Web-Design-Crafting-the-Future-Web (1).pdf
vaghelavidhiba591
 
PPTX
Raksha Bandhan Celebrations PPT festival
sowmyabapuram
 
PDF
Media Training for Authors: Producing Videos & Nailing Interviews
Paula Rizzo
 
PDF
protein structure and function for basics .pdf
RakeshKumar508211
 
PPTX
Cohort Study_PPT.group presentation_pdf.pptx
fatemakhan242
 
PPTX
IBA DISTRICT PIR PRESENTATION.POWERPOINT
ROGELIOLADIERO1
 
PDF
Mathematics Grade 11 Term 1 Week 1_2021.pdf
MalepyaneMokgatle
 
PPTX
Introductions to artificial intelligence
rakshjain77
 
Ocean_and_Freshwater_Awareness_Presentation.pptx
Suhaira9
 
Assam' Vibrant Bihu Festival Bihu presentation.pptx
rpmsbarman
 
Policies & Procedures of Internal Audit Department of Shelter Holding LLC.docx
AlamGir100
 
Information Security and Risk Management.pptx
prembasnet12
 
AMFI - Investor Awareness Presentation.pptx
ssuser89d308
 
Public Speakingbjdsbkjfdkjdasnlkdasnlknadslnbsjknsakjscbnkjbncs.pptx
ranazunairriaz1
 
Enhancing Bambara Groundnut Production Through Improved Agronomic Practices
Francois Stepman
 
garment-industry in bangladesh. how bangladeshi industry is doing
tanvirhossain1570
 
2025-08-03 Joseph 01 (shared slides).pptx
Dale Wells
 
2025-07-27 Abraham 09 (shared slides).pptx
Dale Wells
 
Ss Peter & Paul Choir Formation Training
kiambutownshipsecond
 
Green Natural Green House Presentation (2).pdf
SaeedOsman6
 
Advanced-Web-Design-Crafting-the-Future-Web (1).pdf
vaghelavidhiba591
 
Raksha Bandhan Celebrations PPT festival
sowmyabapuram
 
Media Training for Authors: Producing Videos & Nailing Interviews
Paula Rizzo
 
protein structure and function for basics .pdf
RakeshKumar508211
 
Cohort Study_PPT.group presentation_pdf.pptx
fatemakhan242
 
IBA DISTRICT PIR PRESENTATION.POWERPOINT
ROGELIOLADIERO1
 
Mathematics Grade 11 Term 1 Week 1_2021.pdf
MalepyaneMokgatle
 
Introductions to artificial intelligence
rakshjain77
 

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);